aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/omap/gta04-audio.c
blob: f49877c8da0f31d9130b2ce86ed50441c5f365a7 (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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * gta04.c  --  SoC audio for GTA04 (based on OMAP3 Beagle)
 *
 * Author: Steve Sakoman <steve@sakoman.com>
 * Author: Nikolaus Schaller <hns@goldelico.com>
 *
 * 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.
 *
 * 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 St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/clk.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/input.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/jack.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>

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

#include <linux/i2c/twl4030-madc.h>

#include "omap-mcbsp.h"
#include "omap-pcm.h"
#include "../codecs/twl4030.h"

static int omap3gta04_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;
	unsigned int fmt;
	int ret;

	switch (params_channels(params)) {
	case 2: /* Stereo I2S mode */
		fmt =	SND_SOC_DAIFMT_I2S |
			SND_SOC_DAIFMT_NB_NF |
			SND_SOC_DAIFMT_CBM_CFM;
		break;
	case 4: /* Four channel TDM mode */
		fmt =	SND_SOC_DAIFMT_DSP_A |
			SND_SOC_DAIFMT_IB_NF |
			SND_SOC_DAIFMT_CBM_CFM;
		break;
	default:
		return -EINVAL;
	}

	/* Set codec DAI configuration */
	ret = snd_soc_dai_set_fmt(codec_dai, fmt);
	if (ret < 0) {
		printk(KERN_ERR "can't set codec DAI configuration\n");
		return ret;
	}

	/* Set cpu DAI configuration */
	ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
	if (ret < 0) {
		printk(KERN_ERR "can't set cpu DAI configuration\n");
		return ret;
	}

	/* Set the codec system clock for DAC and ADC */
	ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
				     SND_SOC_CLOCK_IN);
	if (ret < 0) {
		printk(KERN_ERR "can't set codec system clock\n");
		return ret;
	}

	return 0;
}

/* this shows how we could control the AUX in/out switch or the Video in/out */

static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
				 struct snd_kcontrol *k, int event)
{
	/*
	 if (SND_SOC_DAPM_EVENT_ON(event)) {
	 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
	 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
	 } else {
	 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
	 mdelay(1);
	 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
	 }
	 */	
	return 0;
}

static const struct snd_soc_dapm_widget gta04_dapm_widgets[] = {
	SND_SOC_DAPM_DAC("PCM DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
			   0, 0, NULL, 0, omap3pandora_hp_event,
			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_HP("Headphone Jack", NULL),
	SND_SOC_DAPM_LINE("Line Out", NULL),
	SND_SOC_DAPM_MIC("Internal Mic", NULL),
	SND_SOC_DAPM_MIC("Headphone Mic", NULL),
	SND_SOC_DAPM_LINE("Line In", NULL),
};

static const struct snd_soc_dapm_route audio_map[] = {
	{"Headphone Amplifier", NULL, "PCM DAC"},
	{"Line Out", NULL, "PCM DAC"},
	{"Headphone Jack", NULL, "Headphone Amplifier"},

	{"AUXL", NULL, "Line In"},
	{"AUXR", NULL, "Line In"},
	
	/* Headset Mic: HSMIC with bias */
	{"HSMIC", NULL, "Headset Mic Bias"},
	{"Headset Mic Bias", NULL, "Headphone Mic"},

	{"MAINMIC", NULL, "Mic Bias 1"},
	{"Mic Bias 1", NULL, "Internal Mic"},
	/*
	 {"SUBMIC", NULL, "Mic Bias 2"},
	 {"Mic Bias 2", NULL, "Mic (external)"},
	 */
};

static struct {
	struct snd_soc_jack hs_jack;
	struct delayed_work jack_work;
	struct snd_soc_codec *codec;
	int open;
	/* When any jack is present, we:
	 * - poll more quickly to catch button presses
	 * - assume a 'short' is 'button press', not 'headset has
	 *   no mic
	 * 'present' stores SND_JACK_HEADPHONE and SND_JACK_MICROPHONE
	 * indication what we thing is present.
	 */
	int present;
} jack;

static void gta04_audio_jack_work(struct work_struct *work)
{
	long val;
	long delay = msecs_to_jiffies(500);
	int jackbits;

	/* choose delay *before* checking presence so we still get
	 * one long delay on first insertion to help with debounce.
	 */
	if (jack.present)
		delay = msecs_to_jiffies(50);

	val = twl4030_get_madc_conversion(7);
	/* On my device:
	 * open circuit = around 20
	 * short circuit = around 800
	 * microphone   = around 830-840 !!!
	 */
	if (val < 100) {
		/* open circuit */
		jackbits = 0;
		jack.present = 0;
		/* debounce */
		delay = msecs_to_jiffies(500);
	} else if (val < 820) {
		/* short */
		if (jack.present == 0) {
			/* Inserted headset with no mic */
			jack.present = SND_JACK_HEADPHONE;
			jackbits = jack.present;
		} else if (jack.present & SND_JACK_MICROPHONE) {
			/* mic shorter == button press */
			jackbits = SND_JACK_BTN_0 | jack.present;
		} else {
			/* headphones still present */
			jackbits = jack.present;
		}
	} else {
		/* There is a microphone there */
		jack.present = SND_JACK_HEADSET;
		jackbits = jack.present;
	}
	snd_soc_jack_report(&jack.hs_jack, jackbits,
			    SND_JACK_HEADSET | SND_JACK_BTN_0);

	if (jack.open)
		schedule_delayed_work(&jack.jack_work, delay);
}

static int gta04_audio_suspend(struct snd_soc_card *card)
{
	if (jack.codec) {
		snd_soc_dapm_disable_pin(&jack.codec->dapm, "Headset Mic Bias");
		snd_soc_dapm_sync(&jack.codec->dapm);
	}
	return 0;
}

static int gta04_audio_resume(struct snd_soc_card *card)
{
	if (jack.codec && jack.open) {
		snd_soc_dapm_force_enable_pin(&jack.codec->dapm, "Headset Mic Bias");
		snd_soc_dapm_sync(&jack.codec->dapm);
	}
	return 0;
}

static int gta04_jack_open(struct input_dev *dev)
{
	snd_soc_dapm_force_enable_pin(&jack.codec->dapm, "Headset Mic Bias");
	snd_soc_dapm_sync(&jack.codec->dapm);
	jack.open = 1;
	schedule_delayed_work(&jack.jack_work, msecs_to_jiffies(100));
	return 0;
}

static void gta04_jack_close(struct input_dev *dev)
{
	jack.open = 0;
	cancel_delayed_work_sync(&jack.jack_work);
	snd_soc_dapm_disable_pin(&jack.codec->dapm, "Headset Mic Bias");
	snd_soc_dapm_sync(&jack.codec->dapm);
}

static int omap3gta04_init(struct snd_soc_pcm_runtime *runtime)
{
	int ret;
	struct snd_soc_codec *codec = runtime->codec;
	struct snd_soc_dapm_context *dapm = &codec->dapm;
	
	ret = snd_soc_dapm_new_controls(dapm, gta04_dapm_widgets,
					ARRAY_SIZE(gta04_dapm_widgets));
	if (ret < 0)
		return ret;
	
	snd_soc_dapm_add_routes(dapm, audio_map,
				ARRAY_SIZE(audio_map));

//	snd_soc_dapm_enable_pin(codec, "Ext Mic");
//	snd_soc_dapm_enable_pin(codec, "Ext Spk");
//	snd_soc_dapm_disable_pin(codec, "Headphone Mic");
//	snd_soc_dapm_disable_pin(codec, "Headphone Amplifier");

	/* TWL4030 not connected pins */
	//	snd_soc_dapm_nc_pin(codec, "OUTL");
	//	snd_soc_dapm_nc_pin(codec, "OUTR");
	//	snd_soc_dapm_nc_pin(codec, "EARPIECE");
	snd_soc_dapm_nc_pin(dapm, "PREDRIVEL");
	snd_soc_dapm_nc_pin(dapm, "PREDRIVER");
	//	snd_soc_dapm_nc_pin(codec, "HSOL");
	//	snd_soc_dapm_nc_pin(codec, "HSOR");
	snd_soc_dapm_nc_pin(dapm, "CARKITMIC");
	snd_soc_dapm_nc_pin(dapm, "CARKITL");
	snd_soc_dapm_nc_pin(dapm, "CARKITR");
	//	snd_soc_dapm_nc_pin(codec, "HFL");
	//	snd_soc_dapm_nc_pin(codec, "HFR");
	//	snd_soc_dapm_nc_pin(codec, "VIBRA");
	//	snd_soc_dapm_nc_pin(codec, "HSMIC");
	snd_soc_dapm_nc_pin(dapm, "DIGIMIC0");
	snd_soc_dapm_nc_pin(dapm, "DIGIMIC1");

	/* We can detect when something is plugged in,
	 * but we need to poll :-(
	 */
	ret = snd_soc_jack_new(codec, "Headset Jack",
			       SND_JACK_HEADSET | SND_JACK_BTN_0,
			       &jack.hs_jack);
	if (ret)
		return ret;
	INIT_DELAYED_WORK(&jack.jack_work, gta04_audio_jack_work);
	jack.codec = codec;
	jack.hs_jack.jack->input_dev->open = gta04_jack_open;
	jack.hs_jack.jack->input_dev->close = gta04_jack_close;
		
	return snd_soc_dapm_sync(dapm);
}

static struct snd_soc_ops omap3gta04_ops = {
	.hw_params = omap3gta04_hw_params,
};

/* Digital audio interface glue - connects codec <--> CPU */
static struct snd_soc_dai_link omap3gta04_dai = {
		.name = "TWL4030",
		.stream_name = "TWL4030",
		.cpu_dai_name	= "omap-mcbsp.2",
		.platform_name = "omap-pcm-audio",
		.codec_dai_name = "twl4030-hifi",
		.codec_name = "twl4030-codec",
		.dai_fmt = (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
			    SND_SOC_DAIFMT_CBM_CFM),
		.ops = &omap3gta04_ops,
		.init = &omap3gta04_init
};

/* Audio machine driver */
static struct snd_soc_card snd_soc_omap3gta04 = {
	.name = "gta04",
	.owner = THIS_MODULE,
	.dai_link = &omap3gta04_dai,
	.num_links = 1,
	.suspend_pre = gta04_audio_suspend,
	.resume_post = gta04_audio_resume,
};

static struct platform_device *omap3gta04_snd_device;

static int __init omap3gta04_soc_init(void)
{
	int ret;

#if 0
	if (!machine_is_gta04() && !machine_is_omap3_gta04()) {
		pr_debug("Not GTA04!\n");
		return -ENODEV;
	}
#endif
	pr_info("GTA04 OMAP3 SoC snd init\n");
	
	// FIXME: set any GPIOs i.e. enable Audio in/out switch
	// microphone power etc.

	omap3gta04_snd_device = platform_device_alloc("soc-audio", 0);
	if (!omap3gta04_snd_device) {
		printk(KERN_ERR "Platform device allocation failed\n");
		return -ENOMEM;
	}

	platform_set_drvdata(omap3gta04_snd_device, &snd_soc_omap3gta04);

	ret = platform_device_add(omap3gta04_snd_device);
	if (ret)
		goto err1;

	return 0;

err1:
	printk(KERN_ERR "Unable to add platform device\n");
	platform_device_put(omap3gta04_snd_device);

	return ret;
}

static void __exit omap3gta04_soc_exit(void)
{
	platform_device_unregister(omap3gta04_snd_device);
}

module_init(omap3gta04_soc_init);
module_exit(omap3gta04_soc_exit);

MODULE_AUTHOR("Steve Sakoman <steve@sakoman.com>");
MODULE_DESCRIPTION("ALSA SoC OMAP3 GTA04");
MODULE_LICENSE("GPL");