2 * neo1973_gta02_wm8753.c -- SoC audio for Openmoko freerunner
4 * Copyright 2007 Openmoko Inc
5 * Author: Graeme Gregory <graeme@openmoko.org>
6 * Copyright 2007 Wolfson Microelectronics PLC.
7 * Author: Graeme Gregory <linux@wolfsonmicro.com>
8 * Copyright 2009 Wolfson Microelectronics
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/timer.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/soc.h>
24 #include <sound/soc-dapm.h>
26 #include <asm/mach-types.h>
28 #include <plat/regs-iis.h>
30 #include <mach/regs-clock.h>
31 #include <mach/regs-gpio.h>
32 #include <mach/hardware.h>
34 #include <mach/regs-gpioj.h>
35 #include <mach/gta02.h>
36 #include "../codecs/wm8753.h"
37 #include "s3c24xx-pcm.h"
38 #include "s3c24xx-i2s.h"
40 static struct snd_soc_card neo1973_gta02;
42 static int neo1973_gta02_hifi_hw_params(struct snd_pcm_substream *substream,
43 struct snd_pcm_hw_params *params)
45 struct snd_soc_pcm_runtime *rtd = substream->private_data;
46 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
47 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
48 unsigned int pll_out = 0, bclk = 0;
50 unsigned long iis_clkrate;
52 iis_clkrate = s3c24xx_i2s_get_clockrate();
54 switch (params_rate(params)) {
60 bclk = WM8753_BCLK_DIV_4;
64 bclk = WM8753_BCLK_DIV_2;
68 bclk = WM8753_BCLK_DIV_16;
72 bclk = WM8753_BCLK_DIV_8;
76 bclk = WM8753_BCLK_DIV_4;
80 bclk = WM8753_BCLK_DIV_2;
85 /* set codec DAI configuration */
86 ret = snd_soc_dai_set_fmt(codec_dai,
87 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
88 SND_SOC_DAIFMT_CBM_CFM);
92 /* set cpu DAI configuration */
93 ret = snd_soc_dai_set_fmt(cpu_dai,
94 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
95 SND_SOC_DAIFMT_CBM_CFM);
99 /* set the codec system clock for DAC and ADC */
100 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
105 /* set MCLK division for sample rate */
106 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
107 S3C2410_IISMOD_32FS);
111 /* set codec BCLK division for sample rate */
112 ret = snd_soc_dai_set_clkdiv(codec_dai,
113 WM8753_BCLKDIV, bclk);
117 /* set prescaler division for sample rate */
118 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
119 S3C24XX_PRESCALE(4, 4));
123 /* codec PLL input is PCLK/4 */
124 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1,
125 iis_clkrate / 4, pll_out);
132 static int neo1973_gta02_hifi_hw_free(struct snd_pcm_substream *substream)
134 struct snd_soc_pcm_runtime *rtd = substream->private_data;
135 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
137 /* disable the PLL */
138 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0);
142 * Neo1973 WM8753 HiFi DAI opserations.
144 static struct snd_soc_ops neo1973_gta02_hifi_ops = {
145 .hw_params = neo1973_gta02_hifi_hw_params,
146 .hw_free = neo1973_gta02_hifi_hw_free,
149 static int neo1973_gta02_voice_hw_params(
150 struct snd_pcm_substream *substream,
151 struct snd_pcm_hw_params *params)
153 struct snd_soc_pcm_runtime *rtd = substream->private_data;
154 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
155 unsigned int pcmdiv = 0;
157 unsigned long iis_clkrate;
159 iis_clkrate = s3c24xx_i2s_get_clockrate();
161 if (params_rate(params) != 8000)
163 if (params_channels(params) != 1)
166 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
168 /* todo: gg check mode (DSP_B) against CSR datasheet */
169 /* set codec DAI configuration */
170 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
171 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
175 /* set the codec system clock for DAC and ADC */
176 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK,
177 12288000, SND_SOC_CLOCK_IN);
181 /* set codec PCM division for sample rate */
182 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV,
187 /* configue and enable PLL for 12.288MHz output */
188 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2,
189 iis_clkrate / 4, 12288000);
196 static int neo1973_gta02_voice_hw_free(struct snd_pcm_substream *substream)
198 struct snd_soc_pcm_runtime *rtd = substream->private_data;
199 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
201 /* disable the PLL */
202 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0);
205 static struct snd_soc_ops neo1973_gta02_voice_ops = {
206 .hw_params = neo1973_gta02_voice_hw_params,
207 .hw_free = neo1973_gta02_voice_hw_free,
213 static u8 lm4853_state;
215 /* This has no effect, it exists only to maintain compatibility with
216 * existing ALSA state files.
218 static int lm4853_set_state(struct snd_kcontrol *kcontrol,
219 struct snd_ctl_elem_value *ucontrol)
221 int val = ucontrol->value.integer.value[0];
224 lm4853_state |= LM4853_AMP;
226 lm4853_state &= ~LM4853_AMP;
231 static int lm4853_get_state(struct snd_kcontrol *kcontrol,
232 struct snd_ctl_elem_value *ucontrol)
234 ucontrol->value.integer.value[0] = lm4853_state & LM4853_AMP;
239 static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
240 struct snd_ctl_elem_value *ucontrol)
242 int val = ucontrol->value.integer.value[0];
245 lm4853_state |= LM4853_SPK;
246 s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 0);
248 lm4853_state &= ~LM4853_SPK;
249 s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 1);
255 static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
256 struct snd_ctl_elem_value *ucontrol)
258 ucontrol->value.integer.value[0] = (lm4853_state & LM4853_SPK) >> 1;
263 static int lm4853_event(struct snd_soc_dapm_widget *w,
264 struct snd_kcontrol *k,
267 if (SND_SOC_DAPM_EVENT_ON(event))
268 s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 0);
270 if (SND_SOC_DAPM_EVENT_OFF(event))
271 s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 1);
276 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
277 SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
278 SND_SOC_DAPM_LINE("GSM Line Out", NULL),
279 SND_SOC_DAPM_LINE("GSM Line In", NULL),
280 SND_SOC_DAPM_MIC("Headset Mic", NULL),
281 SND_SOC_DAPM_MIC("Handset Mic", NULL),
282 SND_SOC_DAPM_SPK("Handset Spk", NULL),
286 /* example machine audio_mapnections */
287 static const struct snd_soc_dapm_route audio_map[] = {
289 /* Connections to the lm4853 amp */
290 {"Stereo Out", NULL, "LOUT1"},
291 {"Stereo Out", NULL, "ROUT1"},
293 /* Connections to the GSM Module */
294 {"GSM Line Out", NULL, "MONO1"},
295 {"GSM Line Out", NULL, "MONO2"},
296 {"RXP", NULL, "GSM Line In"},
297 {"RXN", NULL, "GSM Line In"},
299 /* Connections to Headset */
300 {"MIC1", NULL, "Mic Bias"},
301 {"Mic Bias", NULL, "Headset Mic"},
304 {"MIC2", NULL, "Mic Bias"},
305 {"MIC2N", NULL, "Mic Bias"},
306 {"Mic Bias", NULL, "Handset Mic"},
309 {"Handset Spk", NULL, "LOUT2"},
310 {"Handset Spk", NULL, "ROUT2"},
312 /* Connect the ALC pins */
313 {"ACIN", NULL, "ACOP"},
316 static const struct snd_kcontrol_new wm8753_neo1973_gta02_controls[] = {
317 SOC_DAPM_PIN_SWITCH("Stereo Out"),
318 SOC_DAPM_PIN_SWITCH("GSM Line Out"),
319 SOC_DAPM_PIN_SWITCH("GSM Line In"),
320 SOC_DAPM_PIN_SWITCH("Headset Mic"),
321 SOC_DAPM_PIN_SWITCH("Handset Mic"),
322 SOC_DAPM_PIN_SWITCH("Handset Spk"),
324 /* This has no effect, it exists only to maintain compatibility with
325 * existing ALSA state files.
327 SOC_SINGLE_EXT("Amp State Switch", 6, 0, 1, 0,
330 SOC_SINGLE_EXT("Amp Spk Switch", 7, 0, 1, 0,
336 * This is an example machine initialisation for a wm8753 connected to a
339 static int neo1973_gta02_wm8753_init(struct snd_soc_codec *codec)
343 /* set up NC codec pins */
344 snd_soc_dapm_nc_pin(codec, "OUT3");
345 snd_soc_dapm_nc_pin(codec, "OUT4");
346 snd_soc_dapm_nc_pin(codec, "LINE1");
347 snd_soc_dapm_nc_pin(codec, "LINE2");
349 /* Add neo1973 gta02 specific widgets */
350 snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
351 ARRAY_SIZE(wm8753_dapm_widgets));
353 /* add neo1973 gta02 specific controls */
354 err = snd_soc_add_controls(codec, wm8753_neo1973_gta02_controls,
355 ARRAY_SIZE(wm8753_neo1973_gta02_controls));
360 /* set up neo1973 gta02 specific audio path audio_map */
361 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
363 /* set endpoints to default off mode */
364 snd_soc_dapm_disable_pin(codec, "Stereo Out");
365 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
366 snd_soc_dapm_disable_pin(codec, "GSM Line In");
367 snd_soc_dapm_disable_pin(codec, "Headset Mic");
368 snd_soc_dapm_disable_pin(codec, "Handset Mic");
369 snd_soc_dapm_disable_pin(codec, "Handset Spk");
371 snd_soc_dapm_sync(codec);
379 static struct snd_soc_dai bt_dai = {
385 .rates = SNDRV_PCM_RATE_8000,
386 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
390 .rates = SNDRV_PCM_RATE_8000,
391 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
394 static struct snd_soc_dai_link neo1973_gta02_dai[] = {
395 { /* Hifi Playback - for similatious use with voice below */
397 .stream_name = "WM8753 HiFi",
398 .cpu_dai = &s3c24xx_i2s_dai,
399 .codec_dai = &wm8753_dai[WM8753_DAI_HIFI],
400 .init = neo1973_gta02_wm8753_init,
401 .ops = &neo1973_gta02_hifi_ops,
405 .stream_name = "Voice",
407 .codec_dai = &wm8753_dai[WM8753_DAI_VOICE],
408 .ops = &neo1973_gta02_voice_ops,
412 static struct snd_soc_card neo1973_gta02 = {
413 .name = "neo1973-gta02",
414 .platform = &s3c24xx_soc_platform,
415 .dai_link = neo1973_gta02_dai,
416 .num_links = ARRAY_SIZE(neo1973_gta02_dai),
419 static struct snd_soc_device neo1973_gta02_snd_devdata = {
420 .card = &neo1973_gta02,
421 .codec_dev = &soc_codec_dev_wm8753,
424 static struct platform_device *neo1973_gta02_snd_device;
426 static int __init neo1973_gta02_init(void)
430 if (!machine_is_neo1973_gta02()) {
432 "Only GTA02 is supported by this ASoC driver\n");
436 /* register bluetooth DAI here */
437 ret = snd_soc_register_dai(&bt_dai);
441 neo1973_gta02_snd_device = platform_device_alloc("soc-audio", -1);
442 if (!neo1973_gta02_snd_device)
445 platform_set_drvdata(neo1973_gta02_snd_device,
446 &neo1973_gta02_snd_devdata);
447 neo1973_gta02_snd_devdata.dev = &neo1973_gta02_snd_device->dev;
448 ret = platform_device_add(neo1973_gta02_snd_device);
451 platform_device_put(neo1973_gta02_snd_device);
455 /* Initialise GPIOs used by amp */
456 s3c2410_gpio_cfgpin(GTA02_GPIO_HP_IN, S3C2410_GPIO_OUTPUT);
457 s3c2410_gpio_cfgpin(GTA02_GPIO_AMP_SHUT, S3C2410_GPIO_OUTPUT);
459 /* Amp off by default */
460 s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 1);
462 /* Speaker off by default */
463 s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 1);
467 module_init(neo1973_gta02_init);
469 static void __exit neo1973_gta02_exit(void)
471 snd_soc_unregister_dai(&bt_dai);
472 platform_device_unregister(neo1973_gta02_snd_device);
474 module_exit(neo1973_gta02_exit);
476 /* Module information */
477 MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org");
478 MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 GTA02");
479 MODULE_LICENSE("GPL");