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>
25 #include <sound/jack.h>
27 #include <asm/mach-types.h>
29 #include <plat/regs-iis.h>
31 #include <mach/regs-clock.h>
32 #include <mach/regs-gpio.h>
33 #include <mach/hardware.h>
35 #include <mach/regs-gpioj.h>
36 #include <mach/gta02.h>
37 #include "../codecs/wm8753.h"
38 #include "s3c24xx-pcm.h"
39 #include "s3c24xx-i2s.h"
41 static struct snd_soc_card neo1973_gta02;
42 static struct snd_soc_jack gta02_hs_jack;
44 static int neo1973_gta02_hifi_hw_params(struct snd_pcm_substream *substream,
45 struct snd_pcm_hw_params *params)
47 struct snd_soc_pcm_runtime *rtd = substream->private_data;
48 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
49 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
50 unsigned int pll_out = 0, bclk = 0;
52 unsigned long iis_clkrate;
54 iis_clkrate = s3c24xx_i2s_get_clockrate();
56 switch (params_rate(params)) {
62 bclk = WM8753_BCLK_DIV_4;
66 bclk = WM8753_BCLK_DIV_2;
70 bclk = WM8753_BCLK_DIV_16;
74 bclk = WM8753_BCLK_DIV_8;
78 bclk = WM8753_BCLK_DIV_4;
82 bclk = WM8753_BCLK_DIV_2;
87 /* set codec DAI configuration */
88 ret = snd_soc_dai_set_fmt(codec_dai,
89 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
90 SND_SOC_DAIFMT_CBM_CFM);
94 /* set cpu DAI configuration */
95 ret = snd_soc_dai_set_fmt(cpu_dai,
96 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
97 SND_SOC_DAIFMT_CBM_CFM);
101 /* set the codec system clock for DAC and ADC */
102 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
107 /* set MCLK division for sample rate */
108 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
109 S3C2410_IISMOD_32FS);
113 /* set codec BCLK division for sample rate */
114 ret = snd_soc_dai_set_clkdiv(codec_dai,
115 WM8753_BCLKDIV, bclk);
119 /* set prescaler division for sample rate */
120 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
121 S3C24XX_PRESCALE(4, 4));
125 /* codec PLL input is PCLK/4 */
126 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1,
127 iis_clkrate / 4, pll_out);
134 static int neo1973_gta02_hifi_hw_free(struct snd_pcm_substream *substream)
136 struct snd_soc_pcm_runtime *rtd = substream->private_data;
137 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
139 /* disable the PLL */
140 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0);
144 * Neo1973 WM8753 HiFi DAI opserations.
146 static struct snd_soc_ops neo1973_gta02_hifi_ops = {
147 .hw_params = neo1973_gta02_hifi_hw_params,
148 .hw_free = neo1973_gta02_hifi_hw_free,
151 static int neo1973_gta02_voice_hw_params(
152 struct snd_pcm_substream *substream,
153 struct snd_pcm_hw_params *params)
155 struct snd_soc_pcm_runtime *rtd = substream->private_data;
156 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
157 unsigned int pcmdiv = 0;
159 unsigned long iis_clkrate;
161 iis_clkrate = s3c24xx_i2s_get_clockrate();
163 if (params_rate(params) != 8000)
165 if (params_channels(params) != 1)
168 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
170 /* todo: gg check mode (DSP_B) against CSR datasheet */
171 /* set codec DAI configuration */
172 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
173 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
177 /* set the codec system clock for DAC and ADC */
178 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK,
179 12288000, SND_SOC_CLOCK_IN);
183 /* set codec PCM division for sample rate */
184 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV,
189 /* configue and enable PLL for 12.288MHz output */
190 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2,
191 iis_clkrate / 4, 12288000);
198 static int neo1973_gta02_voice_hw_free(struct snd_pcm_substream *substream)
200 struct snd_soc_pcm_runtime *rtd = substream->private_data;
201 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
203 /* disable the PLL */
204 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0);
207 static struct snd_soc_ops neo1973_gta02_voice_ops = {
208 .hw_params = neo1973_gta02_voice_hw_params,
209 .hw_free = neo1973_gta02_voice_hw_free,
215 static u8 lm4853_state;
217 /* This has no effect, it exists only to maintain compatibility with
218 * existing ALSA state files.
220 static int lm4853_set_state(struct snd_kcontrol *kcontrol,
221 struct snd_ctl_elem_value *ucontrol)
223 int val = ucontrol->value.integer.value[0];
226 lm4853_state |= LM4853_AMP;
228 lm4853_state &= ~LM4853_AMP;
233 static int lm4853_get_state(struct snd_kcontrol *kcontrol,
234 struct snd_ctl_elem_value *ucontrol)
236 ucontrol->value.integer.value[0] = lm4853_state & LM4853_AMP;
241 static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
242 struct snd_ctl_elem_value *ucontrol)
244 int val = ucontrol->value.integer.value[0];
247 lm4853_state |= LM4853_SPK;
248 s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 0);
250 lm4853_state &= ~LM4853_SPK;
251 s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 1);
257 static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
258 struct snd_ctl_elem_value *ucontrol)
260 ucontrol->value.integer.value[0] = (lm4853_state & LM4853_SPK) >> 1;
265 static int lm4853_event(struct snd_soc_dapm_widget *w,
266 struct snd_kcontrol *k,
269 if (SND_SOC_DAPM_EVENT_ON(event))
270 s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 0);
272 if (SND_SOC_DAPM_EVENT_OFF(event))
273 s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 1);
278 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
279 SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
280 SND_SOC_DAPM_LINE("GSM Line Out", NULL),
281 SND_SOC_DAPM_LINE("GSM Line In", NULL),
282 SND_SOC_DAPM_MIC("Headset Mic", NULL),
283 SND_SOC_DAPM_MIC("Handset Mic", NULL),
284 SND_SOC_DAPM_SPK("Handset Spk", NULL),
288 /* example machine audio_mapnections */
289 static const struct snd_soc_dapm_route audio_map[] = {
291 /* Connections to the lm4853 amp */
292 {"Stereo Out", NULL, "LOUT1"},
293 {"Stereo Out", NULL, "ROUT1"},
295 /* Connections to the GSM Module */
296 {"GSM Line Out", NULL, "MONO1"},
297 {"GSM Line Out", NULL, "MONO2"},
298 {"RXP", NULL, "GSM Line In"},
299 {"RXN", NULL, "GSM Line In"},
301 /* Connections to Headset */
302 {"MIC1", NULL, "Mic Bias"},
303 {"Mic Bias", NULL, "Headset Mic"},
306 {"MIC2", NULL, "Mic Bias"},
307 {"MIC2N", NULL, "Mic Bias"},
308 {"Mic Bias", NULL, "Handset Mic"},
311 {"Handset Spk", NULL, "LOUT2"},
312 {"Handset Spk", NULL, "ROUT2"},
314 /* Connect the ALC pins */
315 {"ACIN", NULL, "ACOP"},
318 static const struct snd_kcontrol_new wm8753_neo1973_gta02_controls[] = {
319 SOC_DAPM_PIN_SWITCH("Stereo Out"),
320 SOC_DAPM_PIN_SWITCH("GSM Line Out"),
321 SOC_DAPM_PIN_SWITCH("GSM Line In"),
322 SOC_DAPM_PIN_SWITCH("Headset Mic"),
323 SOC_DAPM_PIN_SWITCH("Handset Mic"),
324 SOC_DAPM_PIN_SWITCH("Handset Spk"),
326 /* This has no effect, it exists only to maintain compatibility with
327 * existing ALSA state files.
329 SOC_SINGLE_EXT("Amp State Switch", 6, 0, 1, 0,
332 SOC_SINGLE_EXT("Amp Spk Switch", 7, 0, 1, 0,
337 static struct snd_soc_jack_pin gta02_hs_jack_pins[] = {
339 .pin = "Headset Mic",
340 .mask = SND_JACK_MICROPHONE
344 .mask = SND_JACK_HEADPHONE,
349 static struct snd_soc_jack_gpio gta02_hs_jack_gpios[] = {
351 .gpio = GTA02_GPIO_JACK_INSERT,
352 .name = "headset-gpio",
353 .report = SND_JACK_HEADSET,
354 .debounce_time = 100,
359 * This is an example machine initialisation for a wm8753 connected to a
362 static int neo1973_gta02_wm8753_init(struct snd_soc_codec *codec)
366 /* set up NC codec pins */
367 snd_soc_dapm_nc_pin(codec, "OUT3");
368 snd_soc_dapm_nc_pin(codec, "OUT4");
369 snd_soc_dapm_nc_pin(codec, "LINE1");
370 snd_soc_dapm_nc_pin(codec, "LINE2");
372 /* Add neo1973 gta02 specific widgets */
373 snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
374 ARRAY_SIZE(wm8753_dapm_widgets));
376 /* add neo1973 gta02 specific controls */
377 err = snd_soc_add_controls(codec, wm8753_neo1973_gta02_controls,
378 ARRAY_SIZE(wm8753_neo1973_gta02_controls));
383 /* set up neo1973 gta02 specific audio path audio_map */
384 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
386 /* set endpoints to default off mode */
387 snd_soc_dapm_disable_pin(codec, "Stereo Out");
388 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
389 snd_soc_dapm_disable_pin(codec, "GSM Line In");
390 snd_soc_dapm_disable_pin(codec, "Headset Mic");
391 snd_soc_dapm_disable_pin(codec, "Handset Mic");
392 snd_soc_dapm_disable_pin(codec, "Handset Spk");
394 snd_soc_dapm_sync(codec);
396 err = snd_soc_jack_new(&neo1973_gta02, "Headset Jack",
397 SND_JACK_HEADSET, >a02_hs_jack);
399 dev_err(codec->card->dev, "failed to alloc headset jack\n");
403 err = snd_soc_jack_add_pins(>a02_hs_jack, ARRAY_SIZE(gta02_hs_jack_pins),
406 dev_err(codec->card->dev, "failed to add headset jack pins\n");
410 err = snd_soc_jack_add_gpios(>a02_hs_jack, ARRAY_SIZE(gta02_hs_jack_gpios),
411 gta02_hs_jack_gpios);
413 dev_err(codec->card->dev, "failed to add headset jack gpios\n");
424 static struct snd_soc_dai bt_dai = {
430 .rates = SNDRV_PCM_RATE_8000,
431 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
435 .rates = SNDRV_PCM_RATE_8000,
436 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
439 static struct snd_soc_dai_link neo1973_gta02_dai[] = {
440 { /* Hifi Playback - for similatious use with voice below */
442 .stream_name = "WM8753 HiFi",
443 .cpu_dai = &s3c24xx_i2s_dai,
444 .codec_dai = &wm8753_dai[WM8753_DAI_HIFI],
445 .init = neo1973_gta02_wm8753_init,
446 .ops = &neo1973_gta02_hifi_ops,
450 .stream_name = "Voice",
452 .codec_dai = &wm8753_dai[WM8753_DAI_VOICE],
453 .ops = &neo1973_gta02_voice_ops,
457 static struct snd_soc_card neo1973_gta02 = {
458 .name = "neo1973-gta02",
459 .platform = &s3c24xx_soc_platform,
460 .dai_link = neo1973_gta02_dai,
461 .num_links = ARRAY_SIZE(neo1973_gta02_dai),
464 static struct snd_soc_device neo1973_gta02_snd_devdata = {
465 .card = &neo1973_gta02,
466 .codec_dev = &soc_codec_dev_wm8753,
469 static struct platform_device *neo1973_gta02_snd_device;
472 static int __init neo1973_gta02_init(void)
476 if (!machine_is_neo1973_gta02()) {
478 "Only GTA02 is supported by this ASoC driver\n");
482 /* register bluetooth DAI here */
483 ret = snd_soc_register_dai(&bt_dai);
487 neo1973_gta02_snd_device = platform_device_alloc("soc-audio", -1);
488 if (!neo1973_gta02_snd_device) {
490 goto err_unregister_dai;
493 platform_set_drvdata(neo1973_gta02_snd_device,
494 &neo1973_gta02_snd_devdata);
495 neo1973_gta02_snd_devdata.dev = &neo1973_gta02_snd_device->dev;
496 ret = platform_device_add(neo1973_gta02_snd_device);
499 platform_device_put(neo1973_gta02_snd_device);
500 goto err_unregister_dai;
503 /* Initialise GPIOs used by amp */
504 s3c2410_gpio_cfgpin(GTA02_GPIO_HP_IN, S3C2410_GPIO_OUTPUT);
505 s3c2410_gpio_cfgpin(GTA02_GPIO_AMP_SHUT, S3C2410_GPIO_OUTPUT);
507 /* Amp off by default */
508 s3c2410_gpio_setpin(GTA02_GPIO_AMP_SHUT, 1);
510 /* Speaker off by default */
511 s3c2410_gpio_setpin(GTA02_GPIO_HP_IN, 1);
517 snd_soc_unregister_dai(&bt_dai);
520 module_init(neo1973_gta02_init);
522 static void __exit neo1973_gta02_exit(void)
524 snd_soc_unregister_dai(&bt_dai);
525 snd_soc_jack_free_gpios(>a02_hs_jack, ARRAY_SIZE(gta02_hs_jack_gpios),
526 gta02_hs_jack_gpios);
528 platform_device_unregister(neo1973_gta02_snd_device);
530 module_exit(neo1973_gta02_exit);
532 /* Module information */
533 MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org");
534 MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 GTA02");
535 MODULE_LICENSE("GPL");