1 // SPDX-License-Identifier: GPL-2.0-only
2 // SPDX-FileCopyrightText: Copyright (c) 2020-2024 NVIDIA CORPORATION & AFFILIATES.
3 // All rights reserved.
5 // tegra210_i2s.c - Tegra210 I2S driver
8 #include <linux/device.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/of_graph.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
15 #include <sound/core.h>
16 #include <sound/pcm_params.h>
17 #include <sound/simple_card_utils.h>
18 #include <sound/soc.h>
19 #include "tegra210_i2s.h"
20 #include "tegra_cif.h"
22 static const struct reg_default tegra210_i2s_reg_defaults
[] = {
23 { TEGRA210_I2S_RX_INT_MASK
, 0x00000003 },
24 { TEGRA210_I2S_RX_CIF_CTRL
, 0x00007700 },
25 { TEGRA210_I2S_TX_INT_MASK
, 0x00000003 },
26 { TEGRA210_I2S_TX_CIF_CTRL
, 0x00007700 },
27 { TEGRA210_I2S_CG
, 0x1 },
28 { TEGRA210_I2S_TIMING
, 0x0000001f },
29 { TEGRA210_I2S_ENABLE
, 0x1 },
31 * Below update does not have any effect on Tegra186 and Tegra194.
32 * On Tegra210, I2S4 has "i2s4a" and "i2s4b" pins and below update
33 * is required to select i2s4b for it to be functional for I2S
36 { TEGRA210_I2S_CYA
, 0x1 },
39 static void tegra210_i2s_set_slot_ctrl(struct regmap
*regmap
,
40 unsigned int total_slots
,
41 unsigned int tx_slot_mask
,
42 unsigned int rx_slot_mask
)
44 regmap_write(regmap
, TEGRA210_I2S_SLOT_CTRL
, total_slots
- 1);
45 regmap_write(regmap
, TEGRA210_I2S_TX_SLOT_CTRL
, tx_slot_mask
);
46 regmap_write(regmap
, TEGRA210_I2S_RX_SLOT_CTRL
, rx_slot_mask
);
49 static int tegra210_i2s_set_clock_rate(struct device
*dev
,
50 unsigned int clock_rate
)
52 struct tegra210_i2s
*i2s
= dev_get_drvdata(dev
);
56 regmap_read(i2s
->regmap
, TEGRA210_I2S_CTRL
, &val
);
58 /* No need to set rates if I2S is being operated in slave */
59 if (!(val
& I2S_CTRL_MASTER_EN
))
62 err
= clk_set_rate(i2s
->clk_i2s
, clock_rate
);
64 dev_err(dev
, "can't set I2S bit clock rate %u, err: %d\n",
69 if (!IS_ERR(i2s
->clk_sync_input
)) {
71 * Other I/O modules in AHUB can use i2s bclk as reference
72 * clock. Below sets sync input clock rate as per bclk,
73 * which can be used as input to other I/O modules.
75 err
= clk_set_rate(i2s
->clk_sync_input
, clock_rate
);
78 "can't set I2S sync input rate %u, err = %d\n",
87 static int tegra210_i2s_sw_reset(struct snd_soc_component
*compnt
,
90 struct device
*dev
= compnt
->dev
;
91 struct tegra210_i2s
*i2s
= dev_get_drvdata(dev
);
92 unsigned int reset_mask
= I2S_SOFT_RESET_MASK
;
93 unsigned int reset_en
= I2S_SOFT_RESET_EN
;
94 unsigned int reset_reg
, cif_reg
, stream_reg
;
95 unsigned int cif_ctrl
, stream_ctrl
, i2s_ctrl
, val
;
98 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
99 reset_reg
= TEGRA210_I2S_RX_SOFT_RESET
;
100 cif_reg
= TEGRA210_I2S_RX_CIF_CTRL
;
101 stream_reg
= TEGRA210_I2S_RX_CTRL
;
103 reset_reg
= TEGRA210_I2S_TX_SOFT_RESET
;
104 cif_reg
= TEGRA210_I2S_TX_CIF_CTRL
;
105 stream_reg
= TEGRA210_I2S_TX_CTRL
;
108 /* Store CIF and I2S control values */
109 regmap_read(i2s
->regmap
, cif_reg
, &cif_ctrl
);
110 regmap_read(i2s
->regmap
, stream_reg
, &stream_ctrl
);
111 regmap_read(i2s
->regmap
, TEGRA210_I2S_CTRL
, &i2s_ctrl
);
113 /* Reset to make sure the previous transactions are clean */
114 regmap_update_bits(i2s
->regmap
, reset_reg
, reset_mask
, reset_en
);
116 err
= regmap_read_poll_timeout(i2s
->regmap
, reset_reg
, val
,
117 !(val
& reset_mask
& reset_en
),
120 dev_err(dev
, "timeout: failed to reset I2S for %s\n",
121 snd_pcm_direction_name(stream
));
125 /* Restore CIF and I2S control values */
126 regmap_write(i2s
->regmap
, cif_reg
, cif_ctrl
);
127 regmap_write(i2s
->regmap
, stream_reg
, stream_ctrl
);
128 regmap_write(i2s
->regmap
, TEGRA210_I2S_CTRL
, i2s_ctrl
);
133 static int tegra210_i2s_init(struct snd_soc_dapm_widget
*w
,
134 struct snd_kcontrol
*kcontrol
, int event
)
136 struct snd_soc_component
*compnt
= snd_soc_dapm_to_component(w
->dapm
);
137 struct device
*dev
= compnt
->dev
;
138 struct tegra210_i2s
*i2s
= dev_get_drvdata(dev
);
139 unsigned int val
, status_reg
;
144 case TEGRA210_I2S_RX_ENABLE
:
145 stream
= SNDRV_PCM_STREAM_PLAYBACK
;
146 status_reg
= TEGRA210_I2S_RX_STATUS
;
148 case TEGRA210_I2S_TX_ENABLE
:
149 stream
= SNDRV_PCM_STREAM_CAPTURE
;
150 status_reg
= TEGRA210_I2S_TX_STATUS
;
156 /* Ensure I2S is in disabled state before new session */
157 err
= regmap_read_poll_timeout(i2s
->regmap
, status_reg
, val
,
158 !(val
& I2S_EN_MASK
& I2S_EN
),
161 dev_err(dev
, "timeout: previous I2S %s is still active\n",
162 snd_pcm_direction_name(stream
));
166 return tegra210_i2s_sw_reset(compnt
, stream
);
169 static int __maybe_unused
tegra210_i2s_runtime_suspend(struct device
*dev
)
171 struct tegra210_i2s
*i2s
= dev_get_drvdata(dev
);
173 regcache_cache_only(i2s
->regmap
, true);
174 regcache_mark_dirty(i2s
->regmap
);
176 clk_disable_unprepare(i2s
->clk_i2s
);
181 static int __maybe_unused
tegra210_i2s_runtime_resume(struct device
*dev
)
183 struct tegra210_i2s
*i2s
= dev_get_drvdata(dev
);
186 err
= clk_prepare_enable(i2s
->clk_i2s
);
188 dev_err(dev
, "failed to enable I2S bit clock, err: %d\n", err
);
192 regcache_cache_only(i2s
->regmap
, false);
193 regcache_sync(i2s
->regmap
);
198 static void tegra210_i2s_set_data_offset(struct tegra210_i2s
*i2s
,
199 unsigned int data_offset
)
202 regmap_update_bits(i2s
->regmap
, TEGRA210_I2S_TX_CTRL
,
203 I2S_CTRL_DATA_OFFSET_MASK
,
204 data_offset
<< I2S_DATA_SHIFT
);
207 regmap_update_bits(i2s
->regmap
, TEGRA210_I2S_RX_CTRL
,
208 I2S_CTRL_DATA_OFFSET_MASK
,
209 data_offset
<< I2S_DATA_SHIFT
);
212 static int tegra210_i2s_set_fmt(struct snd_soc_dai
*dai
,
215 struct tegra210_i2s
*i2s
= snd_soc_dai_get_drvdata(dai
);
216 unsigned int mask
, val
;
218 mask
= I2S_CTRL_MASTER_EN_MASK
;
219 switch (fmt
& SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK
) {
220 case SND_SOC_DAIFMT_BC_FC
:
223 case SND_SOC_DAIFMT_BP_FP
:
224 val
= I2S_CTRL_MASTER_EN
;
230 mask
|= I2S_CTRL_FRAME_FMT_MASK
| I2S_CTRL_LRCK_POL_MASK
;
231 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
232 case SND_SOC_DAIFMT_DSP_A
:
233 val
|= I2S_CTRL_FRAME_FMT_FSYNC_MODE
;
234 val
|= I2S_CTRL_LRCK_POL_HIGH
;
235 tegra210_i2s_set_data_offset(i2s
, 1);
237 case SND_SOC_DAIFMT_DSP_B
:
238 val
|= I2S_CTRL_FRAME_FMT_FSYNC_MODE
;
239 val
|= I2S_CTRL_LRCK_POL_HIGH
;
240 tegra210_i2s_set_data_offset(i2s
, 0);
242 /* I2S mode has data offset of 1 */
243 case SND_SOC_DAIFMT_I2S
:
244 val
|= I2S_CTRL_FRAME_FMT_LRCK_MODE
;
245 val
|= I2S_CTRL_LRCK_POL_LOW
;
246 tegra210_i2s_set_data_offset(i2s
, 1);
249 * For RJ mode data offset is dependent on the sample size
250 * and the bclk ratio, and so is set when hw_params is called.
252 case SND_SOC_DAIFMT_RIGHT_J
:
253 val
|= I2S_CTRL_FRAME_FMT_LRCK_MODE
;
254 val
|= I2S_CTRL_LRCK_POL_HIGH
;
256 case SND_SOC_DAIFMT_LEFT_J
:
257 val
|= I2S_CTRL_FRAME_FMT_LRCK_MODE
;
258 val
|= I2S_CTRL_LRCK_POL_HIGH
;
259 tegra210_i2s_set_data_offset(i2s
, 0);
265 mask
|= I2S_CTRL_EDGE_CTRL_MASK
;
266 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
267 case SND_SOC_DAIFMT_NB_NF
:
268 val
|= I2S_CTRL_EDGE_CTRL_POS_EDGE
;
270 case SND_SOC_DAIFMT_NB_IF
:
271 val
|= I2S_CTRL_EDGE_CTRL_POS_EDGE
;
272 val
^= I2S_CTRL_LRCK_POL_MASK
;
274 case SND_SOC_DAIFMT_IB_NF
:
275 val
|= I2S_CTRL_EDGE_CTRL_NEG_EDGE
;
277 case SND_SOC_DAIFMT_IB_IF
:
278 val
|= I2S_CTRL_EDGE_CTRL_NEG_EDGE
;
279 val
^= I2S_CTRL_LRCK_POL_MASK
;
285 regmap_update_bits(i2s
->regmap
, TEGRA210_I2S_CTRL
, mask
, val
);
287 i2s
->dai_fmt
= fmt
& SND_SOC_DAIFMT_FORMAT_MASK
;
292 static int tegra210_i2s_set_tdm_slot(struct snd_soc_dai
*dai
,
293 unsigned int tx_mask
, unsigned int rx_mask
,
294 int slots
, int slot_width
)
296 struct tegra210_i2s
*i2s
= snd_soc_dai_get_drvdata(dai
);
298 /* Copy the required tx and rx mask */
299 i2s
->tx_mask
= (tx_mask
> DEFAULT_I2S_SLOT_MASK
) ?
300 DEFAULT_I2S_SLOT_MASK
: tx_mask
;
301 i2s
->rx_mask
= (rx_mask
> DEFAULT_I2S_SLOT_MASK
) ?
302 DEFAULT_I2S_SLOT_MASK
: rx_mask
;
307 static int tegra210_i2s_get_loopback(struct snd_kcontrol
*kcontrol
,
308 struct snd_ctl_elem_value
*ucontrol
)
310 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
311 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
313 ucontrol
->value
.integer
.value
[0] = i2s
->loopback
;
318 static int tegra210_i2s_put_loopback(struct snd_kcontrol
*kcontrol
,
319 struct snd_ctl_elem_value
*ucontrol
)
321 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
322 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
323 int value
= ucontrol
->value
.integer
.value
[0];
325 if (value
== i2s
->loopback
)
328 i2s
->loopback
= value
;
330 regmap_update_bits(i2s
->regmap
, TEGRA210_I2S_CTRL
, I2S_CTRL_LPBK_MASK
,
331 i2s
->loopback
<< I2S_CTRL_LPBK_SHIFT
);
336 static int tegra210_i2s_get_fsync_width(struct snd_kcontrol
*kcontrol
,
337 struct snd_ctl_elem_value
*ucontrol
)
339 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
340 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
342 ucontrol
->value
.integer
.value
[0] = i2s
->fsync_width
;
347 static int tegra210_i2s_put_fsync_width(struct snd_kcontrol
*kcontrol
,
348 struct snd_ctl_elem_value
*ucontrol
)
350 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
351 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
352 int value
= ucontrol
->value
.integer
.value
[0];
354 if (value
== i2s
->fsync_width
)
357 i2s
->fsync_width
= value
;
360 * Frame sync width is used only for FSYNC modes and not
361 * applicable for LRCK modes. Reset value for this field is "0",
362 * which means the width is one bit clock wide.
363 * The width requirement may depend on the codec and in such
364 * cases mixer control is used to update custom values. A value
365 * of "N" here means, width is "N + 1" bit clock wide.
367 regmap_update_bits(i2s
->regmap
, TEGRA210_I2S_CTRL
,
368 I2S_CTRL_FSYNC_WIDTH_MASK
,
369 i2s
->fsync_width
<< I2S_FSYNC_WIDTH_SHIFT
);
374 static int tegra210_i2s_cget_stereo_to_mono(struct snd_kcontrol
*kcontrol
,
375 struct snd_ctl_elem_value
*ucontrol
)
377 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
378 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
380 ucontrol
->value
.enumerated
.item
[0] = i2s
->stereo_to_mono
[I2S_TX_PATH
];
385 static int tegra210_i2s_cput_stereo_to_mono(struct snd_kcontrol
*kcontrol
,
386 struct snd_ctl_elem_value
*ucontrol
)
388 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
389 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
390 unsigned int value
= ucontrol
->value
.enumerated
.item
[0];
392 if (value
== i2s
->stereo_to_mono
[I2S_TX_PATH
])
395 i2s
->stereo_to_mono
[I2S_TX_PATH
] = value
;
400 static int tegra210_i2s_cget_mono_to_stereo(struct snd_kcontrol
*kcontrol
,
401 struct snd_ctl_elem_value
*ucontrol
)
403 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
404 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
406 ucontrol
->value
.enumerated
.item
[0] = i2s
->mono_to_stereo
[I2S_TX_PATH
];
411 static int tegra210_i2s_cput_mono_to_stereo(struct snd_kcontrol
*kcontrol
,
412 struct snd_ctl_elem_value
*ucontrol
)
414 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
415 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
416 unsigned int value
= ucontrol
->value
.enumerated
.item
[0];
418 if (value
== i2s
->mono_to_stereo
[I2S_TX_PATH
])
421 i2s
->mono_to_stereo
[I2S_TX_PATH
] = value
;
426 static int tegra210_i2s_pget_stereo_to_mono(struct snd_kcontrol
*kcontrol
,
427 struct snd_ctl_elem_value
*ucontrol
)
429 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
430 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
432 ucontrol
->value
.enumerated
.item
[0] = i2s
->stereo_to_mono
[I2S_RX_PATH
];
437 static int tegra210_i2s_pput_stereo_to_mono(struct snd_kcontrol
*kcontrol
,
438 struct snd_ctl_elem_value
*ucontrol
)
440 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
441 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
442 unsigned int value
= ucontrol
->value
.enumerated
.item
[0];
444 if (value
== i2s
->stereo_to_mono
[I2S_RX_PATH
])
447 i2s
->stereo_to_mono
[I2S_RX_PATH
] = value
;
452 static int tegra210_i2s_pget_mono_to_stereo(struct snd_kcontrol
*kcontrol
,
453 struct snd_ctl_elem_value
*ucontrol
)
455 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
456 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
458 ucontrol
->value
.enumerated
.item
[0] = i2s
->mono_to_stereo
[I2S_RX_PATH
];
463 static int tegra210_i2s_pput_mono_to_stereo(struct snd_kcontrol
*kcontrol
,
464 struct snd_ctl_elem_value
*ucontrol
)
466 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
467 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
468 unsigned int value
= ucontrol
->value
.enumerated
.item
[0];
470 if (value
== i2s
->mono_to_stereo
[I2S_RX_PATH
])
473 i2s
->mono_to_stereo
[I2S_RX_PATH
] = value
;
478 static int tegra210_i2s_pget_fifo_th(struct snd_kcontrol
*kcontrol
,
479 struct snd_ctl_elem_value
*ucontrol
)
481 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
482 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
484 ucontrol
->value
.integer
.value
[0] = i2s
->rx_fifo_th
;
489 static int tegra210_i2s_pput_fifo_th(struct snd_kcontrol
*kcontrol
,
490 struct snd_ctl_elem_value
*ucontrol
)
492 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
493 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
494 int value
= ucontrol
->value
.integer
.value
[0];
496 if (value
== i2s
->rx_fifo_th
)
499 i2s
->rx_fifo_th
= value
;
504 static int tegra210_i2s_get_bclk_ratio(struct snd_kcontrol
*kcontrol
,
505 struct snd_ctl_elem_value
*ucontrol
)
507 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
508 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
510 ucontrol
->value
.integer
.value
[0] = i2s
->bclk_ratio
;
515 static int tegra210_i2s_put_bclk_ratio(struct snd_kcontrol
*kcontrol
,
516 struct snd_ctl_elem_value
*ucontrol
)
518 struct snd_soc_component
*compnt
= snd_soc_kcontrol_component(kcontrol
);
519 struct tegra210_i2s
*i2s
= snd_soc_component_get_drvdata(compnt
);
520 int value
= ucontrol
->value
.integer
.value
[0];
522 if (value
== i2s
->bclk_ratio
)
525 i2s
->bclk_ratio
= value
;
530 static int tegra210_i2s_set_dai_bclk_ratio(struct snd_soc_dai
*dai
,
533 struct tegra210_i2s
*i2s
= snd_soc_dai_get_drvdata(dai
);
535 i2s
->bclk_ratio
= ratio
;
540 static int tegra210_i2s_set_timing_params(struct device
*dev
,
541 unsigned int sample_size
,
543 unsigned int channels
)
545 struct tegra210_i2s
*i2s
= dev_get_drvdata(dev
);
546 unsigned int val
, bit_count
, bclk_rate
, num_bclk
= sample_size
;
550 num_bclk
*= i2s
->bclk_ratio
;
552 if (i2s
->dai_fmt
== SND_SOC_DAIFMT_RIGHT_J
)
553 tegra210_i2s_set_data_offset(i2s
, num_bclk
- sample_size
);
555 /* I2S bit clock rate */
556 bclk_rate
= srate
* channels
* num_bclk
;
558 err
= tegra210_i2s_set_clock_rate(dev
, bclk_rate
);
560 dev_err(dev
, "can't set I2S bit clock rate %u, err: %d\n",
565 regmap_read(i2s
->regmap
, TEGRA210_I2S_CTRL
, &val
);
568 * For LRCK mode, channel bit count depends on number of bit clocks
569 * on the left channel, where as for FSYNC mode bit count depends on
570 * the number of bit clocks in both left and right channels for DSP
571 * mode or the number of bit clocks in one TDM frame.
574 switch (val
& I2S_CTRL_FRAME_FMT_MASK
) {
575 case I2S_CTRL_FRAME_FMT_LRCK_MODE
:
576 bit_count
= (bclk_rate
/ (srate
* 2)) - 1;
578 case I2S_CTRL_FRAME_FMT_FSYNC_MODE
:
579 bit_count
= (bclk_rate
/ srate
) - 1;
581 tegra210_i2s_set_slot_ctrl(i2s
->regmap
, channels
,
582 i2s
->tx_mask
, i2s
->rx_mask
);
585 dev_err(dev
, "invalid I2S frame format\n");
589 if (bit_count
> I2S_TIMING_CH_BIT_CNT_MASK
) {
590 dev_err(dev
, "invalid I2S channel bit count %u\n", bit_count
);
594 regmap_write(i2s
->regmap
, TEGRA210_I2S_TIMING
,
595 bit_count
<< I2S_TIMING_CH_BIT_CNT_SHIFT
);
600 static int tegra210_i2s_hw_params(struct snd_pcm_substream
*substream
,
601 struct snd_pcm_hw_params
*params
,
602 struct snd_soc_dai
*dai
)
604 struct device
*dev
= dai
->dev
;
605 struct tegra210_i2s
*i2s
= snd_soc_dai_get_drvdata(dai
);
606 unsigned int sample_size
, channels
, srate
, val
, reg
, path
;
607 struct tegra_cif_conf cif_conf
;
608 snd_pcm_format_t sample_format
;
610 memset(&cif_conf
, 0, sizeof(struct tegra_cif_conf
));
612 channels
= params_channels(params
);
614 dev_err(dev
, "invalid I2S %d channel configuration\n",
619 cif_conf
.audio_ch
= channels
;
620 cif_conf
.client_ch
= channels
;
621 if (i2s
->client_channels
)
622 cif_conf
.client_ch
= i2s
->client_channels
;
624 /* AHUB CIF Audio bits configs */
625 switch (params_format(params
)) {
626 case SNDRV_PCM_FORMAT_S8
:
627 cif_conf
.audio_bits
= TEGRA_ACIF_BITS_8
;
629 case SNDRV_PCM_FORMAT_S16_LE
:
630 cif_conf
.audio_bits
= TEGRA_ACIF_BITS_16
;
632 case SNDRV_PCM_FORMAT_S24_LE
:
633 case SNDRV_PCM_FORMAT_S32_LE
:
634 cif_conf
.audio_bits
= TEGRA_ACIF_BITS_32
;
637 dev_err(dev
, "unsupported params audio bit format!\n");
641 sample_format
= params_format(params
);
642 if (i2s
->client_sample_format
>= 0)
643 sample_format
= (snd_pcm_format_t
)i2s
->client_sample_format
;
646 * Format of the I2S for sending/receiving the audio
647 * to/from external device.
649 switch (sample_format
) {
650 case SNDRV_PCM_FORMAT_S8
:
653 cif_conf
.client_bits
= TEGRA_ACIF_BITS_8
;
655 case SNDRV_PCM_FORMAT_S16_LE
:
658 cif_conf
.client_bits
= TEGRA_ACIF_BITS_16
;
660 case SNDRV_PCM_FORMAT_S24_LE
:
663 cif_conf
.client_bits
= TEGRA_ACIF_BITS_24
;
665 case SNDRV_PCM_FORMAT_S32_LE
:
668 cif_conf
.client_bits
= TEGRA_ACIF_BITS_32
;
671 dev_err(dev
, "unsupported client bit format!\n");
675 /* Program sample size */
676 regmap_update_bits(i2s
->regmap
, TEGRA210_I2S_CTRL
,
677 I2S_CTRL_BIT_SIZE_MASK
, val
);
679 srate
= params_rate(params
);
681 /* For playback I2S RX-CIF and for capture TX-CIF is used */
682 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
687 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
690 /* FIFO threshold in terms of frames */
691 max_th
= (I2S_RX_FIFO_DEPTH
/ cif_conf
.audio_ch
) - 1;
693 if (i2s
->rx_fifo_th
> max_th
)
694 i2s
->rx_fifo_th
= max_th
;
696 cif_conf
.threshold
= i2s
->rx_fifo_th
;
698 reg
= TEGRA210_I2S_RX_CIF_CTRL
;
700 reg
= TEGRA210_I2S_TX_CIF_CTRL
;
703 cif_conf
.mono_conv
= i2s
->mono_to_stereo
[path
];
704 cif_conf
.stereo_conv
= i2s
->stereo_to_mono
[path
];
706 tegra_set_cif(i2s
->regmap
, reg
, &cif_conf
);
708 return tegra210_i2s_set_timing_params(dev
, sample_size
, srate
,
712 static const struct snd_soc_dai_ops tegra210_i2s_dai_ops
= {
713 .set_fmt
= tegra210_i2s_set_fmt
,
714 .hw_params
= tegra210_i2s_hw_params
,
715 .set_bclk_ratio
= tegra210_i2s_set_dai_bclk_ratio
,
716 .set_tdm_slot
= tegra210_i2s_set_tdm_slot
,
719 static struct snd_soc_dai_driver tegra210_i2s_dais
[] = {
723 .stream_name
= "CIF-Playback",
726 .rates
= SNDRV_PCM_RATE_8000_192000
,
727 .formats
= SNDRV_PCM_FMTBIT_S8
|
728 SNDRV_PCM_FMTBIT_S16_LE
|
729 SNDRV_PCM_FMTBIT_S24_LE
|
730 SNDRV_PCM_FMTBIT_S32_LE
,
733 .stream_name
= "CIF-Capture",
736 .rates
= SNDRV_PCM_RATE_8000_192000
,
737 .formats
= SNDRV_PCM_FMTBIT_S8
|
738 SNDRV_PCM_FMTBIT_S16_LE
|
739 SNDRV_PCM_FMTBIT_S24_LE
|
740 SNDRV_PCM_FMTBIT_S32_LE
,
746 .stream_name
= "DAP-Playback",
749 .rates
= SNDRV_PCM_RATE_8000_192000
,
750 .formats
= SNDRV_PCM_FMTBIT_S8
|
751 SNDRV_PCM_FMTBIT_S16_LE
|
752 SNDRV_PCM_FMTBIT_S24_LE
|
753 SNDRV_PCM_FMTBIT_S32_LE
,
756 .stream_name
= "DAP-Capture",
759 .rates
= SNDRV_PCM_RATE_8000_192000
,
760 .formats
= SNDRV_PCM_FMTBIT_S8
|
761 SNDRV_PCM_FMTBIT_S16_LE
|
762 SNDRV_PCM_FMTBIT_S24_LE
|
763 SNDRV_PCM_FMTBIT_S32_LE
,
765 .ops
= &tegra210_i2s_dai_ops
,
770 static const char * const tegra210_i2s_stereo_conv_text
[] = {
774 static const char * const tegra210_i2s_mono_conv_text
[] = {
778 static const struct soc_enum tegra210_i2s_mono_conv_enum
=
779 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_i2s_mono_conv_text
),
780 tegra210_i2s_mono_conv_text
);
782 static const struct soc_enum tegra210_i2s_stereo_conv_enum
=
783 SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_i2s_stereo_conv_text
),
784 tegra210_i2s_stereo_conv_text
);
786 static const struct snd_kcontrol_new tegra210_i2s_controls
[] = {
787 SOC_SINGLE_EXT("Loopback", 0, 0, 1, 0, tegra210_i2s_get_loopback
,
788 tegra210_i2s_put_loopback
),
789 SOC_SINGLE_EXT("FSYNC Width", 0, 0, 255, 0,
790 tegra210_i2s_get_fsync_width
,
791 tegra210_i2s_put_fsync_width
),
792 SOC_ENUM_EXT("Capture Stereo To Mono", tegra210_i2s_stereo_conv_enum
,
793 tegra210_i2s_cget_stereo_to_mono
,
794 tegra210_i2s_cput_stereo_to_mono
),
795 SOC_ENUM_EXT("Capture Mono To Stereo", tegra210_i2s_mono_conv_enum
,
796 tegra210_i2s_cget_mono_to_stereo
,
797 tegra210_i2s_cput_mono_to_stereo
),
798 SOC_ENUM_EXT("Playback Stereo To Mono", tegra210_i2s_stereo_conv_enum
,
799 tegra210_i2s_pget_mono_to_stereo
,
800 tegra210_i2s_pput_mono_to_stereo
),
801 SOC_ENUM_EXT("Playback Mono To Stereo", tegra210_i2s_mono_conv_enum
,
802 tegra210_i2s_pget_stereo_to_mono
,
803 tegra210_i2s_pput_stereo_to_mono
),
804 SOC_SINGLE_EXT("Playback FIFO Threshold", 0, 0, I2S_RX_FIFO_DEPTH
- 1,
805 0, tegra210_i2s_pget_fifo_th
, tegra210_i2s_pput_fifo_th
),
806 SOC_SINGLE_EXT("BCLK Ratio", 0, 0, INT_MAX
, 0,
807 tegra210_i2s_get_bclk_ratio
,
808 tegra210_i2s_put_bclk_ratio
),
811 static const struct snd_soc_dapm_widget tegra210_i2s_widgets
[] = {
812 SND_SOC_DAPM_AIF_IN_E("RX", NULL
, 0, TEGRA210_I2S_RX_ENABLE
,
813 0, 0, tegra210_i2s_init
, SND_SOC_DAPM_PRE_PMU
),
814 SND_SOC_DAPM_AIF_OUT_E("TX", NULL
, 0, TEGRA210_I2S_TX_ENABLE
,
815 0, 0, tegra210_i2s_init
, SND_SOC_DAPM_PRE_PMU
),
816 SND_SOC_DAPM_MIC("MIC", NULL
),
817 SND_SOC_DAPM_SPK("SPK", NULL
),
820 static const struct snd_soc_dapm_route tegra210_i2s_routes
[] = {
821 /* Playback route from XBAR */
822 { "XBAR-Playback", NULL
, "XBAR-TX" },
823 { "CIF-Playback", NULL
, "XBAR-Playback" },
824 { "RX", NULL
, "CIF-Playback" },
825 { "DAP-Playback", NULL
, "RX" },
826 { "SPK", NULL
, "DAP-Playback" },
827 /* Capture route to XBAR */
828 { "XBAR-RX", NULL
, "XBAR-Capture" },
829 { "XBAR-Capture", NULL
, "CIF-Capture" },
830 { "CIF-Capture", NULL
, "TX" },
831 { "TX", NULL
, "DAP-Capture" },
832 { "DAP-Capture", NULL
, "MIC" },
835 static const struct snd_soc_component_driver tegra210_i2s_cmpnt
= {
836 .dapm_widgets
= tegra210_i2s_widgets
,
837 .num_dapm_widgets
= ARRAY_SIZE(tegra210_i2s_widgets
),
838 .dapm_routes
= tegra210_i2s_routes
,
839 .num_dapm_routes
= ARRAY_SIZE(tegra210_i2s_routes
),
840 .controls
= tegra210_i2s_controls
,
841 .num_controls
= ARRAY_SIZE(tegra210_i2s_controls
),
844 static bool tegra210_i2s_wr_reg(struct device
*dev
, unsigned int reg
)
847 case TEGRA210_I2S_RX_ENABLE
... TEGRA210_I2S_RX_SOFT_RESET
:
848 case TEGRA210_I2S_RX_INT_MASK
... TEGRA210_I2S_RX_CLK_TRIM
:
849 case TEGRA210_I2S_TX_ENABLE
... TEGRA210_I2S_TX_SOFT_RESET
:
850 case TEGRA210_I2S_TX_INT_MASK
... TEGRA210_I2S_TX_CLK_TRIM
:
851 case TEGRA210_I2S_ENABLE
... TEGRA210_I2S_CG
:
852 case TEGRA210_I2S_CTRL
... TEGRA210_I2S_CYA
:
859 static bool tegra210_i2s_rd_reg(struct device
*dev
, unsigned int reg
)
861 if (tegra210_i2s_wr_reg(dev
, reg
))
865 case TEGRA210_I2S_RX_STATUS
:
866 case TEGRA210_I2S_RX_INT_STATUS
:
867 case TEGRA210_I2S_RX_CIF_FIFO_STATUS
:
868 case TEGRA210_I2S_TX_STATUS
:
869 case TEGRA210_I2S_TX_INT_STATUS
:
870 case TEGRA210_I2S_TX_CIF_FIFO_STATUS
:
871 case TEGRA210_I2S_STATUS
:
872 case TEGRA210_I2S_INT_STATUS
:
879 static bool tegra210_i2s_volatile_reg(struct device
*dev
, unsigned int reg
)
882 case TEGRA210_I2S_RX_STATUS
:
883 case TEGRA210_I2S_RX_INT_STATUS
:
884 case TEGRA210_I2S_RX_CIF_FIFO_STATUS
:
885 case TEGRA210_I2S_TX_STATUS
:
886 case TEGRA210_I2S_TX_INT_STATUS
:
887 case TEGRA210_I2S_TX_CIF_FIFO_STATUS
:
888 case TEGRA210_I2S_STATUS
:
889 case TEGRA210_I2S_INT_STATUS
:
890 case TEGRA210_I2S_RX_SOFT_RESET
:
891 case TEGRA210_I2S_TX_SOFT_RESET
:
898 static const struct regmap_config tegra210_i2s_regmap_config
= {
902 .max_register
= TEGRA210_I2S_CYA
,
903 .writeable_reg
= tegra210_i2s_wr_reg
,
904 .readable_reg
= tegra210_i2s_rd_reg
,
905 .volatile_reg
= tegra210_i2s_volatile_reg
,
906 .reg_defaults
= tegra210_i2s_reg_defaults
,
907 .num_reg_defaults
= ARRAY_SIZE(tegra210_i2s_reg_defaults
),
908 .cache_type
= REGCACHE_FLAT
,
912 * The AHUB HW modules are interconnected with CIF which are capable of
913 * supporting Channel and Sample bit format conversion. This needs different
914 * CIF Audio and client configuration. As one of the config comes from
915 * params_channels() or params_format(), the extra configuration is passed from
916 * CIF Port of DT I2S node which can help to perform this conversion.
918 * 4ch audio = 4ch client = 2ch 2ch
919 * -----> ADMAIF -----------> CIF -------------> I2S ---->
921 static void tegra210_parse_client_convert(struct device
*dev
)
923 struct tegra210_i2s
*i2s
= dev_get_drvdata(dev
);
924 struct device_node
*ports
, *ep
;
925 struct simple_util_data data
= {};
928 ports
= of_get_child_by_name(dev
->of_node
, "ports");
930 ep
= of_graph_get_endpoint_by_regs(ports
, cif_port
, -1);
932 simple_util_parse_convert(ep
, NULL
, &data
);
938 if (data
.convert_channels
)
939 i2s
->client_channels
= data
.convert_channels
;
941 if (data
.convert_sample_format
)
942 i2s
->client_sample_format
= simple_util_get_sample_fmt(&data
);
945 static int tegra210_i2s_probe(struct platform_device
*pdev
)
947 struct device
*dev
= &pdev
->dev
;
948 struct tegra210_i2s
*i2s
;
952 i2s
= devm_kzalloc(dev
, sizeof(*i2s
), GFP_KERNEL
);
956 i2s
->rx_fifo_th
= DEFAULT_I2S_RX_FIFO_THRESHOLD
;
957 i2s
->tx_mask
= DEFAULT_I2S_SLOT_MASK
;
958 i2s
->rx_mask
= DEFAULT_I2S_SLOT_MASK
;
959 i2s
->loopback
= false;
960 i2s
->client_sample_format
= -EINVAL
;
962 dev_set_drvdata(dev
, i2s
);
964 i2s
->clk_i2s
= devm_clk_get(dev
, "i2s");
965 if (IS_ERR(i2s
->clk_i2s
)) {
966 dev_err(dev
, "can't retrieve I2S bit clock\n");
967 return PTR_ERR(i2s
->clk_i2s
);
971 * Not an error, as this clock is needed only when some other I/O
972 * requires input clock from current I2S instance, which is
973 * configurable from DT.
975 i2s
->clk_sync_input
= devm_clk_get(dev
, "sync_input");
976 if (IS_ERR(i2s
->clk_sync_input
))
977 dev_dbg(dev
, "can't retrieve I2S sync input clock\n");
979 regs
= devm_platform_ioremap_resource(pdev
, 0);
981 return PTR_ERR(regs
);
983 i2s
->regmap
= devm_regmap_init_mmio(dev
, regs
,
984 &tegra210_i2s_regmap_config
);
985 if (IS_ERR(i2s
->regmap
)) {
986 dev_err(dev
, "regmap init failed\n");
987 return PTR_ERR(i2s
->regmap
);
990 tegra210_parse_client_convert(dev
);
992 regcache_cache_only(i2s
->regmap
, true);
994 err
= devm_snd_soc_register_component(dev
, &tegra210_i2s_cmpnt
,
996 ARRAY_SIZE(tegra210_i2s_dais
));
998 dev_err(dev
, "can't register I2S component, err: %d\n", err
);
1002 pm_runtime_enable(dev
);
1007 static void tegra210_i2s_remove(struct platform_device
*pdev
)
1009 pm_runtime_disable(&pdev
->dev
);
1012 static const struct dev_pm_ops tegra210_i2s_pm_ops
= {
1013 SET_RUNTIME_PM_OPS(tegra210_i2s_runtime_suspend
,
1014 tegra210_i2s_runtime_resume
, NULL
)
1015 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1016 pm_runtime_force_resume
)
1019 static const struct of_device_id tegra210_i2s_of_match
[] = {
1020 { .compatible
= "nvidia,tegra210-i2s" },
1023 MODULE_DEVICE_TABLE(of
, tegra210_i2s_of_match
);
1025 static struct platform_driver tegra210_i2s_driver
= {
1027 .name
= "tegra210-i2s",
1028 .of_match_table
= tegra210_i2s_of_match
,
1029 .pm
= &tegra210_i2s_pm_ops
,
1031 .probe
= tegra210_i2s_probe
,
1032 .remove
= tegra210_i2s_remove
,
1034 module_platform_driver(tegra210_i2s_driver
)
1036 MODULE_AUTHOR("Songhee Baek <sbaek@nvidia.com>");
1037 MODULE_DESCRIPTION("Tegra210 ASoC I2S driver");
1038 MODULE_LICENSE("GPL v2");