ASoC: tegra-alt: T210 AHUB drivers
[linux-3.10.git] / sound / soc / tegra-alt / tegra210_mixer_alt.c
1 /*
2  * tegra210_mixer_alt.c - Tegra210 MIXER driver
3  *
4  * Copyright (c) 2014 NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <linux/clk.h>
20 #include <linux/device.h>
21 #include <linux/io.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/regmap.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 <linux/of_device.h>
33
34 #include "tegra210_xbar_alt.h"
35 #include "tegra210_mixer_alt.h"
36
37 #define DRV_NAME "tegra210_mixer"
38
39 #define MIXER_GAIN_CONFIG_RAM_ADDR(id)  \
40         (TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_ADDR_0 +     \
41                 id*TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_ADDR_STRIDE)
42
43 static int tegra210_mixer_runtime_suspend(struct device *dev)
44 {
45         struct tegra210_mixer *mixer = dev_get_drvdata(dev);
46
47         regcache_cache_only(mixer->regmap, true);
48
49 #ifndef CONFIG_MACH_GRENADA
50         clk_disable_unprepare(mixer->clk_mixer);
51 #endif
52
53         return 0;
54 }
55
56 static int tegra210_mixer_runtime_resume(struct device *dev)
57 {
58         struct tegra210_mixer *mixer = dev_get_drvdata(dev);
59 #ifndef CONFIG_MACH_GRENADA
60         int ret;
61
62         ret = clk_prepare_enable(mixer->clk_mixer);
63         if (ret) {
64                 dev_err(dev, "clk_enable failed: %d\n", ret);
65                 return ret;
66         }
67 #endif
68
69         regcache_cache_only(mixer->regmap, false);
70
71         return 0;
72 }
73
74 static int tegra210_mixer_write_ram(struct tegra210_mixer *mixer,
75                                 unsigned int addr,
76                                 unsigned int val)
77 {
78         unsigned int reg, value, wait = 0xffff;
79
80         /* check if busy */
81         do {
82                 regmap_read(mixer->regmap,
83                                 TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_CTRL, &value);
84                 wait--;
85                 if (!wait)
86                         return -EINVAL;
87         } while (value & 0x80000000);
88         value = 0;
89
90         reg = (addr << TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_CTRL_RAM_ADDR_SHIFT) &
91                         TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_CTRL_RAM_ADDR_MASK;
92         reg |= TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_CTRL_ADDR_INIT_EN;
93         reg |= TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_CTRL_RW_WRITE;
94         reg |= TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_CTRL_SEQ_ACCESS_EN;
95
96         regmap_write(mixer->regmap,
97                 TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_CTRL, reg);
98         regmap_write(mixer->regmap,
99                 TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_DATA, val);
100
101         return 0;
102 }
103
104 static int tegra210_mixer_get_gain(struct snd_kcontrol *kcontrol,
105         struct snd_ctl_elem_value *ucontrol)
106 {
107         return 0;
108 }
109
110 static int tegra210_mixer_put_gain(struct snd_kcontrol *kcontrol,
111         struct snd_ctl_elem_value *ucontrol)
112 {
113         struct soc_mixer_control *mc =
114                 (struct soc_mixer_control *)kcontrol->private_value;
115         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
116         struct tegra210_mixer *mixer = snd_soc_codec_get_drvdata(codec);
117         unsigned int reg = mc->reg;
118         unsigned int ret;
119
120         /* set the gain and trigger config */
121         ret = tegra210_mixer_write_ram(mixer, reg + 0x09,
122                                 ucontrol->value.integer.value[0]);
123         ret |= tegra210_mixer_write_ram(mixer, reg + 0x0f,
124                                 ucontrol->value.integer.value[0]);
125
126         return ret;
127 }
128
129 static int tegra210_mixer_set_audio_cif(struct tegra210_mixer *mixer,
130                                 struct snd_pcm_hw_params *params,
131                                 unsigned int reg)
132 {
133         int channels, audio_bits;
134         struct tegra210_xbar_cif_conf cif_conf;
135
136         channels = params_channels(params);
137         if (channels < 2)
138                 return -EINVAL;
139
140         switch (params_format(params)) {
141         case SNDRV_PCM_FORMAT_S16_LE:
142                 audio_bits = TEGRA210_AUDIOCIF_BITS_16;
143                 break;
144         case SNDRV_PCM_FORMAT_S32_LE:
145                 audio_bits = TEGRA210_AUDIOCIF_BITS_32;
146                 break;
147         default:
148                 return -EINVAL;
149         }
150
151         cif_conf.threshold = 0;
152         cif_conf.audio_channels = channels;
153         cif_conf.client_channels = channels;
154         cif_conf.audio_bits = audio_bits;
155         cif_conf.client_bits = audio_bits;
156         cif_conf.expand = 0;
157         cif_conf.stereo_conv = 0;
158         cif_conf.replicate = 0;
159         cif_conf.truncate = 0;
160         cif_conf.mono_conv = 0;
161
162         mixer->soc_data->set_audio_cif(mixer->regmap, reg, &cif_conf);
163         return 0;
164 }
165
166
167 static int tegra210_mixer_in_hw_params(struct snd_pcm_substream *substream,
168                                  struct snd_pcm_hw_params *params,
169                                  struct snd_soc_dai *dai)
170 {
171         struct tegra210_mixer *mixer = snd_soc_dai_get_drvdata(dai);
172         int ret, i;
173
174         ret = tegra210_mixer_set_audio_cif(mixer, params,
175                                 TEGRA210_MIXER_AXBAR_RX1_CIF_CTRL +
176                                 (dai->id * TEGRA210_MIXER_AXBAR_RX_STRIDE));
177
178         /* write the gain config poly coefficients */
179         for (i = 0; i < 14; i++) {
180                 tegra210_mixer_write_ram (mixer,
181                         MIXER_GAIN_CONFIG_RAM_ADDR(dai->id) + i,
182                         mixer->gain_coeff[i]);
183         }
184
185         /* trigger the polynomial configuration */
186         tegra210_mixer_write_ram (mixer,
187                 MIXER_GAIN_CONFIG_RAM_ADDR(dai->id) + 0xf,
188                 0x01);
189
190         return ret;
191 }
192
193 static int tegra210_mixer_out_hw_params(struct snd_pcm_substream *substream,
194                                  struct snd_pcm_hw_params *params,
195                                  struct snd_soc_dai *dai)
196 {
197         struct tegra210_mixer *mixer = snd_soc_dai_get_drvdata(dai);
198         int ret;
199
200         ret = tegra210_mixer_set_audio_cif(mixer, params,
201                                 TEGRA210_MIXER_AXBAR_TX1_CIF_CTRL +
202                                 ((dai->id-10) * TEGRA210_MIXER_AXBAR_TX_STRIDE));
203
204         return ret;
205 }
206
207 static int tegra210_mixer_codec_probe(struct snd_soc_codec *codec)
208 {
209         struct tegra210_mixer *mixer = snd_soc_codec_get_drvdata(codec);
210         int ret;
211
212         codec->control_data = mixer->regmap;
213         ret = snd_soc_codec_set_cache_io(codec, 32, 32, SND_SOC_REGMAP);
214         if (ret != 0) {
215                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
216                 return ret;
217         }
218
219         return 0;
220 }
221
222 static struct snd_soc_dai_ops tegra210_mixer_out_dai_ops = {
223         .hw_params      = tegra210_mixer_out_hw_params,
224 };
225
226 static struct snd_soc_dai_ops tegra210_mixer_in_dai_ops = {
227         .hw_params      = tegra210_mixer_in_hw_params,
228 };
229
230 #define IN_DAI(sname, id, dai_ops)                                              \
231         {                                                       \
232                 .name = #sname #id,                                     \
233                 .playback = {                                   \
234                         .stream_name = #sname #id " Receive",   \
235                         .channels_min = 1,                      \
236                         .channels_max = 2,              \
237                         .rates = SNDRV_PCM_RATE_8000_96000, \
238                         .formats = SNDRV_PCM_FMTBIT_S16_LE, \
239                 },                                              \
240                 .ops = dai_ops,         \
241         }
242
243 #define OUT_DAI(sname, id, dai_ops)                                     \
244         {                                                       \
245                 .name = #sname #id,                                     \
246                 .capture = {                                    \
247                         .stream_name = #sname #id " Transmit",  \
248                         .channels_min = 1,                      \
249                         .channels_max = 2,                      \
250                         .rates = SNDRV_PCM_RATE_8000_96000,     \
251                         .formats = SNDRV_PCM_FMTBIT_S16_LE,     \
252                 },                                              \
253                 .ops = dai_ops,         \
254         }
255
256 static struct snd_soc_dai_driver tegra210_mixer_dais[] = {
257         IN_DAI(RX, 1, &tegra210_mixer_in_dai_ops),
258         IN_DAI(RX, 2, &tegra210_mixer_in_dai_ops),
259         IN_DAI(RX, 3, &tegra210_mixer_in_dai_ops),
260         IN_DAI(RX, 4, &tegra210_mixer_in_dai_ops),
261         IN_DAI(RX, 5, &tegra210_mixer_in_dai_ops),
262         IN_DAI(RX, 6, &tegra210_mixer_in_dai_ops),
263         IN_DAI(RX, 7, &tegra210_mixer_in_dai_ops),
264         IN_DAI(RX, 8, &tegra210_mixer_in_dai_ops),
265         IN_DAI(RX, 9, &tegra210_mixer_in_dai_ops),
266         IN_DAI(RX, 10, &tegra210_mixer_in_dai_ops),
267         OUT_DAI(TX, 1, &tegra210_mixer_out_dai_ops),
268         OUT_DAI(TX, 2, &tegra210_mixer_out_dai_ops),
269         OUT_DAI(TX, 3, &tegra210_mixer_out_dai_ops),
270         OUT_DAI(TX, 4, &tegra210_mixer_out_dai_ops),
271         OUT_DAI(TX, 5, &tegra210_mixer_out_dai_ops),
272 };
273
274 #define ADDER_CTRL_DECL(name, reg)      \
275         static const struct snd_kcontrol_new name[] = { \
276                 SOC_DAPM_SINGLE("RX1", reg, 0, 1, 0),   \
277                 SOC_DAPM_SINGLE("RX2", reg, 1, 1, 0),   \
278                 SOC_DAPM_SINGLE("RX3", reg, 2, 1, 0),   \
279                 SOC_DAPM_SINGLE("RX4", reg, 3, 1, 0),   \
280                 SOC_DAPM_SINGLE("RX5", reg, 4, 1, 0),   \
281                 SOC_DAPM_SINGLE("RX6", reg, 5, 1, 0),   \
282                 SOC_DAPM_SINGLE("RX7", reg, 6, 1, 0),   \
283                 SOC_DAPM_SINGLE("RX8", reg, 7, 1, 0),   \
284                 SOC_DAPM_SINGLE("RX9", reg, 8, 1, 0),   \
285                 SOC_DAPM_SINGLE("RX10", reg, 9, 1, 0),  \
286         }
287
288 ADDER_CTRL_DECL(adder1, TEGRA210_MIXER_AXBAR_TX1_ADDER_CONFIG);
289 ADDER_CTRL_DECL(adder2, TEGRA210_MIXER_AXBAR_TX2_ADDER_CONFIG);
290 ADDER_CTRL_DECL(adder3, TEGRA210_MIXER_AXBAR_TX3_ADDER_CONFIG);
291 ADDER_CTRL_DECL(adder4, TEGRA210_MIXER_AXBAR_TX4_ADDER_CONFIG);
292 ADDER_CTRL_DECL(adder5, TEGRA210_MIXER_AXBAR_TX5_ADDER_CONFIG);
293
294 static const struct snd_kcontrol_new tegra210_mixer_gain_ctls[] = {     \
295         SOC_SINGLE_EXT("RX1 Gain", MIXER_GAIN_CONFIG_RAM_ADDR(0), 0, 0x20000, 0,
296                 tegra210_mixer_get_gain, tegra210_mixer_put_gain),
297         SOC_SINGLE_EXT("RX2 Gain", MIXER_GAIN_CONFIG_RAM_ADDR(1), 0, 0x20000, 0,
298                 tegra210_mixer_get_gain, tegra210_mixer_put_gain),
299         SOC_SINGLE_EXT("RX3 Gain", MIXER_GAIN_CONFIG_RAM_ADDR(2), 0, 0x20000, 0,
300                 tegra210_mixer_get_gain, tegra210_mixer_put_gain),
301         SOC_SINGLE_EXT("RX4 Gain", MIXER_GAIN_CONFIG_RAM_ADDR(3), 0, 0x20000, 0,
302                 tegra210_mixer_get_gain, tegra210_mixer_put_gain),
303         SOC_SINGLE_EXT("RX5 Gain", MIXER_GAIN_CONFIG_RAM_ADDR(4), 0, 0x20000, 0,
304                 tegra210_mixer_get_gain, tegra210_mixer_put_gain),
305         SOC_SINGLE_EXT("RX6 Gain", MIXER_GAIN_CONFIG_RAM_ADDR(5), 0, 0x20000, 0,
306                 tegra210_mixer_get_gain, tegra210_mixer_put_gain),
307         SOC_SINGLE_EXT("RX7 Gain", MIXER_GAIN_CONFIG_RAM_ADDR(6), 0, 0x20000, 0,
308                 tegra210_mixer_get_gain, tegra210_mixer_put_gain),
309         SOC_SINGLE_EXT("RX8 Gain", MIXER_GAIN_CONFIG_RAM_ADDR(7), 0, 0x20000, 0,
310                 tegra210_mixer_get_gain, tegra210_mixer_put_gain),
311         SOC_SINGLE_EXT("RX9 Gain", MIXER_GAIN_CONFIG_RAM_ADDR(8), 0, 0x20000, 0,
312                 tegra210_mixer_get_gain, tegra210_mixer_put_gain),
313         SOC_SINGLE_EXT("RX10 Gain", MIXER_GAIN_CONFIG_RAM_ADDR(9), 0, 0x20000, 0,
314                 tegra210_mixer_get_gain, tegra210_mixer_put_gain),
315         SOC_SINGLE("Mixer Enable", TEGRA210_MIXER_ENABLE, 0, 1, 0),
316 };
317
318 static const struct snd_soc_dapm_widget tegra210_mixer_widgets[] = {
319         SND_SOC_DAPM_AIF_IN("RX1", NULL, 0, SND_SOC_NOPM, 0, 0),
320         SND_SOC_DAPM_AIF_IN("RX2", NULL, 0, SND_SOC_NOPM, 0, 0),
321         SND_SOC_DAPM_AIF_IN("RX3", NULL, 0, SND_SOC_NOPM, 0, 0),
322         SND_SOC_DAPM_AIF_IN("RX4", NULL, 0, SND_SOC_NOPM, 0, 0),
323         SND_SOC_DAPM_AIF_IN("RX5", NULL, 0, SND_SOC_NOPM, 0, 0),
324         SND_SOC_DAPM_AIF_IN("RX6", NULL, 0, SND_SOC_NOPM, 0, 0),
325         SND_SOC_DAPM_AIF_IN("RX7", NULL, 0, SND_SOC_NOPM, 0, 0),
326         SND_SOC_DAPM_AIF_IN("RX8", NULL, 0, SND_SOC_NOPM, 0, 0),
327         SND_SOC_DAPM_AIF_IN("RX9", NULL, 0, SND_SOC_NOPM, 0, 0),
328         SND_SOC_DAPM_AIF_IN("RX10", NULL, 0, SND_SOC_NOPM, 0, 0),
329         SND_SOC_DAPM_AIF_OUT("TX1", NULL, 0,
330                 TEGRA210_MIXER_AXBAR_TX1_ENABLE, 0, 0),
331         SND_SOC_DAPM_AIF_OUT("TX2", NULL, 0,
332                 TEGRA210_MIXER_AXBAR_TX2_ENABLE, 0, 0),
333         SND_SOC_DAPM_AIF_OUT("TX3", NULL, 0,
334                 TEGRA210_MIXER_AXBAR_TX3_ENABLE, 0, 0),
335         SND_SOC_DAPM_AIF_OUT("TX4", NULL, 0,
336                 TEGRA210_MIXER_AXBAR_TX4_ENABLE, 0, 0),
337         SND_SOC_DAPM_AIF_OUT("TX5", NULL, 0,
338                 TEGRA210_MIXER_AXBAR_TX5_ENABLE, 0, 0),
339         SND_SOC_DAPM_MIXER("Adder1", SND_SOC_NOPM, 1, 0,
340                 adder1, ARRAY_SIZE(adder1)),
341         SND_SOC_DAPM_MIXER("Adder2", SND_SOC_NOPM, 1, 0,
342                 adder2, ARRAY_SIZE(adder2)),
343         SND_SOC_DAPM_MIXER("Adder3", SND_SOC_NOPM, 1, 0,
344                 adder3, ARRAY_SIZE(adder3)),
345         SND_SOC_DAPM_MIXER("Adder4", SND_SOC_NOPM, 1, 0,
346                 adder4, ARRAY_SIZE(adder4)),
347         SND_SOC_DAPM_MIXER("Adder5", SND_SOC_NOPM, 1, 0,
348                 adder5, ARRAY_SIZE(adder5)),
349 };
350
351 #define MIXER_ROUTES(name, id)  \
352         {name,  "RX1",  "RX1",},        \
353         {name,  "RX2",  "RX2",},        \
354         {name,  "RX3",  "RX3",},        \
355         {name,  "RX4",  "RX4",},        \
356         {name,  "RX5",  "RX5",},        \
357         {name,  "RX6",  "RX6",},        \
358         {name,  "RX7",  "RX7",},        \
359         {name,  "RX8",  "RX8",},        \
360         {name,  "RX9",  "RX9",},        \
361         {name,  "RX10", "RX10"},        \
362         {"TX"#id,       NULL,   name}
363
364 static const struct snd_soc_dapm_route tegra210_mixer_routes[] = {
365         { "RX1",        NULL,   "RX1 Receive" },
366         { "RX2",        NULL,   "RX2 Receive" },
367         { "RX3",        NULL,   "RX3 Receive" },
368         { "RX4",        NULL,   "RX4 Receive" },
369         { "RX5",        NULL,   "RX5 Receive" },
370         { "RX6",        NULL,   "RX6 Receive" },
371         { "RX7",        NULL,   "RX7 Receive" },
372         { "RX8",        NULL,   "RX8 Receive" },
373         { "RX9",        NULL,   "RX9 Receive" },
374         { "RX10",       NULL,   "RX10 Receive" },
375         /* route between MIXER RXs and TXs */
376         MIXER_ROUTES("Adder1", 1),
377         MIXER_ROUTES("Adder2", 2),
378         MIXER_ROUTES("Adder3", 3),
379         MIXER_ROUTES("Adder4", 4),
380         MIXER_ROUTES("Adder5", 5),
381         { "TX1 Transmit",       NULL,   "TX1" },
382         { "TX2 Transmit",       NULL,   "TX2" },
383         { "TX3 Transmit",       NULL,   "TX3" },
384         { "TX4 Transmit",       NULL,   "TX4" },
385         { "TX5 Transmit",       NULL,   "TX5" },
386 };
387
388 static struct snd_soc_codec_driver tegra210_mixer_codec = {
389         .probe = tegra210_mixer_codec_probe,
390         .dapm_widgets = tegra210_mixer_widgets,
391         .num_dapm_widgets = ARRAY_SIZE(tegra210_mixer_widgets),
392         .dapm_routes = tegra210_mixer_routes,
393         .num_dapm_routes = ARRAY_SIZE(tegra210_mixer_routes),
394         .controls = tegra210_mixer_gain_ctls,
395         .num_controls = ARRAY_SIZE(tegra210_mixer_gain_ctls),
396 };
397
398 static bool tegra210_mixer_wr_reg(struct device *dev,
399                                 unsigned int reg)
400 {
401         if (reg < TEGRA210_MIXER_AXBAR_RX_LIMIT)
402                 reg %= TEGRA210_MIXER_AXBAR_RX_STRIDE;
403         else if (reg < TEGRA210_MIXER_AXBAR_TX_LIMIT)
404                 reg = (reg % TEGRA210_MIXER_AXBAR_TX_STRIDE) +
405                         TEGRA210_MIXER_AXBAR_TX1_ENABLE;
406
407         switch (reg) {
408         case TEGRA210_MIXER_AXBAR_RX1_SOFT_RESET:
409         case TEGRA210_MIXER_AXBAR_RX1_STATUS:
410         case TEGRA210_MIXER_AXBAR_RX1_CIF_CTRL:
411         case TEGRA210_MIXER_AXBAR_RX1_CTRL:
412         case TEGRA210_MIXER_AXBAR_RX1_PEAK_CTRL:
413
414         case TEGRA210_MIXER_AXBAR_TX1_ENABLE:
415         case TEGRA210_MIXER_AXBAR_TX1_SOFT_RESET:
416         case TEGRA210_MIXER_AXBAR_TX1_INT_MASK:
417         case TEGRA210_MIXER_AXBAR_TX1_INT_SET:
418         case TEGRA210_MIXER_AXBAR_TX1_INT_CLEAR:
419         case TEGRA210_MIXER_AXBAR_TX1_CIF_CTRL:
420         case TEGRA210_MIXER_AXBAR_TX1_ADDER_CONFIG:
421
422         case TEGRA210_MIXER_ENABLE:
423         case TEGRA210_MIXER_SOFT_RESET:
424         case TEGRA210_MIXER_CG:
425         case TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_CTRL:
426         case TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_DATA:
427         case TEGRA210_MIXER_AHUBRAMCTL_PEAKM_RAM_CTRL:
428         case TEGRA210_MIXER_AHUBRAMCTL_PEAKM_RAM_DATA:
429         case TEGRA210_MIXER_CTRL:
430                 return true;
431         default:
432                 return false;
433         };
434 }
435
436 static bool tegra210_mixer_rd_reg(struct device *dev,
437                                 unsigned int reg)
438 {
439         if (reg < TEGRA210_MIXER_AXBAR_RX_LIMIT)
440                 reg %= TEGRA210_MIXER_AXBAR_RX_STRIDE;
441         else if (reg < TEGRA210_MIXER_AXBAR_TX_LIMIT)
442                 reg = (reg % TEGRA210_MIXER_AXBAR_TX_STRIDE) +
443                         TEGRA210_MIXER_AXBAR_TX1_ENABLE;
444
445         switch (reg) {
446         case TEGRA210_MIXER_AXBAR_RX1_SOFT_RESET:
447         case TEGRA210_MIXER_AXBAR_RX1_STATUS:
448         case TEGRA210_MIXER_AXBAR_RX1_CIF_CTRL:
449         case TEGRA210_MIXER_AXBAR_RX1_CTRL:
450         case TEGRA210_MIXER_AXBAR_RX1_PEAK_CTRL:
451         case TEGRA210_MIXER_AXBAR_RX1_SAMPLE_COUNT:
452
453         case TEGRA210_MIXER_AXBAR_TX1_ENABLE:
454         case TEGRA210_MIXER_AXBAR_TX1_SOFT_RESET:
455         case TEGRA210_MIXER_AXBAR_TX1_STATUS:
456         case TEGRA210_MIXER_AXBAR_TX1_INT_STATUS:
457         case TEGRA210_MIXER_AXBAR_TX1_INT_MASK:
458         case TEGRA210_MIXER_AXBAR_TX1_INT_SET:
459         case TEGRA210_MIXER_AXBAR_TX1_INT_CLEAR:
460         case TEGRA210_MIXER_AXBAR_TX1_CIF_CTRL:
461         case TEGRA210_MIXER_AXBAR_TX1_ADDER_CONFIG:
462
463         case TEGRA210_MIXER_ENABLE:
464         case TEGRA210_MIXER_SOFT_RESET:
465         case TEGRA210_MIXER_CG:
466         case TEGRA210_MIXER_STATUS:
467         case TEGRA210_MIXER_INT_STATUS:
468         case TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_CTRL:
469         case TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_DATA:
470         case TEGRA210_MIXER_AHUBRAMCTL_PEAKM_RAM_CTRL:
471         case TEGRA210_MIXER_AHUBRAMCTL_PEAKM_RAM_DATA:
472         case TEGRA210_MIXER_CTRL:
473                 return true;
474         default:
475                 return false;
476         };
477 }
478
479 static bool tegra210_mixer_volatile_reg(struct device *dev,
480                                 unsigned int reg)
481 {
482         if (reg < TEGRA210_MIXER_AXBAR_RX_LIMIT)
483                 reg %= TEGRA210_MIXER_AXBAR_RX_STRIDE;
484         else if (reg < TEGRA210_MIXER_AXBAR_TX_LIMIT)
485                 reg = (reg % TEGRA210_MIXER_AXBAR_TX_STRIDE) +
486                         TEGRA210_MIXER_AXBAR_TX1_ENABLE;
487
488         switch (reg) {
489         case TEGRA210_MIXER_AXBAR_RX1_SOFT_RESET:
490         case TEGRA210_MIXER_AXBAR_RX1_STATUS:
491
492         case TEGRA210_MIXER_AXBAR_TX1_SOFT_RESET:
493         case TEGRA210_MIXER_AXBAR_TX1_STATUS:
494         case TEGRA210_MIXER_AXBAR_TX1_INT_STATUS:
495         case TEGRA210_MIXER_AXBAR_TX1_INT_SET:
496
497         case TEGRA210_MIXER_SOFT_RESET:
498         case TEGRA210_MIXER_STATUS:
499         case TEGRA210_MIXER_INT_STATUS:
500         case TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_CTRL:
501         case TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_DATA:
502         case TEGRA210_MIXER_AHUBRAMCTL_PEAKM_RAM_CTRL:
503         case TEGRA210_MIXER_AHUBRAMCTL_PEAKM_RAM_DATA:
504                 return true;
505         default:
506                 return false;
507         };
508 }
509
510 static bool tegra210_mixer_precious_reg(struct device *dev,
511                                 unsigned int reg)
512 {
513         switch (reg) {
514         case TEGRA210_MIXER_AHUBRAMCTL_GAIN_CONFIG_RAM_DATA:
515         case TEGRA210_MIXER_AHUBRAMCTL_PEAKM_RAM_DATA:
516                 return true;
517         default:
518                 return false;
519         };
520 }
521
522 static const struct regmap_config tegra210_mixer_regmap_config = {
523         .reg_bits = 32,
524         .reg_stride = 4,
525         .val_bits = 32,
526         .max_register = TEGRA210_MIXER_CTRL,
527         .writeable_reg = tegra210_mixer_wr_reg,
528         .readable_reg = tegra210_mixer_rd_reg,
529         .volatile_reg = tegra210_mixer_volatile_reg,
530         .precious_reg = tegra210_mixer_precious_reg,
531         .cache_type = REGCACHE_RBTREE,
532 };
533
534 static const struct tegra210_mixer_soc_data soc_data_tegra210 = {
535         .set_audio_cif = tegra210_xbar_set_cif
536 };
537
538 static const struct of_device_id tegra210_mixer_of_match[] = {
539         { .compatible = "nvidia,tegra210-amixer", .data = &soc_data_tegra210 },
540         {},
541 };
542
543 static int tegra210_mixer_platform_probe(struct platform_device *pdev)
544 {
545         struct tegra210_mixer *mixer;
546         struct resource *mem, *memregion;
547         void __iomem *regs;
548         int ret;
549         const struct of_device_id *match;
550         struct tegra210_mixer_soc_data *soc_data;
551
552         match = of_match_device(tegra210_mixer_of_match, &pdev->dev);
553         if (!match) {
554                 dev_err(&pdev->dev, "Error: No device match found\n");
555                 ret = -ENODEV;
556                 goto err;
557         }
558         soc_data = (struct tegra210_mixer_soc_data *)match->data;
559
560         mixer = devm_kzalloc(&pdev->dev,
561                 sizeof(struct tegra210_mixer), GFP_KERNEL);
562         if (!mixer) {
563                 dev_err(&pdev->dev, "Can't allocate tegra210_mixer\n");
564                 ret = -ENOMEM;
565                 goto err;
566         }
567         dev_set_drvdata(&pdev->dev, mixer);
568
569         mixer->soc_data = soc_data;
570         mixer->gain_coeff[0] = 0x2003EEC;
571         mixer->gain_coeff[1] = 0x38EBC;
572         mixer->gain_coeff[2] = 0xFFFFFEBB;
573         mixer->gain_coeff[3] = 0x1414253;
574         mixer->gain_coeff[4] = 0xF6CEC;
575         mixer->gain_coeff[5] = 0xFFFFC9DE;
576         mixer->gain_coeff[6] = 0xC4B873;
577         mixer->gain_coeff[7] = 0x422F76;
578         mixer->gain_coeff[8] = 0xFFFA021C;
579         mixer->gain_coeff[9] = 0x10000;
580         mixer->gain_coeff[10] = 0x1A1;
581         mixer->gain_coeff[11] = 0x823;
582         mixer->gain_coeff[12] = 0x3e80;
583         mixer->gain_coeff[13] = 0x83126E;
584
585 #ifndef CONFIG_MACH_GRENADA
586         mixer->clk_mixer = devm_clk_get(&pdev->dev, NULL);
587         if (IS_ERR(mixer->clk_mixer)) {
588                 dev_err(&pdev->dev, "Can't retrieve tegra210_mixer clock\n");
589                 ret = PTR_ERR(mixer->clk_mixer);
590                 goto err;
591         }
592 #endif
593
594         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
595         if (!mem) {
596                 dev_err(&pdev->dev, "No memory resource\n");
597                 ret = -ENODEV;
598                 goto err_clk_put;
599         }
600
601         memregion = devm_request_mem_region(&pdev->dev, mem->start,
602                                             resource_size(mem), DRV_NAME);
603         if (!memregion) {
604                 dev_err(&pdev->dev, "Memory region already claimed\n");
605                 ret = -EBUSY;
606                 goto err_clk_put;
607         }
608
609         regs = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
610         if (!regs) {
611                 dev_err(&pdev->dev, "ioremap failed\n");
612                 ret = -ENOMEM;
613                 goto err_clk_put;
614         }
615
616         mixer->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
617                                             &tegra210_mixer_regmap_config);
618         if (IS_ERR(mixer->regmap)) {
619                 dev_err(&pdev->dev, "regmap init failed\n");
620                 ret = PTR_ERR(mixer->regmap);
621                 goto err_clk_put;
622         }
623         regcache_cache_only(mixer->regmap, true);
624
625         if (of_property_read_u32(pdev->dev.of_node,
626                                 "nvidia,ahub-amixer-id",
627                                 &pdev->dev.id) < 0) {
628                 dev_err(&pdev->dev,
629                         "Missing property nvidia,ahub-amixer-id\n");
630                 ret = -ENODEV;
631                 goto err_clk_put;
632         }
633
634         pm_runtime_enable(&pdev->dev);
635         if (!pm_runtime_enabled(&pdev->dev)) {
636                 ret = tegra210_mixer_runtime_resume(&pdev->dev);
637                 if (ret)
638                         goto err_pm_disable;
639         }
640
641         ret = snd_soc_register_codec(&pdev->dev, &tegra210_mixer_codec,
642                                      tegra210_mixer_dais,
643                                      ARRAY_SIZE(tegra210_mixer_dais));
644         if (ret != 0) {
645                 dev_err(&pdev->dev, "Could not register CODEC: %d\n", ret);
646                 goto err_suspend;
647         }
648
649         return 0;
650
651 err_suspend:
652         if (!pm_runtime_status_suspended(&pdev->dev))
653                 tegra210_mixer_runtime_suspend(&pdev->dev);
654 err_pm_disable:
655         pm_runtime_disable(&pdev->dev);
656 err_clk_put:
657 #ifndef CONFIG_MACH_GRENADA
658         devm_clk_put(&pdev->dev, mixer->clk_mixer);
659 #else
660         dev_err(&pdev->dev, "error\n");
661 #endif
662 err:
663         return ret;
664 }
665
666 static int tegra210_mixer_platform_remove(struct platform_device *pdev)
667 {
668 #ifndef CONFIG_MACH_GRENADA
669         struct tegra210_mixer *mixer = dev_get_drvdata(&pdev->dev);
670 #endif
671
672         snd_soc_unregister_codec(&pdev->dev);
673
674         pm_runtime_disable(&pdev->dev);
675         if (!pm_runtime_status_suspended(&pdev->dev))
676                 tegra210_mixer_runtime_suspend(&pdev->dev);
677
678 #ifndef CONFIG_MACH_GRENADA
679         devm_clk_put(&pdev->dev, mixer->clk_mixer);
680 #endif
681
682         return 0;
683 }
684
685 static const struct dev_pm_ops tegra210_mixer_pm_ops = {
686         SET_RUNTIME_PM_OPS(tegra210_mixer_runtime_suspend,
687                            tegra210_mixer_runtime_resume, NULL)
688 };
689
690 static struct platform_driver tegra210_mixer_driver = {
691         .driver = {
692                 .name = DRV_NAME,
693                 .owner = THIS_MODULE,
694                 .of_match_table = tegra210_mixer_of_match,
695                 .pm = &tegra210_mixer_pm_ops,
696         },
697         .probe = tegra210_mixer_platform_probe,
698         .remove = tegra210_mixer_platform_remove,
699 };
700 module_platform_driver(tegra210_mixer_driver);
701
702 MODULE_AUTHOR("Arun Shamanna Lakshmi <aruns@nvidia.com>");
703 MODULE_DESCRIPTION("Tegra210 MIXER ASoC driver");
704 MODULE_LICENSE("GPL v2");
705 MODULE_ALIAS("platform:" DRV_NAME);
706 MODULE_DEVICE_TABLE(of, tegra210_mixer_of_match);