summaryrefslogtreecommitdiff
path: root/sound/soc/tegra/tegra_soc_ad193x.c
blob: c059648a8c2f4b34578afeb2092ac172a083a995 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * tegra_soc_ad193x.c  --  SoC audio for tegra
 *
 * (c) 2010-2011 Nvidia Graphics Pvt. Ltd.
 *  http://www.nvidia.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "tegra_soc.h"
#include <sound/soc-dapm.h>
#include "../codecs/ad193x.h"

static struct platform_device *tegra_snd_device;

extern struct snd_soc_dai tegra_i2s_dai[];
extern struct snd_soc_dai tegra_generic_codec_dai[];
extern struct snd_soc_platform tegra_soc_platform;

static int tegra_hifi_hw_params(struct snd_pcm_substream *substream,
				struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
	struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
	int dai_flag = 0, sys_clk;
	int err;
	int num_chans;
	int mode;

	enum dac_dap_data_format data_fmt;

	if (tegra_das_is_port_master(tegra_audio_codec_type_hifi))
		dai_flag |= SND_SOC_DAIFMT_CBM_CFM;
	else
		dai_flag |= SND_SOC_DAIFMT_CBS_CFS;

	dai_flag |= SND_SOC_DAIFMT_DSP_A;

	err = snd_soc_dai_set_fmt(codec_dai, dai_flag);
	if (err < 0) {
		pr_err("codec_dai fmt not set\n");
		return err;
	}
	err = snd_soc_dai_set_fmt(cpu_dai, dai_flag);
	if (err < 0) {
		pr_err("cpu_dai fmt not set\n");
		return err;
	}

	/*FIXME: not sure this is the right way.
	   This should be samplerate times 256 or 128 based on codec need */
	sys_clk = 48000 * 512;
	err = snd_soc_dai_set_sysclk(codec_dai, 0, sys_clk, SND_SOC_CLOCK_IN);
	if (err < 0) {
		pr_err("codec_dai clock not set\n");
		return err;
	}
	err = snd_soc_dai_set_sysclk(cpu_dai, 0, sys_clk, SND_SOC_CLOCK_IN);
	if (err < 0) {
		pr_err("cpu_dai clock not set\n");
		return err;
	}

	data_fmt = tegra_das_get_codec_data_fmt(tegra_audio_codec_type_hifi);
	if (!(data_fmt & dac_dap_data_format_tdm)) {
		mode = snd_soc_read(codec_dai, AD193X_DAC_CTRL0);
		mode &= ~(AD193X_DAC_SERFMT_MASK);
		mode |= AD193X_DAC_SERFMT_STEREO;
		snd_soc_write(codec_dai, AD193X_DAC_CTRL0, mode);
		snd_soc_dai_set_tdm_slot(codec_dai, 0xFF, 0xFF, 2, 32);
	} else {
		/* set codec DAI slots, 8 channels, all channels are enabled */
		num_chans = codec_dai->id == 0 ? 4 : 8;
		snd_soc_dai_set_tdm_slot(codec_dai, 0xFF, 0xFF, num_chans, 32);
	}

	return 0;
}

void tegra_ext_control(struct snd_soc_codec *codec, int new_con)
{
	return;
}

int tegra_codec_startup(struct snd_pcm_substream *substream)
{
	tegra_das_power_mode(true);

	return 0;
}

void tegra_codec_shutdown(struct snd_pcm_substream *substream)
{
	tegra_das_power_mode(false);
}

int tegra_soc_suspend_pre(struct platform_device *pdev, pm_message_t state)
{
	return 0;
}

int tegra_soc_suspend_post(struct platform_device *pdev, pm_message_t state)
{
	tegra_das_disable_mclk();

	return 0;
}

int tegra_soc_resume_pre(struct platform_device *pdev)
{
	tegra_das_enable_mclk();

	return 0;
}

int tegra_soc_resume_post(struct platform_device *pdev)
{
	return 0;
}

static struct snd_soc_ops tegra_hifi_ops = {
	.hw_params = tegra_hifi_hw_params,
	.startup = tegra_codec_startup,
	.shutdown = tegra_codec_shutdown,
};

static int tegra_codec_init(struct snd_soc_codec *codec)
{
	struct tegra_audio_data *audio_data = codec->socdev->codec_data;
	int err = 0;

	if (!audio_data->init_done) {

		err = tegra_das_open();
		if (err) {
			pr_err("Failed get dap mclk\n");
			err = -ENODEV;
			goto ad193x_init_fail;
		}

		err = tegra_das_enable_mclk();
		if (err) {
			pr_err("Failed to enable dap mclk\n");
			err = -ENODEV;
			goto ad193x_init_fail;
		}

		err = tegra_controls_init(codec);
		if (err < 0) {
			pr_err("Failed in controls init\n");
			goto ad193x_init_fail;
		}

		audio_data->codec = codec;
		audio_data->init_done = 1;
	}

	return err;

ad193x_init_fail:

	tegra_das_disable_mclk();
	tegra_das_close();
	return err;
}

#define TEGRA_CREATE_SOC_DAI_LINK(xname, xstreamname, \
			xcpudai, xcodecdai, xops) \
{							\
	.name = xname,					\
	.stream_name = xstreamname,     \
	.cpu_dai = xcpudai,             \
	.codec_dai = xcodecdai,         \
	.init = tegra_codec_init,       \
	.ops = xops,                    \
}

/* Currently both the DAI link to the same CODEC dai
 * as only one of them can be active at the same time
 * */
static struct snd_soc_dai_link tegra_soc_dai[] = {
	TEGRA_CREATE_SOC_DAI_LINK("AD1937", "AD1937 Multi-8",
				  &tegra_i2s_dai[0], &ad193x_dai,
				  &tegra_hifi_ops),

	TEGRA_CREATE_SOC_DAI_LINK("AD1937", "Generic Multi-16",
				  &tegra_i2s_dai[1],
				  &tegra_generic_codec_dai[3], &tegra_hifi_ops),
};

static struct tegra_audio_data audio_data = {
	.init_done = 0,
	.play_device = TEGRA_AUDIO_DEVICE_NONE,
	.capture_device = TEGRA_AUDIO_DEVICE_NONE,
	.is_call_mode = false,
	.codec_con = TEGRA_AUDIO_OFF,
};

static struct snd_soc_card tegra_snd_soc = {
	.name = "tegra-ad193x",
	.platform = &tegra_soc_platform,
	.dai_link = tegra_soc_dai,
	.num_links = ARRAY_SIZE(tegra_soc_dai),
	.suspend_pre = tegra_soc_suspend_pre,
	.suspend_post = tegra_soc_suspend_post,
	.resume_pre = tegra_soc_resume_pre,
	.resume_post = tegra_soc_resume_post,
};

static struct snd_soc_device tegra_snd_devdata = {
	.card = &tegra_snd_soc,
	.codec_dev = &soc_codec_dev_ad193x,
	.codec_data = &audio_data,
};

static int __init tegra_init(void)
{
	int ret = 0;

	tegra_snd_device = platform_device_alloc("soc-audio", -1);
	if (!tegra_snd_device) {
		pr_err("failed to allocate soc-audio\n");
		return -ENOMEM;
	}

	platform_set_drvdata(tegra_snd_device, &tegra_snd_devdata);
	tegra_snd_devdata.dev = &tegra_snd_device->dev;

	ret = platform_device_add(tegra_snd_device);
	if (ret) {
		pr_err("audio device could not be added\n");
		goto fail;
	}

	return 0;

fail:
	if (tegra_snd_device) {
		platform_device_put(tegra_snd_device);
		tegra_snd_device = 0;
	}

	return ret;
}

static void __exit tegra_exit(void)
{
	platform_device_unregister(tegra_snd_device);
}

module_init(tegra_init);
module_exit(tegra_exit);

/* Module information */
MODULE_DESCRIPTION("Tegra ALSA SoC for AD193X");
MODULE_LICENSE("GPL");