summaryrefslogtreecommitdiff
path: root/ANDROID_3.4.5/sound/soc/wmt/wmt-soc.c
blob: f62e993db79db271157187779cfd78de7df7c977 (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
/*++ 
 * linux/sound/soc/wmt/wmt-soc.c
 * WonderMedia audio driver for ALSA
 *
 * Copyright c 2010  WonderMedia  Technologies, Inc.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * WonderMedia Technologies, Inc.
 * 4F, 533, Chung-Cheng Road, Hsin-Tien, Taipei 231, R.O.C
--*/

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/device.h>

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/hwdep.h>

#include <asm/mach-types.h>
#include <mach/hardware.h>

#include "wmt-soc.h"
#include "wmt-pcm.h"
#include "wmt_hwdep.h"
#include "../codecs/wmt_vt1602.h"
#include "../codecs/vt1603.h"

extern int wmt_getsyspara(char *varname, unsigned char *varval, int *varlen);
extern void wmt_set_i2s_share_pin();
char wmt_codec_name[80];
char wmt_dai_name[80];
char wmt_rate[10];

#define AUDIO_NAME "WMT_SOC"
//#define WMT_SOC_DEBUG 1
//#define WMT_SOC_DEBUG_DETAIL 1

#ifdef WMT_SOC_DEBUG
#define DPRINTK(format, arg...) \
	printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
#else
#define DPRINTK(format, arg...) do {} while (0)
#endif

#ifdef WMT_SOC_DEBUG_DETAIL
#define DBG_DETAIL(format, arg...) \
	printk(KERN_INFO AUDIO_NAME ": [%s]" format "\n" , __FUNCTION__, ## arg)
#else
#define DBG_DETAIL(format, arg...) do {} while (0)
#endif

#define err(format, arg...) \
	printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg)
#define info(format, arg...) \
	printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
#define warn(format, arg...) \
	printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg)

#define WMT_I2S_RATES  SNDRV_PCM_RATE_8000_96000

static struct snd_soc_card snd_soc_machine_wmt;

static int wmt_soc_primary_startup(struct snd_pcm_substream *substream)
{
	DBG_DETAIL();
	return 0;
}

static void wmt_soc_primary_shutdown(struct snd_pcm_substream *substream)
{
	DBG_DETAIL();
}

static int wmt_soc_primary_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->codec_dai;
	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
	int ret = 0;

	DBG_DETAIL();
	
	if (strcmp(wmt_codec_name, "hwdac")) {
		/* Set codec DAI configuration */
		ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_CBS_CFS |
				SND_SOC_DAIFMT_I2S |SND_SOC_DAIFMT_NB_NF);
		if (ret < 0)
			return ret;
	}
	

	/* Set cpu DAI configuration for I2S */
	ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S 
				|SND_SOC_DAIFMT_MASTER_MASK | SND_SOC_DAIFMT_NB_NF);  
	if (ret < 0)
		return ret;

	if ((!strcmp(wmt_codec_name, "vt1602")) || (!strcmp(wmt_codec_name, "vt1603"))) {
		/* Set the codec system clock for DAC and ADC */
		if (!(params_rate(params) % 11025)) {
			ret = snd_soc_dai_set_sysclk(codec_dai, 0, 11289600,
						    SND_SOC_CLOCK_IN);		    
		}				    
		else {
			ret = snd_soc_dai_set_sysclk(codec_dai, 0, 12288000,
						    SND_SOC_CLOCK_IN);				    
	  }					    
	}

	return ret;
}

static int wmt_soc_second_startup(struct snd_pcm_substream *substream)
{
	//dump_stack();
	DBG_DETAIL();
	return 0;
}

static void wmt_soc_second_shutdown(struct snd_pcm_substream *substream)
{
	//dump_stack();
	DBG_DETAIL();
}

static int wmt_soc_second_hw_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *params)
{
	//dump_stack();
	DBG_DETAIL();
	return 0;
}

static int wmt_soc_dai_init(struct snd_soc_pcm_runtime *rtd)
{
	DBG_DETAIL();
	wmt_soc_hwdep_new(rtd->codec);
	return 0;
}

static int wmt_suspend_pre(struct snd_soc_card *card)
{
	snd_soc_dapm_disable_pin(&card->rtd->codec->dapm, "Left HP");
	snd_soc_dapm_disable_pin(&card->rtd->codec->dapm, "Right HP");
	snd_soc_dapm_disable_pin(&card->rtd->codec->dapm, "Left SPK");
	snd_soc_dapm_disable_pin(&card->rtd->codec->dapm, "Right SPK");
}

static int wmt_suspend_post(struct snd_soc_card *card)
{
	DBG_DETAIL();

	/* Disable BIT15:I2S clock, BIT4:ARFP clock, BIT3:ARF clock */
	PMCEU_VAL &= ~(BIT15 | BIT4 | BIT3);

	snd_soc_dapm_enable_pin(&card->rtd->codec->dapm, "Left HP");
	snd_soc_dapm_enable_pin(&card->rtd->codec->dapm, "Right HP");
	snd_soc_dapm_enable_pin(&card->rtd->codec->dapm, "Left SPK");
	snd_soc_dapm_enable_pin(&card->rtd->codec->dapm, "Right SPK");
	
	return 0;
}

static int wmt_resume_pre(struct snd_soc_card *card)
{
	/* Enable MCLK before VT1602 codec enable, otherwise the codec will be disabled. */
	
	/* set to 24.576MHz */
	auto_pll_divisor(DEV_I2S, CLK_ENABLE , 0, 0);
	auto_pll_divisor(DEV_I2S, SET_PLLDIV, 1, 24576);
	/* Enable BIT4:ARFP clock, BIT3:ARF clock */
	PMCEU_VAL |= (BIT4 | BIT3);
	/* Enable BIT2:AUD clock */
	PMCE3_VAL |= BIT2;

	wmt_set_i2s_share_pin();
	return 0;
}

static struct snd_soc_ops wmt_soc_primary_ops = {
	.startup = wmt_soc_primary_startup,
	.hw_params = wmt_soc_primary_hw_params,
	.shutdown = wmt_soc_primary_shutdown,
};

static struct snd_soc_ops wmt_soc_second_ops = {
	.startup = wmt_soc_second_startup,
	.hw_params = wmt_soc_second_hw_params,
	.shutdown = wmt_soc_second_shutdown,
};

/* Digital audio interface glue - connects codec <--> CPU */
static struct snd_soc_dai_link wmt_dai[] = {
	{
		.name = "HiFi",
		.stream_name = "HiFi",
		.platform_name = "wmt-audio-pcm.0",
		.init = wmt_soc_dai_init,
		.ops = &wmt_soc_primary_ops,
	},
	{
		.name = "Voice",
		.stream_name = "Voice",
		.platform_name = "wmt-pcm-dma.0",
		.cpu_dai_name = "wmt-pcm-controller.0",
		.codec_dai_name = "HWDAC",
		.codec_name = "wmt-i2s-hwdac.0",
		.ops = &wmt_soc_second_ops,
	},
};

/* Audio machine driver */
static struct snd_soc_card snd_soc_machine_wmt = {
	.name = "WMT_VT1609",
	.dai_link = wmt_dai,
	.num_links = ARRAY_SIZE(wmt_dai),
	.suspend_pre = wmt_suspend_pre,
	.suspend_post = wmt_suspend_post,
	.resume_pre = wmt_resume_pre,
};

static struct platform_device *wmt_snd_device;

static int __init wmt_soc_init(void)
{
	int ret, i;
	char buf[64];
	int len = ARRAY_SIZE(buf);
	
	if (wmt_getsyspara("wmt.audio.i2s", buf, &len) != 0) {
		strcpy(wmt_dai_name, "null");	
		strcpy(wmt_codec_name, "null");
	}
	else {
		strcpy(wmt_dai_name, "i2s");
	}

	if (strcmp(wmt_dai_name, "null")) {
		for (i = 0; i < 80; ++i) {
			if (buf[i] == ':')
				break;
			else
				wmt_codec_name[i] = buf[i];
		}
	}
	else {
		return -EINVAL;
	}

	// is wm8994, return and load wmt_wm8994 module
	if (strcmp(wmt_codec_name, "wm8994") == 0)
		return -ENODEV;

	info("dai_name=%s, codec_name=%s", wmt_dai_name, wmt_codec_name);
	

	wmt_i2s_dai.playback.rates = (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_8000);
	wmt_i2s_dai.capture.rates = (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_8000);
	wmt_dai[0].cpu_dai_name = "wmt-i2s.0";

	if (!strcmp(wmt_codec_name, "vt1602")) {		
		wmt_dai[0].codec_dai_name = "VT1602";
		wmt_dai[0].codec_name = "vt1602.1-001a";
	}
	else if (!strcmp(wmt_codec_name, "hwdac")) {
		wmt_dai[0].codec_dai_name = "HWDAC";
		wmt_dai[0].codec_name = "wmt-i2s-hwdac.0";
	}
	else if (!strcmp(wmt_codec_name, "vt1603")) {
		wmt_dai[0].codec_dai_name = "VT1603";
		wmt_dai[0].codec_name = "vt1603-codec";
	}

	/* Doing register process after plug-in */
	wmt_snd_device = platform_device_alloc("soc-audio", -1);
	if (!wmt_snd_device)
		return -ENOMEM;

	platform_set_drvdata(wmt_snd_device, &snd_soc_machine_wmt);
	
	ret = platform_device_add(wmt_snd_device);
	if (ret)
		goto err1;

	return 0;
	
err1:
	platform_device_put(wmt_snd_device);
	return ret;

}

static void __exit wmt_soc_exit(void)
{
	DBG_DETAIL();
	
	platform_device_unregister(wmt_snd_device);
}

module_init(wmt_soc_init);
module_exit(wmt_soc_exit);

MODULE_AUTHOR("WonderMedia Technologies, Inc.");
MODULE_DESCRIPTION("WMT [ALSA SoC/Machine] driver");
MODULE_LICENSE("GPL");