1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // tegra210_i2s.c - Tegra210 I2S driver
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Copyright (c) 2020 NVIDIA CORPORATION. All rights reserved.
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/clk.h>
8*4882a593Smuzhiyun #include <linux/device.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/of_device.h>
11*4882a593Smuzhiyun #include <linux/platform_device.h>
12*4882a593Smuzhiyun #include <linux/pm_runtime.h>
13*4882a593Smuzhiyun #include <linux/regmap.h>
14*4882a593Smuzhiyun #include <sound/core.h>
15*4882a593Smuzhiyun #include <sound/pcm_params.h>
16*4882a593Smuzhiyun #include <sound/soc.h>
17*4882a593Smuzhiyun #include "tegra210_i2s.h"
18*4882a593Smuzhiyun #include "tegra_cif.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun static const struct reg_default tegra210_i2s_reg_defaults[] = {
21*4882a593Smuzhiyun { TEGRA210_I2S_RX_INT_MASK, 0x00000003 },
22*4882a593Smuzhiyun { TEGRA210_I2S_RX_CIF_CTRL, 0x00007700 },
23*4882a593Smuzhiyun { TEGRA210_I2S_TX_INT_MASK, 0x00000003 },
24*4882a593Smuzhiyun { TEGRA210_I2S_TX_CIF_CTRL, 0x00007700 },
25*4882a593Smuzhiyun { TEGRA210_I2S_CG, 0x1 },
26*4882a593Smuzhiyun { TEGRA210_I2S_TIMING, 0x0000001f },
27*4882a593Smuzhiyun { TEGRA210_I2S_ENABLE, 0x1 },
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun * Below update does not have any effect on Tegra186 and Tegra194.
30*4882a593Smuzhiyun * On Tegra210, I2S4 has "i2s4a" and "i2s4b" pins and below update
31*4882a593Smuzhiyun * is required to select i2s4b for it to be functional for I2S
32*4882a593Smuzhiyun * operation.
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun { TEGRA210_I2S_CYA, 0x1 },
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun
tegra210_i2s_set_slot_ctrl(struct regmap * regmap,unsigned int total_slots,unsigned int tx_slot_mask,unsigned int rx_slot_mask)37*4882a593Smuzhiyun static void tegra210_i2s_set_slot_ctrl(struct regmap *regmap,
38*4882a593Smuzhiyun unsigned int total_slots,
39*4882a593Smuzhiyun unsigned int tx_slot_mask,
40*4882a593Smuzhiyun unsigned int rx_slot_mask)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun regmap_write(regmap, TEGRA210_I2S_SLOT_CTRL, total_slots - 1);
43*4882a593Smuzhiyun regmap_write(regmap, TEGRA210_I2S_TX_SLOT_CTRL, tx_slot_mask);
44*4882a593Smuzhiyun regmap_write(regmap, TEGRA210_I2S_RX_SLOT_CTRL, rx_slot_mask);
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
tegra210_i2s_set_clock_rate(struct device * dev,unsigned int clock_rate)47*4882a593Smuzhiyun static int tegra210_i2s_set_clock_rate(struct device *dev,
48*4882a593Smuzhiyun unsigned int clock_rate)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun struct tegra210_i2s *i2s = dev_get_drvdata(dev);
51*4882a593Smuzhiyun unsigned int val;
52*4882a593Smuzhiyun int err;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun regmap_read(i2s->regmap, TEGRA210_I2S_CTRL, &val);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /* No need to set rates if I2S is being operated in slave */
57*4882a593Smuzhiyun if (!(val & I2S_CTRL_MASTER_EN))
58*4882a593Smuzhiyun return 0;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun err = clk_set_rate(i2s->clk_i2s, clock_rate);
61*4882a593Smuzhiyun if (err) {
62*4882a593Smuzhiyun dev_err(dev, "can't set I2S bit clock rate %u, err: %d\n",
63*4882a593Smuzhiyun clock_rate, err);
64*4882a593Smuzhiyun return err;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun if (!IS_ERR(i2s->clk_sync_input)) {
68*4882a593Smuzhiyun /*
69*4882a593Smuzhiyun * Other I/O modules in AHUB can use i2s bclk as reference
70*4882a593Smuzhiyun * clock. Below sets sync input clock rate as per bclk,
71*4882a593Smuzhiyun * which can be used as input to other I/O modules.
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun err = clk_set_rate(i2s->clk_sync_input, clock_rate);
74*4882a593Smuzhiyun if (err) {
75*4882a593Smuzhiyun dev_err(dev,
76*4882a593Smuzhiyun "can't set I2S sync input rate %u, err = %d\n",
77*4882a593Smuzhiyun clock_rate, err);
78*4882a593Smuzhiyun return err;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun return 0;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
tegra210_i2s_sw_reset(struct snd_soc_component * compnt,bool is_playback)85*4882a593Smuzhiyun static int tegra210_i2s_sw_reset(struct snd_soc_component *compnt,
86*4882a593Smuzhiyun bool is_playback)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun struct device *dev = compnt->dev;
89*4882a593Smuzhiyun struct tegra210_i2s *i2s = dev_get_drvdata(dev);
90*4882a593Smuzhiyun unsigned int reset_mask = I2S_SOFT_RESET_MASK;
91*4882a593Smuzhiyun unsigned int reset_en = I2S_SOFT_RESET_EN;
92*4882a593Smuzhiyun unsigned int reset_reg, cif_reg, stream_reg;
93*4882a593Smuzhiyun unsigned int cif_ctrl, stream_ctrl, i2s_ctrl, val;
94*4882a593Smuzhiyun int err;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun if (is_playback) {
97*4882a593Smuzhiyun reset_reg = TEGRA210_I2S_RX_SOFT_RESET;
98*4882a593Smuzhiyun cif_reg = TEGRA210_I2S_RX_CIF_CTRL;
99*4882a593Smuzhiyun stream_reg = TEGRA210_I2S_RX_CTRL;
100*4882a593Smuzhiyun } else {
101*4882a593Smuzhiyun reset_reg = TEGRA210_I2S_TX_SOFT_RESET;
102*4882a593Smuzhiyun cif_reg = TEGRA210_I2S_TX_CIF_CTRL;
103*4882a593Smuzhiyun stream_reg = TEGRA210_I2S_TX_CTRL;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun /* Store CIF and I2S control values */
107*4882a593Smuzhiyun regmap_read(i2s->regmap, cif_reg, &cif_ctrl);
108*4882a593Smuzhiyun regmap_read(i2s->regmap, stream_reg, &stream_ctrl);
109*4882a593Smuzhiyun regmap_read(i2s->regmap, TEGRA210_I2S_CTRL, &i2s_ctrl);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* Reset to make sure the previous transactions are clean */
112*4882a593Smuzhiyun regmap_update_bits(i2s->regmap, reset_reg, reset_mask, reset_en);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun err = regmap_read_poll_timeout(i2s->regmap, reset_reg, val,
115*4882a593Smuzhiyun !(val & reset_mask & reset_en),
116*4882a593Smuzhiyun 10, 10000);
117*4882a593Smuzhiyun if (err) {
118*4882a593Smuzhiyun dev_err(dev, "timeout: failed to reset I2S for %s\n",
119*4882a593Smuzhiyun is_playback ? "playback" : "capture");
120*4882a593Smuzhiyun return err;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /* Restore CIF and I2S control values */
124*4882a593Smuzhiyun regmap_write(i2s->regmap, cif_reg, cif_ctrl);
125*4882a593Smuzhiyun regmap_write(i2s->regmap, stream_reg, stream_ctrl);
126*4882a593Smuzhiyun regmap_write(i2s->regmap, TEGRA210_I2S_CTRL, i2s_ctrl);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun return 0;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
tegra210_i2s_init(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)131*4882a593Smuzhiyun static int tegra210_i2s_init(struct snd_soc_dapm_widget *w,
132*4882a593Smuzhiyun struct snd_kcontrol *kcontrol, int event)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_dapm_to_component(w->dapm);
135*4882a593Smuzhiyun struct device *dev = compnt->dev;
136*4882a593Smuzhiyun struct tegra210_i2s *i2s = dev_get_drvdata(dev);
137*4882a593Smuzhiyun unsigned int val, status_reg;
138*4882a593Smuzhiyun bool is_playback;
139*4882a593Smuzhiyun int err;
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun switch (w->reg) {
142*4882a593Smuzhiyun case TEGRA210_I2S_RX_ENABLE:
143*4882a593Smuzhiyun is_playback = true;
144*4882a593Smuzhiyun status_reg = TEGRA210_I2S_RX_STATUS;
145*4882a593Smuzhiyun break;
146*4882a593Smuzhiyun case TEGRA210_I2S_TX_ENABLE:
147*4882a593Smuzhiyun is_playback = false;
148*4882a593Smuzhiyun status_reg = TEGRA210_I2S_TX_STATUS;
149*4882a593Smuzhiyun break;
150*4882a593Smuzhiyun default:
151*4882a593Smuzhiyun return -EINVAL;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /* Ensure I2S is in disabled state before new session */
155*4882a593Smuzhiyun err = regmap_read_poll_timeout(i2s->regmap, status_reg, val,
156*4882a593Smuzhiyun !(val & I2S_EN_MASK & I2S_EN),
157*4882a593Smuzhiyun 10, 10000);
158*4882a593Smuzhiyun if (err) {
159*4882a593Smuzhiyun dev_err(dev, "timeout: previous I2S %s is still active\n",
160*4882a593Smuzhiyun is_playback ? "playback" : "capture");
161*4882a593Smuzhiyun return err;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun return tegra210_i2s_sw_reset(compnt, is_playback);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
tegra210_i2s_runtime_suspend(struct device * dev)167*4882a593Smuzhiyun static int __maybe_unused tegra210_i2s_runtime_suspend(struct device *dev)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun struct tegra210_i2s *i2s = dev_get_drvdata(dev);
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun regcache_cache_only(i2s->regmap, true);
172*4882a593Smuzhiyun regcache_mark_dirty(i2s->regmap);
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun clk_disable_unprepare(i2s->clk_i2s);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun return 0;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
tegra210_i2s_runtime_resume(struct device * dev)179*4882a593Smuzhiyun static int __maybe_unused tegra210_i2s_runtime_resume(struct device *dev)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun struct tegra210_i2s *i2s = dev_get_drvdata(dev);
182*4882a593Smuzhiyun int err;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun err = clk_prepare_enable(i2s->clk_i2s);
185*4882a593Smuzhiyun if (err) {
186*4882a593Smuzhiyun dev_err(dev, "failed to enable I2S bit clock, err: %d\n", err);
187*4882a593Smuzhiyun return err;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun regcache_cache_only(i2s->regmap, false);
191*4882a593Smuzhiyun regcache_sync(i2s->regmap);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun return 0;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
tegra210_i2s_set_data_offset(struct tegra210_i2s * i2s,unsigned int data_offset)196*4882a593Smuzhiyun static void tegra210_i2s_set_data_offset(struct tegra210_i2s *i2s,
197*4882a593Smuzhiyun unsigned int data_offset)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun /* Capture path */
200*4882a593Smuzhiyun regmap_update_bits(i2s->regmap, TEGRA210_I2S_TX_CTRL,
201*4882a593Smuzhiyun I2S_CTRL_DATA_OFFSET_MASK,
202*4882a593Smuzhiyun data_offset << I2S_DATA_SHIFT);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /* Playback path */
205*4882a593Smuzhiyun regmap_update_bits(i2s->regmap, TEGRA210_I2S_RX_CTRL,
206*4882a593Smuzhiyun I2S_CTRL_DATA_OFFSET_MASK,
207*4882a593Smuzhiyun data_offset << I2S_DATA_SHIFT);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
tegra210_i2s_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)210*4882a593Smuzhiyun static int tegra210_i2s_set_fmt(struct snd_soc_dai *dai,
211*4882a593Smuzhiyun unsigned int fmt)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_dai_get_drvdata(dai);
214*4882a593Smuzhiyun unsigned int mask, val;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun mask = I2S_CTRL_MASTER_EN_MASK;
217*4882a593Smuzhiyun switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
218*4882a593Smuzhiyun case SND_SOC_DAIFMT_CBS_CFS:
219*4882a593Smuzhiyun val = 0;
220*4882a593Smuzhiyun break;
221*4882a593Smuzhiyun case SND_SOC_DAIFMT_CBM_CFM:
222*4882a593Smuzhiyun val = I2S_CTRL_MASTER_EN;
223*4882a593Smuzhiyun break;
224*4882a593Smuzhiyun default:
225*4882a593Smuzhiyun return -EINVAL;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun mask |= I2S_CTRL_FRAME_FMT_MASK | I2S_CTRL_LRCK_POL_MASK;
229*4882a593Smuzhiyun switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
230*4882a593Smuzhiyun case SND_SOC_DAIFMT_DSP_A:
231*4882a593Smuzhiyun val |= I2S_CTRL_FRAME_FMT_FSYNC_MODE;
232*4882a593Smuzhiyun val |= I2S_CTRL_LRCK_POL_HIGH;
233*4882a593Smuzhiyun tegra210_i2s_set_data_offset(i2s, 1);
234*4882a593Smuzhiyun break;
235*4882a593Smuzhiyun case SND_SOC_DAIFMT_DSP_B:
236*4882a593Smuzhiyun val |= I2S_CTRL_FRAME_FMT_FSYNC_MODE;
237*4882a593Smuzhiyun val |= I2S_CTRL_LRCK_POL_HIGH;
238*4882a593Smuzhiyun tegra210_i2s_set_data_offset(i2s, 0);
239*4882a593Smuzhiyun break;
240*4882a593Smuzhiyun /* I2S mode has data offset of 1 */
241*4882a593Smuzhiyun case SND_SOC_DAIFMT_I2S:
242*4882a593Smuzhiyun val |= I2S_CTRL_FRAME_FMT_LRCK_MODE;
243*4882a593Smuzhiyun val |= I2S_CTRL_LRCK_POL_LOW;
244*4882a593Smuzhiyun tegra210_i2s_set_data_offset(i2s, 1);
245*4882a593Smuzhiyun break;
246*4882a593Smuzhiyun /*
247*4882a593Smuzhiyun * For RJ mode data offset is dependent on the sample size
248*4882a593Smuzhiyun * and the bclk ratio, and so is set when hw_params is called.
249*4882a593Smuzhiyun */
250*4882a593Smuzhiyun case SND_SOC_DAIFMT_RIGHT_J:
251*4882a593Smuzhiyun val |= I2S_CTRL_FRAME_FMT_LRCK_MODE;
252*4882a593Smuzhiyun val |= I2S_CTRL_LRCK_POL_HIGH;
253*4882a593Smuzhiyun break;
254*4882a593Smuzhiyun case SND_SOC_DAIFMT_LEFT_J:
255*4882a593Smuzhiyun val |= I2S_CTRL_FRAME_FMT_LRCK_MODE;
256*4882a593Smuzhiyun val |= I2S_CTRL_LRCK_POL_HIGH;
257*4882a593Smuzhiyun tegra210_i2s_set_data_offset(i2s, 0);
258*4882a593Smuzhiyun break;
259*4882a593Smuzhiyun default:
260*4882a593Smuzhiyun return -EINVAL;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun mask |= I2S_CTRL_EDGE_CTRL_MASK;
264*4882a593Smuzhiyun switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
265*4882a593Smuzhiyun case SND_SOC_DAIFMT_NB_NF:
266*4882a593Smuzhiyun val |= I2S_CTRL_EDGE_CTRL_POS_EDGE;
267*4882a593Smuzhiyun break;
268*4882a593Smuzhiyun case SND_SOC_DAIFMT_NB_IF:
269*4882a593Smuzhiyun val |= I2S_CTRL_EDGE_CTRL_POS_EDGE;
270*4882a593Smuzhiyun val ^= I2S_CTRL_LRCK_POL_MASK;
271*4882a593Smuzhiyun break;
272*4882a593Smuzhiyun case SND_SOC_DAIFMT_IB_NF:
273*4882a593Smuzhiyun val |= I2S_CTRL_EDGE_CTRL_NEG_EDGE;
274*4882a593Smuzhiyun break;
275*4882a593Smuzhiyun case SND_SOC_DAIFMT_IB_IF:
276*4882a593Smuzhiyun val |= I2S_CTRL_EDGE_CTRL_NEG_EDGE;
277*4882a593Smuzhiyun val ^= I2S_CTRL_LRCK_POL_MASK;
278*4882a593Smuzhiyun break;
279*4882a593Smuzhiyun default:
280*4882a593Smuzhiyun return -EINVAL;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun regmap_update_bits(i2s->regmap, TEGRA210_I2S_CTRL, mask, val);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun i2s->dai_fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun return 0;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
tegra210_i2s_set_tdm_slot(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int slot_width)290*4882a593Smuzhiyun static int tegra210_i2s_set_tdm_slot(struct snd_soc_dai *dai,
291*4882a593Smuzhiyun unsigned int tx_mask, unsigned int rx_mask,
292*4882a593Smuzhiyun int slots, int slot_width)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_dai_get_drvdata(dai);
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun /* Copy the required tx and rx mask */
297*4882a593Smuzhiyun i2s->tx_mask = (tx_mask > DEFAULT_I2S_SLOT_MASK) ?
298*4882a593Smuzhiyun DEFAULT_I2S_SLOT_MASK : tx_mask;
299*4882a593Smuzhiyun i2s->rx_mask = (rx_mask > DEFAULT_I2S_SLOT_MASK) ?
300*4882a593Smuzhiyun DEFAULT_I2S_SLOT_MASK : rx_mask;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun return 0;
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun
tegra210_i2s_get_loopback(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)305*4882a593Smuzhiyun static int tegra210_i2s_get_loopback(struct snd_kcontrol *kcontrol,
306*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
309*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun ucontrol->value.integer.value[0] = i2s->loopback;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun return 0;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
tegra210_i2s_put_loopback(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)316*4882a593Smuzhiyun static int tegra210_i2s_put_loopback(struct snd_kcontrol *kcontrol,
317*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
320*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
321*4882a593Smuzhiyun int value = ucontrol->value.integer.value[0];
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun if (value == i2s->loopback)
324*4882a593Smuzhiyun return 0;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun i2s->loopback = value;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun regmap_update_bits(i2s->regmap, TEGRA210_I2S_CTRL, I2S_CTRL_LPBK_MASK,
329*4882a593Smuzhiyun i2s->loopback << I2S_CTRL_LPBK_SHIFT);
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun return 1;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
tegra210_i2s_get_fsync_width(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)334*4882a593Smuzhiyun static int tegra210_i2s_get_fsync_width(struct snd_kcontrol *kcontrol,
335*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
338*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun ucontrol->value.integer.value[0] = i2s->fsync_width;
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun return 0;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun
tegra210_i2s_put_fsync_width(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)345*4882a593Smuzhiyun static int tegra210_i2s_put_fsync_width(struct snd_kcontrol *kcontrol,
346*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
349*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
350*4882a593Smuzhiyun int value = ucontrol->value.integer.value[0];
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun if (value == i2s->fsync_width)
353*4882a593Smuzhiyun return 0;
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun i2s->fsync_width = value;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /*
358*4882a593Smuzhiyun * Frame sync width is used only for FSYNC modes and not
359*4882a593Smuzhiyun * applicable for LRCK modes. Reset value for this field is "0",
360*4882a593Smuzhiyun * which means the width is one bit clock wide.
361*4882a593Smuzhiyun * The width requirement may depend on the codec and in such
362*4882a593Smuzhiyun * cases mixer control is used to update custom values. A value
363*4882a593Smuzhiyun * of "N" here means, width is "N + 1" bit clock wide.
364*4882a593Smuzhiyun */
365*4882a593Smuzhiyun regmap_update_bits(i2s->regmap, TEGRA210_I2S_CTRL,
366*4882a593Smuzhiyun I2S_CTRL_FSYNC_WIDTH_MASK,
367*4882a593Smuzhiyun i2s->fsync_width << I2S_FSYNC_WIDTH_SHIFT);
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun return 1;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
tegra210_i2s_cget_stereo_to_mono(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)372*4882a593Smuzhiyun static int tegra210_i2s_cget_stereo_to_mono(struct snd_kcontrol *kcontrol,
373*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
376*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = i2s->stereo_to_mono[I2S_TX_PATH];
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun return 0;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun
tegra210_i2s_cput_stereo_to_mono(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)383*4882a593Smuzhiyun static int tegra210_i2s_cput_stereo_to_mono(struct snd_kcontrol *kcontrol,
384*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
387*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
388*4882a593Smuzhiyun unsigned int value = ucontrol->value.enumerated.item[0];
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun if (value == i2s->stereo_to_mono[I2S_TX_PATH])
391*4882a593Smuzhiyun return 0;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun i2s->stereo_to_mono[I2S_TX_PATH] = value;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun return 1;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun
tegra210_i2s_cget_mono_to_stereo(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)398*4882a593Smuzhiyun static int tegra210_i2s_cget_mono_to_stereo(struct snd_kcontrol *kcontrol,
399*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
402*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = i2s->mono_to_stereo[I2S_TX_PATH];
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun return 0;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
tegra210_i2s_cput_mono_to_stereo(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)409*4882a593Smuzhiyun static int tegra210_i2s_cput_mono_to_stereo(struct snd_kcontrol *kcontrol,
410*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
413*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
414*4882a593Smuzhiyun unsigned int value = ucontrol->value.enumerated.item[0];
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun if (value == i2s->mono_to_stereo[I2S_TX_PATH])
417*4882a593Smuzhiyun return 0;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun i2s->mono_to_stereo[I2S_TX_PATH] = value;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun return 1;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun
tegra210_i2s_pget_stereo_to_mono(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)424*4882a593Smuzhiyun static int tegra210_i2s_pget_stereo_to_mono(struct snd_kcontrol *kcontrol,
425*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
428*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = i2s->stereo_to_mono[I2S_RX_PATH];
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun return 0;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun
tegra210_i2s_pput_stereo_to_mono(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)435*4882a593Smuzhiyun static int tegra210_i2s_pput_stereo_to_mono(struct snd_kcontrol *kcontrol,
436*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
439*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
440*4882a593Smuzhiyun unsigned int value = ucontrol->value.enumerated.item[0];
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun if (value == i2s->stereo_to_mono[I2S_RX_PATH])
443*4882a593Smuzhiyun return 0;
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun i2s->stereo_to_mono[I2S_RX_PATH] = value;
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun return 1;
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun
tegra210_i2s_pget_mono_to_stereo(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)450*4882a593Smuzhiyun static int tegra210_i2s_pget_mono_to_stereo(struct snd_kcontrol *kcontrol,
451*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
454*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = i2s->mono_to_stereo[I2S_RX_PATH];
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun return 0;
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun
tegra210_i2s_pput_mono_to_stereo(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)461*4882a593Smuzhiyun static int tegra210_i2s_pput_mono_to_stereo(struct snd_kcontrol *kcontrol,
462*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
465*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
466*4882a593Smuzhiyun unsigned int value = ucontrol->value.enumerated.item[0];
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun if (value == i2s->mono_to_stereo[I2S_RX_PATH])
469*4882a593Smuzhiyun return 0;
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun i2s->mono_to_stereo[I2S_RX_PATH] = value;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun return 1;
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun
tegra210_i2s_pget_fifo_th(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)476*4882a593Smuzhiyun static int tegra210_i2s_pget_fifo_th(struct snd_kcontrol *kcontrol,
477*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
480*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun ucontrol->value.integer.value[0] = i2s->rx_fifo_th;
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun return 0;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun
tegra210_i2s_pput_fifo_th(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)487*4882a593Smuzhiyun static int tegra210_i2s_pput_fifo_th(struct snd_kcontrol *kcontrol,
488*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
489*4882a593Smuzhiyun {
490*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
491*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
492*4882a593Smuzhiyun int value = ucontrol->value.integer.value[0];
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun if (value == i2s->rx_fifo_th)
495*4882a593Smuzhiyun return 0;
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun i2s->rx_fifo_th = value;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun return 1;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun
tegra210_i2s_get_bclk_ratio(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)502*4882a593Smuzhiyun static int tegra210_i2s_get_bclk_ratio(struct snd_kcontrol *kcontrol,
503*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
506*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun ucontrol->value.integer.value[0] = i2s->bclk_ratio;
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun return 0;
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun
tegra210_i2s_put_bclk_ratio(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)513*4882a593Smuzhiyun static int tegra210_i2s_put_bclk_ratio(struct snd_kcontrol *kcontrol,
514*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun struct snd_soc_component *compnt = snd_soc_kcontrol_component(kcontrol);
517*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_component_get_drvdata(compnt);
518*4882a593Smuzhiyun int value = ucontrol->value.integer.value[0];
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun if (value == i2s->bclk_ratio)
521*4882a593Smuzhiyun return 0;
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun i2s->bclk_ratio = value;
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun return 1;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun
tegra210_i2s_set_dai_bclk_ratio(struct snd_soc_dai * dai,unsigned int ratio)528*4882a593Smuzhiyun static int tegra210_i2s_set_dai_bclk_ratio(struct snd_soc_dai *dai,
529*4882a593Smuzhiyun unsigned int ratio)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_dai_get_drvdata(dai);
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun i2s->bclk_ratio = ratio;
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun return 0;
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
tegra210_i2s_set_timing_params(struct device * dev,unsigned int sample_size,unsigned int srate,unsigned int channels)538*4882a593Smuzhiyun static int tegra210_i2s_set_timing_params(struct device *dev,
539*4882a593Smuzhiyun unsigned int sample_size,
540*4882a593Smuzhiyun unsigned int srate,
541*4882a593Smuzhiyun unsigned int channels)
542*4882a593Smuzhiyun {
543*4882a593Smuzhiyun struct tegra210_i2s *i2s = dev_get_drvdata(dev);
544*4882a593Smuzhiyun unsigned int val, bit_count, bclk_rate, num_bclk = sample_size;
545*4882a593Smuzhiyun int err;
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun if (i2s->bclk_ratio)
548*4882a593Smuzhiyun num_bclk *= i2s->bclk_ratio;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun if (i2s->dai_fmt == SND_SOC_DAIFMT_RIGHT_J)
551*4882a593Smuzhiyun tegra210_i2s_set_data_offset(i2s, num_bclk - sample_size);
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun /* I2S bit clock rate */
554*4882a593Smuzhiyun bclk_rate = srate * channels * num_bclk;
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun err = tegra210_i2s_set_clock_rate(dev, bclk_rate);
557*4882a593Smuzhiyun if (err) {
558*4882a593Smuzhiyun dev_err(dev, "can't set I2S bit clock rate %u, err: %d\n",
559*4882a593Smuzhiyun bclk_rate, err);
560*4882a593Smuzhiyun return err;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun regmap_read(i2s->regmap, TEGRA210_I2S_CTRL, &val);
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun /*
566*4882a593Smuzhiyun * For LRCK mode, channel bit count depends on number of bit clocks
567*4882a593Smuzhiyun * on the left channel, where as for FSYNC mode bit count depends on
568*4882a593Smuzhiyun * the number of bit clocks in both left and right channels for DSP
569*4882a593Smuzhiyun * mode or the number of bit clocks in one TDM frame.
570*4882a593Smuzhiyun *
571*4882a593Smuzhiyun */
572*4882a593Smuzhiyun switch (val & I2S_CTRL_FRAME_FMT_MASK) {
573*4882a593Smuzhiyun case I2S_CTRL_FRAME_FMT_LRCK_MODE:
574*4882a593Smuzhiyun bit_count = (bclk_rate / (srate * 2)) - 1;
575*4882a593Smuzhiyun break;
576*4882a593Smuzhiyun case I2S_CTRL_FRAME_FMT_FSYNC_MODE:
577*4882a593Smuzhiyun bit_count = (bclk_rate / srate) - 1;
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun tegra210_i2s_set_slot_ctrl(i2s->regmap, channels,
580*4882a593Smuzhiyun i2s->tx_mask, i2s->rx_mask);
581*4882a593Smuzhiyun break;
582*4882a593Smuzhiyun default:
583*4882a593Smuzhiyun dev_err(dev, "invalid I2S frame format\n");
584*4882a593Smuzhiyun return -EINVAL;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun if (bit_count > I2S_TIMING_CH_BIT_CNT_MASK) {
588*4882a593Smuzhiyun dev_err(dev, "invalid I2S channel bit count %u\n", bit_count);
589*4882a593Smuzhiyun return -EINVAL;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun regmap_write(i2s->regmap, TEGRA210_I2S_TIMING,
593*4882a593Smuzhiyun bit_count << I2S_TIMING_CH_BIT_CNT_SHIFT);
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun return 0;
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun
tegra210_i2s_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)598*4882a593Smuzhiyun static int tegra210_i2s_hw_params(struct snd_pcm_substream *substream,
599*4882a593Smuzhiyun struct snd_pcm_hw_params *params,
600*4882a593Smuzhiyun struct snd_soc_dai *dai)
601*4882a593Smuzhiyun {
602*4882a593Smuzhiyun struct device *dev = dai->dev;
603*4882a593Smuzhiyun struct tegra210_i2s *i2s = snd_soc_dai_get_drvdata(dai);
604*4882a593Smuzhiyun unsigned int sample_size, channels, srate, val, reg, path;
605*4882a593Smuzhiyun struct tegra_cif_conf cif_conf;
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun memset(&cif_conf, 0, sizeof(struct tegra_cif_conf));
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun channels = params_channels(params);
610*4882a593Smuzhiyun if (channels < 1) {
611*4882a593Smuzhiyun dev_err(dev, "invalid I2S %d channel configuration\n",
612*4882a593Smuzhiyun channels);
613*4882a593Smuzhiyun return -EINVAL;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun cif_conf.audio_ch = channels;
617*4882a593Smuzhiyun cif_conf.client_ch = channels;
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun switch (params_format(params)) {
620*4882a593Smuzhiyun case SNDRV_PCM_FORMAT_S8:
621*4882a593Smuzhiyun val = I2S_BITS_8;
622*4882a593Smuzhiyun sample_size = 8;
623*4882a593Smuzhiyun cif_conf.audio_bits = TEGRA_ACIF_BITS_8;
624*4882a593Smuzhiyun cif_conf.client_bits = TEGRA_ACIF_BITS_8;
625*4882a593Smuzhiyun break;
626*4882a593Smuzhiyun case SNDRV_PCM_FORMAT_S16_LE:
627*4882a593Smuzhiyun val = I2S_BITS_16;
628*4882a593Smuzhiyun sample_size = 16;
629*4882a593Smuzhiyun cif_conf.audio_bits = TEGRA_ACIF_BITS_16;
630*4882a593Smuzhiyun cif_conf.client_bits = TEGRA_ACIF_BITS_16;
631*4882a593Smuzhiyun break;
632*4882a593Smuzhiyun case SNDRV_PCM_FORMAT_S32_LE:
633*4882a593Smuzhiyun val = I2S_BITS_32;
634*4882a593Smuzhiyun sample_size = 32;
635*4882a593Smuzhiyun cif_conf.audio_bits = TEGRA_ACIF_BITS_32;
636*4882a593Smuzhiyun cif_conf.client_bits = TEGRA_ACIF_BITS_32;
637*4882a593Smuzhiyun break;
638*4882a593Smuzhiyun default:
639*4882a593Smuzhiyun dev_err(dev, "unsupported format!\n");
640*4882a593Smuzhiyun return -EOPNOTSUPP;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun /* Program sample size */
644*4882a593Smuzhiyun regmap_update_bits(i2s->regmap, TEGRA210_I2S_CTRL,
645*4882a593Smuzhiyun I2S_CTRL_BIT_SIZE_MASK, val);
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun srate = params_rate(params);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun /* For playback I2S RX-CIF and for capture TX-CIF is used */
650*4882a593Smuzhiyun if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
651*4882a593Smuzhiyun path = I2S_RX_PATH;
652*4882a593Smuzhiyun else
653*4882a593Smuzhiyun path = I2S_TX_PATH;
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
656*4882a593Smuzhiyun unsigned int max_th;
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun /* FIFO threshold in terms of frames */
659*4882a593Smuzhiyun max_th = (I2S_RX_FIFO_DEPTH / cif_conf.audio_ch) - 1;
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun if (i2s->rx_fifo_th > max_th)
662*4882a593Smuzhiyun i2s->rx_fifo_th = max_th;
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun cif_conf.threshold = i2s->rx_fifo_th;
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun reg = TEGRA210_I2S_RX_CIF_CTRL;
667*4882a593Smuzhiyun } else {
668*4882a593Smuzhiyun reg = TEGRA210_I2S_TX_CIF_CTRL;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun cif_conf.mono_conv = i2s->mono_to_stereo[path];
672*4882a593Smuzhiyun cif_conf.stereo_conv = i2s->stereo_to_mono[path];
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun tegra_set_cif(i2s->regmap, reg, &cif_conf);
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun return tegra210_i2s_set_timing_params(dev, sample_size, srate,
677*4882a593Smuzhiyun cif_conf.client_ch);
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun static const struct snd_soc_dai_ops tegra210_i2s_dai_ops = {
681*4882a593Smuzhiyun .set_fmt = tegra210_i2s_set_fmt,
682*4882a593Smuzhiyun .hw_params = tegra210_i2s_hw_params,
683*4882a593Smuzhiyun .set_bclk_ratio = tegra210_i2s_set_dai_bclk_ratio,
684*4882a593Smuzhiyun .set_tdm_slot = tegra210_i2s_set_tdm_slot,
685*4882a593Smuzhiyun };
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun static struct snd_soc_dai_driver tegra210_i2s_dais[] = {
688*4882a593Smuzhiyun {
689*4882a593Smuzhiyun .name = "I2S-CIF",
690*4882a593Smuzhiyun .playback = {
691*4882a593Smuzhiyun .stream_name = "CIF-Playback",
692*4882a593Smuzhiyun .channels_min = 1,
693*4882a593Smuzhiyun .channels_max = 16,
694*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_8000_192000,
695*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S8 |
696*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_S16_LE |
697*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_S32_LE,
698*4882a593Smuzhiyun },
699*4882a593Smuzhiyun .capture = {
700*4882a593Smuzhiyun .stream_name = "CIF-Capture",
701*4882a593Smuzhiyun .channels_min = 1,
702*4882a593Smuzhiyun .channels_max = 16,
703*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_8000_192000,
704*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S8 |
705*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_S16_LE |
706*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_S32_LE,
707*4882a593Smuzhiyun },
708*4882a593Smuzhiyun },
709*4882a593Smuzhiyun {
710*4882a593Smuzhiyun .name = "I2S-DAP",
711*4882a593Smuzhiyun .playback = {
712*4882a593Smuzhiyun .stream_name = "DAP-Playback",
713*4882a593Smuzhiyun .channels_min = 1,
714*4882a593Smuzhiyun .channels_max = 16,
715*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_8000_192000,
716*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S8 |
717*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_S16_LE |
718*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_S32_LE,
719*4882a593Smuzhiyun },
720*4882a593Smuzhiyun .capture = {
721*4882a593Smuzhiyun .stream_name = "DAP-Capture",
722*4882a593Smuzhiyun .channels_min = 1,
723*4882a593Smuzhiyun .channels_max = 16,
724*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_8000_192000,
725*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S8 |
726*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_S16_LE |
727*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_S32_LE,
728*4882a593Smuzhiyun },
729*4882a593Smuzhiyun .ops = &tegra210_i2s_dai_ops,
730*4882a593Smuzhiyun .symmetric_rates = 1,
731*4882a593Smuzhiyun },
732*4882a593Smuzhiyun };
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun static const char * const tegra210_i2s_stereo_conv_text[] = {
735*4882a593Smuzhiyun "CH0", "CH1", "AVG",
736*4882a593Smuzhiyun };
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun static const char * const tegra210_i2s_mono_conv_text[] = {
739*4882a593Smuzhiyun "Zero", "Copy",
740*4882a593Smuzhiyun };
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun static const struct soc_enum tegra210_i2s_mono_conv_enum =
743*4882a593Smuzhiyun SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_i2s_mono_conv_text),
744*4882a593Smuzhiyun tegra210_i2s_mono_conv_text);
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun static const struct soc_enum tegra210_i2s_stereo_conv_enum =
747*4882a593Smuzhiyun SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(tegra210_i2s_stereo_conv_text),
748*4882a593Smuzhiyun tegra210_i2s_stereo_conv_text);
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun static const struct snd_kcontrol_new tegra210_i2s_controls[] = {
751*4882a593Smuzhiyun SOC_SINGLE_EXT("Loopback", 0, 0, 1, 0, tegra210_i2s_get_loopback,
752*4882a593Smuzhiyun tegra210_i2s_put_loopback),
753*4882a593Smuzhiyun SOC_SINGLE_EXT("FSYNC Width", 0, 0, 255, 0,
754*4882a593Smuzhiyun tegra210_i2s_get_fsync_width,
755*4882a593Smuzhiyun tegra210_i2s_put_fsync_width),
756*4882a593Smuzhiyun SOC_ENUM_EXT("Capture Stereo To Mono", tegra210_i2s_stereo_conv_enum,
757*4882a593Smuzhiyun tegra210_i2s_cget_stereo_to_mono,
758*4882a593Smuzhiyun tegra210_i2s_cput_stereo_to_mono),
759*4882a593Smuzhiyun SOC_ENUM_EXT("Capture Mono To Stereo", tegra210_i2s_mono_conv_enum,
760*4882a593Smuzhiyun tegra210_i2s_cget_mono_to_stereo,
761*4882a593Smuzhiyun tegra210_i2s_cput_mono_to_stereo),
762*4882a593Smuzhiyun SOC_ENUM_EXT("Playback Stereo To Mono", tegra210_i2s_stereo_conv_enum,
763*4882a593Smuzhiyun tegra210_i2s_pget_mono_to_stereo,
764*4882a593Smuzhiyun tegra210_i2s_pput_mono_to_stereo),
765*4882a593Smuzhiyun SOC_ENUM_EXT("Playback Mono To Stereo", tegra210_i2s_mono_conv_enum,
766*4882a593Smuzhiyun tegra210_i2s_pget_stereo_to_mono,
767*4882a593Smuzhiyun tegra210_i2s_pput_stereo_to_mono),
768*4882a593Smuzhiyun SOC_SINGLE_EXT("Playback FIFO Threshold", 0, 0, I2S_RX_FIFO_DEPTH - 1,
769*4882a593Smuzhiyun 0, tegra210_i2s_pget_fifo_th, tegra210_i2s_pput_fifo_th),
770*4882a593Smuzhiyun SOC_SINGLE_EXT("BCLK Ratio", 0, 0, INT_MAX, 0,
771*4882a593Smuzhiyun tegra210_i2s_get_bclk_ratio,
772*4882a593Smuzhiyun tegra210_i2s_put_bclk_ratio),
773*4882a593Smuzhiyun };
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun static const struct snd_soc_dapm_widget tegra210_i2s_widgets[] = {
776*4882a593Smuzhiyun SND_SOC_DAPM_AIF_IN_E("RX", NULL, 0, TEGRA210_I2S_RX_ENABLE,
777*4882a593Smuzhiyun 0, 0, tegra210_i2s_init, SND_SOC_DAPM_PRE_PMU),
778*4882a593Smuzhiyun SND_SOC_DAPM_AIF_OUT_E("TX", NULL, 0, TEGRA210_I2S_TX_ENABLE,
779*4882a593Smuzhiyun 0, 0, tegra210_i2s_init, SND_SOC_DAPM_PRE_PMU),
780*4882a593Smuzhiyun SND_SOC_DAPM_MIC("MIC", NULL),
781*4882a593Smuzhiyun SND_SOC_DAPM_SPK("SPK", NULL),
782*4882a593Smuzhiyun };
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun static const struct snd_soc_dapm_route tegra210_i2s_routes[] = {
785*4882a593Smuzhiyun /* Playback route from XBAR */
786*4882a593Smuzhiyun { "XBAR-Playback", NULL, "XBAR-TX" },
787*4882a593Smuzhiyun { "CIF-Playback", NULL, "XBAR-Playback" },
788*4882a593Smuzhiyun { "RX", NULL, "CIF-Playback" },
789*4882a593Smuzhiyun { "DAP-Playback", NULL, "RX" },
790*4882a593Smuzhiyun { "SPK", NULL, "DAP-Playback" },
791*4882a593Smuzhiyun /* Capture route to XBAR */
792*4882a593Smuzhiyun { "XBAR-RX", NULL, "XBAR-Capture" },
793*4882a593Smuzhiyun { "XBAR-Capture", NULL, "CIF-Capture" },
794*4882a593Smuzhiyun { "CIF-Capture", NULL, "TX" },
795*4882a593Smuzhiyun { "TX", NULL, "DAP-Capture" },
796*4882a593Smuzhiyun { "DAP-Capture", NULL, "MIC" },
797*4882a593Smuzhiyun };
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun static const struct snd_soc_component_driver tegra210_i2s_cmpnt = {
800*4882a593Smuzhiyun .dapm_widgets = tegra210_i2s_widgets,
801*4882a593Smuzhiyun .num_dapm_widgets = ARRAY_SIZE(tegra210_i2s_widgets),
802*4882a593Smuzhiyun .dapm_routes = tegra210_i2s_routes,
803*4882a593Smuzhiyun .num_dapm_routes = ARRAY_SIZE(tegra210_i2s_routes),
804*4882a593Smuzhiyun .controls = tegra210_i2s_controls,
805*4882a593Smuzhiyun .num_controls = ARRAY_SIZE(tegra210_i2s_controls),
806*4882a593Smuzhiyun .non_legacy_dai_naming = 1,
807*4882a593Smuzhiyun };
808*4882a593Smuzhiyun
tegra210_i2s_wr_reg(struct device * dev,unsigned int reg)809*4882a593Smuzhiyun static bool tegra210_i2s_wr_reg(struct device *dev, unsigned int reg)
810*4882a593Smuzhiyun {
811*4882a593Smuzhiyun switch (reg) {
812*4882a593Smuzhiyun case TEGRA210_I2S_RX_ENABLE ... TEGRA210_I2S_RX_SOFT_RESET:
813*4882a593Smuzhiyun case TEGRA210_I2S_RX_INT_MASK ... TEGRA210_I2S_RX_CLK_TRIM:
814*4882a593Smuzhiyun case TEGRA210_I2S_TX_ENABLE ... TEGRA210_I2S_TX_SOFT_RESET:
815*4882a593Smuzhiyun case TEGRA210_I2S_TX_INT_MASK ... TEGRA210_I2S_TX_CLK_TRIM:
816*4882a593Smuzhiyun case TEGRA210_I2S_ENABLE ... TEGRA210_I2S_CG:
817*4882a593Smuzhiyun case TEGRA210_I2S_CTRL ... TEGRA210_I2S_CYA:
818*4882a593Smuzhiyun return true;
819*4882a593Smuzhiyun default:
820*4882a593Smuzhiyun return false;
821*4882a593Smuzhiyun };
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun
tegra210_i2s_rd_reg(struct device * dev,unsigned int reg)824*4882a593Smuzhiyun static bool tegra210_i2s_rd_reg(struct device *dev, unsigned int reg)
825*4882a593Smuzhiyun {
826*4882a593Smuzhiyun if (tegra210_i2s_wr_reg(dev, reg))
827*4882a593Smuzhiyun return true;
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun switch (reg) {
830*4882a593Smuzhiyun case TEGRA210_I2S_RX_STATUS:
831*4882a593Smuzhiyun case TEGRA210_I2S_RX_INT_STATUS:
832*4882a593Smuzhiyun case TEGRA210_I2S_RX_CIF_FIFO_STATUS:
833*4882a593Smuzhiyun case TEGRA210_I2S_TX_STATUS:
834*4882a593Smuzhiyun case TEGRA210_I2S_TX_INT_STATUS:
835*4882a593Smuzhiyun case TEGRA210_I2S_TX_CIF_FIFO_STATUS:
836*4882a593Smuzhiyun case TEGRA210_I2S_STATUS:
837*4882a593Smuzhiyun case TEGRA210_I2S_INT_STATUS:
838*4882a593Smuzhiyun return true;
839*4882a593Smuzhiyun default:
840*4882a593Smuzhiyun return false;
841*4882a593Smuzhiyun };
842*4882a593Smuzhiyun }
843*4882a593Smuzhiyun
tegra210_i2s_volatile_reg(struct device * dev,unsigned int reg)844*4882a593Smuzhiyun static bool tegra210_i2s_volatile_reg(struct device *dev, unsigned int reg)
845*4882a593Smuzhiyun {
846*4882a593Smuzhiyun switch (reg) {
847*4882a593Smuzhiyun case TEGRA210_I2S_RX_STATUS:
848*4882a593Smuzhiyun case TEGRA210_I2S_RX_INT_STATUS:
849*4882a593Smuzhiyun case TEGRA210_I2S_RX_CIF_FIFO_STATUS:
850*4882a593Smuzhiyun case TEGRA210_I2S_TX_STATUS:
851*4882a593Smuzhiyun case TEGRA210_I2S_TX_INT_STATUS:
852*4882a593Smuzhiyun case TEGRA210_I2S_TX_CIF_FIFO_STATUS:
853*4882a593Smuzhiyun case TEGRA210_I2S_STATUS:
854*4882a593Smuzhiyun case TEGRA210_I2S_INT_STATUS:
855*4882a593Smuzhiyun case TEGRA210_I2S_RX_SOFT_RESET:
856*4882a593Smuzhiyun case TEGRA210_I2S_TX_SOFT_RESET:
857*4882a593Smuzhiyun return true;
858*4882a593Smuzhiyun default:
859*4882a593Smuzhiyun return false;
860*4882a593Smuzhiyun };
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun static const struct regmap_config tegra210_i2s_regmap_config = {
864*4882a593Smuzhiyun .reg_bits = 32,
865*4882a593Smuzhiyun .reg_stride = 4,
866*4882a593Smuzhiyun .val_bits = 32,
867*4882a593Smuzhiyun .max_register = TEGRA210_I2S_CYA,
868*4882a593Smuzhiyun .writeable_reg = tegra210_i2s_wr_reg,
869*4882a593Smuzhiyun .readable_reg = tegra210_i2s_rd_reg,
870*4882a593Smuzhiyun .volatile_reg = tegra210_i2s_volatile_reg,
871*4882a593Smuzhiyun .reg_defaults = tegra210_i2s_reg_defaults,
872*4882a593Smuzhiyun .num_reg_defaults = ARRAY_SIZE(tegra210_i2s_reg_defaults),
873*4882a593Smuzhiyun .cache_type = REGCACHE_FLAT,
874*4882a593Smuzhiyun };
875*4882a593Smuzhiyun
tegra210_i2s_probe(struct platform_device * pdev)876*4882a593Smuzhiyun static int tegra210_i2s_probe(struct platform_device *pdev)
877*4882a593Smuzhiyun {
878*4882a593Smuzhiyun struct device *dev = &pdev->dev;
879*4882a593Smuzhiyun struct tegra210_i2s *i2s;
880*4882a593Smuzhiyun void __iomem *regs;
881*4882a593Smuzhiyun int err;
882*4882a593Smuzhiyun
883*4882a593Smuzhiyun i2s = devm_kzalloc(dev, sizeof(*i2s), GFP_KERNEL);
884*4882a593Smuzhiyun if (!i2s)
885*4882a593Smuzhiyun return -ENOMEM;
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun i2s->rx_fifo_th = DEFAULT_I2S_RX_FIFO_THRESHOLD;
888*4882a593Smuzhiyun i2s->tx_mask = DEFAULT_I2S_SLOT_MASK;
889*4882a593Smuzhiyun i2s->rx_mask = DEFAULT_I2S_SLOT_MASK;
890*4882a593Smuzhiyun i2s->loopback = false;
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun dev_set_drvdata(dev, i2s);
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun i2s->clk_i2s = devm_clk_get(dev, "i2s");
895*4882a593Smuzhiyun if (IS_ERR(i2s->clk_i2s)) {
896*4882a593Smuzhiyun dev_err(dev, "can't retrieve I2S bit clock\n");
897*4882a593Smuzhiyun return PTR_ERR(i2s->clk_i2s);
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun /*
901*4882a593Smuzhiyun * Not an error, as this clock is needed only when some other I/O
902*4882a593Smuzhiyun * requires input clock from current I2S instance, which is
903*4882a593Smuzhiyun * configurable from DT.
904*4882a593Smuzhiyun */
905*4882a593Smuzhiyun i2s->clk_sync_input = devm_clk_get(dev, "sync_input");
906*4882a593Smuzhiyun if (IS_ERR(i2s->clk_sync_input))
907*4882a593Smuzhiyun dev_dbg(dev, "can't retrieve I2S sync input clock\n");
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun regs = devm_platform_ioremap_resource(pdev, 0);
910*4882a593Smuzhiyun if (IS_ERR(regs))
911*4882a593Smuzhiyun return PTR_ERR(regs);
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun i2s->regmap = devm_regmap_init_mmio(dev, regs,
914*4882a593Smuzhiyun &tegra210_i2s_regmap_config);
915*4882a593Smuzhiyun if (IS_ERR(i2s->regmap)) {
916*4882a593Smuzhiyun dev_err(dev, "regmap init failed\n");
917*4882a593Smuzhiyun return PTR_ERR(i2s->regmap);
918*4882a593Smuzhiyun }
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun regcache_cache_only(i2s->regmap, true);
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun err = devm_snd_soc_register_component(dev, &tegra210_i2s_cmpnt,
923*4882a593Smuzhiyun tegra210_i2s_dais,
924*4882a593Smuzhiyun ARRAY_SIZE(tegra210_i2s_dais));
925*4882a593Smuzhiyun if (err) {
926*4882a593Smuzhiyun dev_err(dev, "can't register I2S component, err: %d\n", err);
927*4882a593Smuzhiyun return err;
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun pm_runtime_enable(dev);
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun return 0;
933*4882a593Smuzhiyun }
934*4882a593Smuzhiyun
tegra210_i2s_remove(struct platform_device * pdev)935*4882a593Smuzhiyun static int tegra210_i2s_remove(struct platform_device *pdev)
936*4882a593Smuzhiyun {
937*4882a593Smuzhiyun pm_runtime_disable(&pdev->dev);
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun return 0;
940*4882a593Smuzhiyun }
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun static const struct dev_pm_ops tegra210_i2s_pm_ops = {
943*4882a593Smuzhiyun SET_RUNTIME_PM_OPS(tegra210_i2s_runtime_suspend,
944*4882a593Smuzhiyun tegra210_i2s_runtime_resume, NULL)
945*4882a593Smuzhiyun SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
946*4882a593Smuzhiyun pm_runtime_force_resume)
947*4882a593Smuzhiyun };
948*4882a593Smuzhiyun
949*4882a593Smuzhiyun static const struct of_device_id tegra210_i2s_of_match[] = {
950*4882a593Smuzhiyun { .compatible = "nvidia,tegra210-i2s" },
951*4882a593Smuzhiyun {},
952*4882a593Smuzhiyun };
953*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, tegra210_i2s_of_match);
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun static struct platform_driver tegra210_i2s_driver = {
956*4882a593Smuzhiyun .driver = {
957*4882a593Smuzhiyun .name = "tegra210-i2s",
958*4882a593Smuzhiyun .of_match_table = tegra210_i2s_of_match,
959*4882a593Smuzhiyun .pm = &tegra210_i2s_pm_ops,
960*4882a593Smuzhiyun },
961*4882a593Smuzhiyun .probe = tegra210_i2s_probe,
962*4882a593Smuzhiyun .remove = tegra210_i2s_remove,
963*4882a593Smuzhiyun };
964*4882a593Smuzhiyun module_platform_driver(tegra210_i2s_driver)
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun MODULE_AUTHOR("Songhee Baek <sbaek@nvidia.com>");
967*4882a593Smuzhiyun MODULE_DESCRIPTION("Tegra210 ASoC I2S driver");
968*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
969