summaryrefslogtreecommitdiff
path: root/sound/soc/codecs/av8100_audio.c
blob: 7bcb985b9ebb735bc4a6519dffad089c0794be03 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * Author: Ola Lilja (ola.o.lilja@stericsson.com)
 *         for ST-Ericsson.
 *
 * License terms:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/initval.h>
#include <sound/soc.h>
#include <video/av8100.h>
#include <video/hdmi.h>

#include "av8100_audio.h"

#define AV8100_SUPPORTED_RATE (SNDRV_PCM_RATE_48000)
#define AV8100_SUPPORTED_FMT (SNDRV_PCM_FMTBIT_S16_LE)

static int setupAV8100_stereo(void)
{
	union av8100_configuration config;
	struct av8100_status status;
	int ret;

	pr_debug("%s: Enter.\n", __func__);

	/* Startup AV8100 if it is not already started */
	status = av8100_status_get();
	if (status.av8100_state < AV8100_OPMODE_STANDBY) {
		pr_info("%s: Powering up AV8100.", __func__);
		ret = av8100_powerup();
		if (ret != 0) {
			pr_err("%s: Power up AV8100 failed "
				"(av8100_powerup returned %d)!\n",
				__func__,
				ret);
			return -EINVAL;
		}
	}
	if (status.av8100_state < AV8100_OPMODE_INIT) {
		ret = av8100_download_firmware(NULL, 0, I2C_INTERFACE);
		if (ret != 0) {
			pr_err("%s: Download firmware failed "
				"(av8100_download_firmware returned %d)!\n",
				__func__,
				ret);
			return -EINVAL;
		}
	}

	/* Set the HDMI format of AV8100 */
	pr_info("%s: Setting hdmi_format.", __func__);
	config.hdmi_format.hdmi_mode = AV8100_HDMI_ON;
	config.hdmi_format.hdmi_format = AV8100_HDMI;
	ret = av8100_conf_prep(AV8100_COMMAND_AUDIO_INPUT_FORMAT, &config);
	if (ret != 0) {
		pr_err("%s: Setting hdmi_format failed "
			"(av8100_conf_prep returned %d)!\n",
			__func__,
			ret);
		return -EINVAL;
	}
	ret = av8100_conf_w(AV8100_COMMAND_AUDIO_INPUT_FORMAT,
			NULL,
			NULL,
			I2C_INTERFACE);
	if (ret != 0) {
		pr_err("%s: Setting hdmi_format failed "
			"(av8100_conf_w returned %d)!\n",
			__func__,
			ret);
		return -EINVAL;
	}

	/* Set the audio input format of AV8100 */
	pr_info("%s: Setting audio_input_format.", __func__);
	config.audio_input_format.audio_input_if_format	= AV8100_AUDIO_I2SDELAYED_MODE;
	config.audio_input_format.i2s_input_nb = 1;
	config.audio_input_format.sample_audio_freq = AV8100_AUDIO_FREQ_48KHZ;
	config.audio_input_format.audio_word_lg	= AV8100_AUDIO_16BITS;
	config.audio_input_format.audio_format = AV8100_AUDIO_LPCM_MODE;
	config.audio_input_format.audio_if_mode = AV8100_AUDIO_MASTER;
	config.audio_input_format.audio_mute = AV8100_AUDIO_MUTE_DISABLE;
	ret = av8100_conf_prep(AV8100_COMMAND_AUDIO_INPUT_FORMAT, &config);
	if (ret != 0) {
		pr_err("%s: Setting audio_input_format failed "
			"(av8100_conf_prep returned %d)!\n",
			__func__,
			ret);
		return -EINVAL;
	}
	if (av8100_conf_w(AV8100_COMMAND_AUDIO_INPUT_FORMAT,
		NULL, NULL, I2C_INTERFACE) != 0) {
		pr_err("%s: Setting audio_input_format failed "
			"(av8100_conf_w returned %d)!\n",
			__func__,
			ret);
		return -EINVAL;
	}

	return 0;
}

static int av8100_codec_pcm_prepare(struct snd_pcm_substream *substream,
				struct snd_soc_dai *dai)
{
	pr_debug("%s: Enter.\n", __func__);

	return 0;
}

static int av8100_codec_pcm_hw_params(struct snd_pcm_substream *substream,
				struct snd_pcm_hw_params *hw_params,
				struct snd_soc_dai *dai)
{
	int ret;
	int channels;

	pr_debug("%s: Enter.\n", __func__);

	channels = params_channels(hw_params);
	switch (channels) {
	case 1:
		goto error_channels;
	case 2:
		ret = setupAV8100_stereo();
		break;
	case 6:
		goto error_channels;
	default:
		goto error_channels;
	}

	return ret;

error_channels:
	pr_err("%s: Unsupported number of channels (%d)!\n", __func__, channels);

	return -1;
}

static void av8100_codec_pcm_shutdown(struct snd_pcm_substream *substream,
				struct snd_soc_dai *dai)
{
	pr_debug("%s: Enter.\n", __func__);
}

static int av8100_codec_set_dai_sysclk(struct snd_soc_dai *codec_dai,
				int clk_id,
				unsigned int freq, int dir)
{
	pr_debug("%s: Enter.\n", __func__);

	return 0;
}

static int av8100_codec_set_dai_fmt(struct snd_soc_dai *codec_dai,
				unsigned int fmt)
{
	pr_debug("%s: Enter.\n", __func__);

	return 0;
}

struct snd_soc_dai av8100_codec_dai[] = {
	{
		.name = "av8100_0",
		.playback = {
			.stream_name = "av8100_0",
			.channels_min = 2,
			.channels_max = 6,
			.rates = AV8100_SUPPORTED_RATE,
			.formats = AV8100_SUPPORTED_FMT,
		},
		.capture = {
			.stream_name = "av8100_0",
			.channels_min = 2,
			.channels_max = 6,
			.rates = AV8100_SUPPORTED_RATE,
			.formats = AV8100_SUPPORTED_FMT,
		},
		.ops = (struct snd_soc_dai_ops[]) {
			{
			.prepare = av8100_codec_pcm_prepare,
			.hw_params = av8100_codec_pcm_hw_params,
			.shutdown = av8100_codec_pcm_shutdown,
			.set_sysclk = av8100_codec_set_dai_sysclk,
			.set_fmt = av8100_codec_set_dai_fmt,
			}
		},
	}
};
EXPORT_SYMBOL_GPL(av8100_codec_dai);

static unsigned int av8100_codec_read(struct snd_soc_codec *codec,
				unsigned int ctl)
{
	pr_debug("%s: Enter.\n", __func__);

	return 0;
}

static int av8100_codec_write(struct snd_soc_codec *codec,
			unsigned int ctl,
			unsigned int value)
{
	pr_debug("%s: Enter.\n", __func__);

	return 0;
}

static int av8100_codec_probe(struct platform_device *pdev)
{
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
	struct snd_soc_codec *codec;
	int ret;

	pr_info("%s: Enter (pdev = %p).\n", __func__, pdev);

	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
	if (codec == NULL)
		return -ENOMEM;
	codec->name = "AV8100";
	codec->owner = THIS_MODULE;
	codec->dai = &av8100_codec_dai[0];
	codec->num_dai = 1;
	codec->read = av8100_codec_read;
	codec->write = av8100_codec_write;
	INIT_LIST_HEAD(&codec->dapm_widgets);
	INIT_LIST_HEAD(&codec->dapm_paths);

	mutex_init(&codec->mutex);

	socdev->card->codec = codec;

	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
	if (ret < 0) {
		pr_err("%s: Error: to create new PCMs. error %d\n",
			__func__,
			ret);
		goto err;
	}

	return 0;

err:
	kfree(codec);
	return ret;
}

static int av8100_codec_remove(struct platform_device *pdev)
{
	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
	struct snd_soc_codec *codec = socdev->card->codec;

	pr_debug("%s: Enter (pdev = %p).\n", __func__, pdev);

	if (!codec)
		return 0;

	snd_soc_free_pcms(socdev);
	kfree(socdev->card->codec);

	return 0;
}

static int av8100_codec_suspend(struct platform_device *pdev,
				pm_message_t state)
{
	pr_debug("%s: Enter (pdev = %p).\n", __func__, pdev);

	return 0;
}

static int av8100_codec_resume(struct platform_device *pdev)
{
	pr_debug("%s: Enter (pdev = %p).\n", __func__, pdev);

	return 0;
}

struct snd_soc_codec_device soc_codec_dev_av8100 = {
	.probe = av8100_codec_probe,
	.remove = av8100_codec_remove,
	.suspend = av8100_codec_suspend,
	.resume = av8100_codec_resume
};
EXPORT_SYMBOL_GPL(soc_codec_dev_av8100);

static int __devinit av8100_codec_init(void)
{
	int ret;

	pr_debug("%s: Enter.\n", __func__);

	pr_info("%s: Register codec-dai.\n", __func__);
	ret = snd_soc_register_dai(&av8100_codec_dai[0]);
	if (ret < 0) {
		pr_debug("%s: Error: Failed to register codec-dai (ret = %d).\n",
			__func__,
			ret);
	}

	return ret;
}

static void av8100_codec_exit(void)
{
	pr_debug("%s: Enter.\n", __func__);

	snd_soc_unregister_dai(&av8100_codec_dai[0]);
}

module_init(av8100_codec_init);
module_exit(av8100_codec_exit);

MODULE_DESCRIPTION("AV8100 ASoC codec driver");
MODULE_AUTHOR("www.stericsson.com");
MODULE_LICENSE("GPL");