2 * neo1973_gta02_wm8753.c -- SoC audio for Openmoko Freerunner(GTA02)
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 <linux/gpio.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dapm.h>
27 #include <asm/mach-types.h>
29 #include <plat/regs-iis.h>
31 #include <mach/regs-clock.h>
33 #include <mach/gta02.h>
34 #include "../codecs/wm8753.h"
35 #include "s3c24xx-pcm.h"
36 #include "s3c24xx-i2s.h"
38 static struct snd_soc_card neo1973_gta02
;
40 static int neo1973_gta02_hifi_hw_params(struct snd_pcm_substream
*substream
,
41 struct snd_pcm_hw_params
*params
)
43 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
44 struct snd_soc_dai
*codec_dai
= rtd
->dai
->codec_dai
;
45 struct snd_soc_dai
*cpu_dai
= rtd
->dai
->cpu_dai
;
46 unsigned int pll_out
= 0, bclk
= 0;
48 unsigned long iis_clkrate
;
50 iis_clkrate
= s3c24xx_i2s_get_clockrate();
52 switch (params_rate(params
)) {
58 bclk
= WM8753_BCLK_DIV_4
;
62 bclk
= WM8753_BCLK_DIV_2
;
66 bclk
= WM8753_BCLK_DIV_16
;
70 bclk
= WM8753_BCLK_DIV_8
;
74 bclk
= WM8753_BCLK_DIV_4
;
78 bclk
= WM8753_BCLK_DIV_2
;
83 /* set codec DAI configuration */
84 ret
= snd_soc_dai_set_fmt(codec_dai
,
85 SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
86 SND_SOC_DAIFMT_CBM_CFM
);
90 /* set cpu DAI configuration */
91 ret
= snd_soc_dai_set_fmt(cpu_dai
,
92 SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
93 SND_SOC_DAIFMT_CBM_CFM
);
97 /* set the codec system clock for DAC and ADC */
98 ret
= snd_soc_dai_set_sysclk(codec_dai
, WM8753_MCLK
, pll_out
,
103 /* set MCLK division for sample rate */
104 ret
= snd_soc_dai_set_clkdiv(cpu_dai
, S3C24XX_DIV_MCLK
,
105 S3C2410_IISMOD_32FS
);
109 /* set codec BCLK division for sample rate */
110 ret
= snd_soc_dai_set_clkdiv(codec_dai
,
111 WM8753_BCLKDIV
, bclk
);
115 /* set prescaler division for sample rate */
116 ret
= snd_soc_dai_set_clkdiv(cpu_dai
, S3C24XX_DIV_PRESCALER
,
117 S3C24XX_PRESCALE(4, 4));
121 /* codec PLL input is PCLK/4 */
122 ret
= snd_soc_dai_set_pll(codec_dai
, WM8753_PLL1
,
123 iis_clkrate
/ 4, pll_out
);
130 static int neo1973_gta02_hifi_hw_free(struct snd_pcm_substream
*substream
)
132 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
133 struct snd_soc_dai
*codec_dai
= rtd
->dai
->codec_dai
;
135 /* disable the PLL */
136 return snd_soc_dai_set_pll(codec_dai
, WM8753_PLL1
, 0, 0);
140 * Neo1973 WM8753 HiFi DAI opserations.
142 static struct snd_soc_ops neo1973_gta02_hifi_ops
= {
143 .hw_params
= neo1973_gta02_hifi_hw_params
,
144 .hw_free
= neo1973_gta02_hifi_hw_free
,
147 static int neo1973_gta02_voice_hw_params(
148 struct snd_pcm_substream
*substream
,
149 struct snd_pcm_hw_params
*params
)
151 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
152 struct snd_soc_dai
*codec_dai
= rtd
->dai
->codec_dai
;
153 unsigned int pcmdiv
= 0;
155 unsigned long iis_clkrate
;
157 iis_clkrate
= s3c24xx_i2s_get_clockrate();
159 if (params_rate(params
) != 8000)
161 if (params_channels(params
) != 1)
164 pcmdiv
= WM8753_PCM_DIV_6
; /* 2.048 MHz */
166 /* todo: gg check mode (DSP_B) against CSR datasheet */
167 /* set codec DAI configuration */
168 ret
= snd_soc_dai_set_fmt(codec_dai
, SND_SOC_DAIFMT_DSP_B
|
169 SND_SOC_DAIFMT_NB_NF
| SND_SOC_DAIFMT_CBS_CFS
);
173 /* set the codec system clock for DAC and ADC */
174 ret
= snd_soc_dai_set_sysclk(codec_dai
, WM8753_PCMCLK
,
175 12288000, SND_SOC_CLOCK_IN
);
179 /* set codec PCM division for sample rate */
180 ret
= snd_soc_dai_set_clkdiv(codec_dai
, WM8753_PCMDIV
,
185 /* configue and enable PLL for 12.288MHz output */
186 ret
= snd_soc_dai_set_pll(codec_dai
, WM8753_PLL2
,
187 iis_clkrate
/ 4, 12288000);
194 static int neo1973_gta02_voice_hw_free(struct snd_pcm_substream
*substream
)
196 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
197 struct snd_soc_dai
*codec_dai
= rtd
->dai
->codec_dai
;
199 /* disable the PLL */
200 return snd_soc_dai_set_pll(codec_dai
, WM8753_PLL2
, 0, 0);
203 static struct snd_soc_ops neo1973_gta02_voice_ops
= {
204 .hw_params
= neo1973_gta02_voice_hw_params
,
205 .hw_free
= neo1973_gta02_voice_hw_free
,
211 static u8 lm4853_state
;
213 /* This has no effect, it exists only to maintain compatibility with
214 * existing ALSA state files.
216 static int lm4853_set_state(struct snd_kcontrol
*kcontrol
,
217 struct snd_ctl_elem_value
*ucontrol
)
219 int val
= ucontrol
->value
.integer
.value
[0];
222 lm4853_state
|= LM4853_AMP
;
224 lm4853_state
&= ~LM4853_AMP
;
229 static int lm4853_get_state(struct snd_kcontrol
*kcontrol
,
230 struct snd_ctl_elem_value
*ucontrol
)
232 ucontrol
->value
.integer
.value
[0] = lm4853_state
& LM4853_AMP
;
237 static int lm4853_set_spk(struct snd_kcontrol
*kcontrol
,
238 struct snd_ctl_elem_value
*ucontrol
)
240 int val
= ucontrol
->value
.integer
.value
[0];
243 lm4853_state
|= LM4853_SPK
;
244 gpio_set_value(GTA02_GPIO_HP_IN
, 0);
246 lm4853_state
&= ~LM4853_SPK
;
247 gpio_set_value(GTA02_GPIO_HP_IN
, 1);
253 static int lm4853_get_spk(struct snd_kcontrol
*kcontrol
,
254 struct snd_ctl_elem_value
*ucontrol
)
256 ucontrol
->value
.integer
.value
[0] = (lm4853_state
& LM4853_SPK
) >> 1;
261 static int lm4853_event(struct snd_soc_dapm_widget
*w
,
262 struct snd_kcontrol
*k
,
265 gpio_set_value(GTA02_GPIO_AMP_SHUT
, SND_SOC_DAPM_EVENT_OFF(value
));
270 static const struct snd_soc_dapm_widget wm8753_dapm_widgets
[] = {
271 SND_SOC_DAPM_SPK("Stereo Out", lm4853_event
),
272 SND_SOC_DAPM_LINE("GSM Line Out", NULL
),
273 SND_SOC_DAPM_LINE("GSM Line In", NULL
),
274 SND_SOC_DAPM_MIC("Headset Mic", NULL
),
275 SND_SOC_DAPM_MIC("Handset Mic", NULL
),
276 SND_SOC_DAPM_SPK("Handset Spk", NULL
),
280 /* example machine audio_mapnections */
281 static const struct snd_soc_dapm_route audio_map
[] = {
283 /* Connections to the lm4853 amp */
284 {"Stereo Out", NULL
, "LOUT1"},
285 {"Stereo Out", NULL
, "ROUT1"},
287 /* Connections to the GSM Module */
288 {"GSM Line Out", NULL
, "MONO1"},
289 {"GSM Line Out", NULL
, "MONO2"},
290 {"RXP", NULL
, "GSM Line In"},
291 {"RXN", NULL
, "GSM Line In"},
293 /* Connections to Headset */
294 {"MIC1", NULL
, "Mic Bias"},
295 {"Mic Bias", NULL
, "Headset Mic"},
298 {"MIC2", NULL
, "Mic Bias"},
299 {"MIC2N", NULL
, "Mic Bias"},
300 {"Mic Bias", NULL
, "Handset Mic"},
303 {"Handset Spk", NULL
, "LOUT2"},
304 {"Handset Spk", NULL
, "ROUT2"},
306 /* Connect the ALC pins */
307 {"ACIN", NULL
, "ACOP"},
310 static const struct snd_kcontrol_new wm8753_neo1973_gta02_controls
[] = {
311 SOC_DAPM_PIN_SWITCH("Stereo Out"),
312 SOC_DAPM_PIN_SWITCH("GSM Line Out"),
313 SOC_DAPM_PIN_SWITCH("GSM Line In"),
314 SOC_DAPM_PIN_SWITCH("Headset Mic"),
315 SOC_DAPM_PIN_SWITCH("Handset Mic"),
316 SOC_DAPM_PIN_SWITCH("Handset Spk"),
318 /* This has no effect, it exists only to maintain compatibility with
319 * existing ALSA state files.
321 SOC_SINGLE_EXT("Amp State Switch", 6, 0, 1, 0,
324 SOC_SINGLE_EXT("Amp Spk Switch", 7, 0, 1, 0,
330 * This is an example machine initialisation for a wm8753 connected to a
333 static int neo1973_gta02_wm8753_init(struct snd_soc_codec
*codec
)
337 /* set up NC codec pins */
338 snd_soc_dapm_nc_pin(codec
, "OUT3");
339 snd_soc_dapm_nc_pin(codec
, "OUT4");
340 snd_soc_dapm_nc_pin(codec
, "LINE1");
341 snd_soc_dapm_nc_pin(codec
, "LINE2");
343 /* Add neo1973 gta02 specific widgets */
344 snd_soc_dapm_new_controls(codec
, wm8753_dapm_widgets
,
345 ARRAY_SIZE(wm8753_dapm_widgets
));
347 /* add neo1973 gta02 specific controls */
348 err
= snd_soc_add_controls(codec
, wm8753_neo1973_gta02_controls
,
349 ARRAY_SIZE(wm8753_neo1973_gta02_controls
));
354 /* set up neo1973 gta02 specific audio path audio_map */
355 snd_soc_dapm_add_routes(codec
, audio_map
, ARRAY_SIZE(audio_map
));
357 /* set endpoints to default off mode */
358 snd_soc_dapm_disable_pin(codec
, "Stereo Out");
359 snd_soc_dapm_disable_pin(codec
, "GSM Line Out");
360 snd_soc_dapm_disable_pin(codec
, "GSM Line In");
361 snd_soc_dapm_disable_pin(codec
, "Headset Mic");
362 snd_soc_dapm_disable_pin(codec
, "Handset Mic");
363 snd_soc_dapm_disable_pin(codec
, "Handset Spk");
365 snd_soc_dapm_sync(codec
);
373 static struct snd_soc_dai bt_dai
= {
379 .rates
= SNDRV_PCM_RATE_8000
,
380 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,},
384 .rates
= SNDRV_PCM_RATE_8000
,
385 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,},
388 static struct snd_soc_dai_link neo1973_gta02_dai
[] = {
389 { /* Hifi Playback - for similatious use with voice below */
391 .stream_name
= "WM8753 HiFi",
392 .cpu_dai
= &s3c24xx_i2s_dai
,
393 .codec_dai
= &wm8753_dai
[WM8753_DAI_HIFI
],
394 .init
= neo1973_gta02_wm8753_init
,
395 .ops
= &neo1973_gta02_hifi_ops
,
399 .stream_name
= "Voice",
401 .codec_dai
= &wm8753_dai
[WM8753_DAI_VOICE
],
402 .ops
= &neo1973_gta02_voice_ops
,
406 static struct snd_soc_card neo1973_gta02
= {
407 .name
= "neo1973-gta02",
408 .platform
= &s3c24xx_soc_platform
,
409 .dai_link
= neo1973_gta02_dai
,
410 .num_links
= ARRAY_SIZE(neo1973_gta02_dai
),
413 static struct snd_soc_device neo1973_gta02_snd_devdata
= {
414 .card
= &neo1973_gta02
,
415 .codec_dev
= &soc_codec_dev_wm8753
,
418 static struct platform_device
*neo1973_gta02_snd_device
;
420 static int __init
neo1973_gta02_init(void)
424 if (!machine_is_neo1973_gta02()) {
426 "Only GTA02 is supported by this ASoC driver\n");
430 /* register bluetooth DAI here */
431 ret
= snd_soc_register_dai(&bt_dai
);
435 neo1973_gta02_snd_device
= platform_device_alloc("soc-audio", -1);
436 if (!neo1973_gta02_snd_device
)
439 platform_set_drvdata(neo1973_gta02_snd_device
,
440 &neo1973_gta02_snd_devdata
);
441 neo1973_gta02_snd_devdata
.dev
= &neo1973_gta02_snd_device
->dev
;
442 ret
= platform_device_add(neo1973_gta02_snd_device
);
445 platform_device_put(neo1973_gta02_snd_device
);
449 /* Initialise GPIOs used by amp */
450 ret
= gpio_request(GTA02_GPIO_HP_IN
, "GTA02_HP_IN");
452 pr_err("gta02_wm8753: Failed to register GPIO %d\n", GTA02_GPIO_HP_IN
);
453 goto err_unregister_device
;
456 ret
= gpio_direction_output(GTA02_GPIO_AMP_HP_IN
, 1);
458 pr_err("gta02_wm8753: Failed to configure GPIO %d\n", GTA02_GPIO_HP_IN
);
459 goto err_free_gpio_hp_in
;
462 ret
= gpio_request(GTA02_GPIO_AMP_SHUT
, "GTA02_AMP_SHUT");
464 pr_err("gta02_wm8753: Failed to register GPIO %d\n", GTA02_GPIO_AMP_SHUT
);
465 goto err_free_gpio_hp_in
;
468 ret
= gpio_direction_output(GTA02_GPIO_AMP_SHUT
, 1);
470 pr_err("gta02_wm8753: Failed to configure GPIO %d\n", GTA02_GPIO_AMP_SHUT
);
471 goto err_free_gpio_amp_shut
;
476 err_free_gpio_amp_shut
:
477 gpio_free(GTA02_GPIO_AMP_SHUT
);
479 gpio_free(GTA02_GPIO_HP_IN
);
480 err_unregister_device
:
481 platform_device_unregister(neo1973_gta02_snd_device
);
484 module_init(neo1973_gta02_init
);
486 static void __exit
neo1973_gta02_exit(void)
488 snd_soc_unregister_dai(&bt_dai
);
489 platform_device_unregister(neo1973_gta02_snd_device
);
490 gpio_free(GTA02_GPIO_HP_IN
);
491 gpio_free(GTA02_GPIO_AMP_SHUT
);
493 module_exit(neo1973_gta02_exit
);
495 /* Module information */
496 MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org");
497 MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 GTA02");
498 MODULE_LICENSE("GPL");