arm: socfpga: Enable ARM_TWD for socfpga
[linux-2.6.git] / sound / soc / samsung / neo1973_wm8753.c
blob807db417d234a5d3571f6ff6be86893acad514d9
1 /*
2 * neo1973_wm8753.c -- SoC audio for Openmoko Neo1973 and Freerunner devices
4 * Copyright 2007 Openmoko Inc
5 * Author: Graeme Gregory <graeme@openmoko.org>
6 * Copyright 2007 Wolfson Microelectronics PLC.
7 * Author: Graeme Gregory
8 * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
9 * Copyright 2009 Wolfson Microelectronics
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/gpio.h>
21 #include <sound/soc.h>
23 #include <asm/mach-types.h>
24 #include "regs-iis.h"
26 #include "../codecs/wm8753.h"
27 #include "s3c24xx-i2s.h"
29 static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
30 struct snd_pcm_hw_params *params)
32 struct snd_soc_pcm_runtime *rtd = substream->private_data;
33 struct snd_soc_dai *codec_dai = rtd->codec_dai;
34 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
35 unsigned int pll_out = 0, bclk = 0;
36 int ret = 0;
37 unsigned long iis_clkrate;
39 iis_clkrate = s3c24xx_i2s_get_clockrate();
41 switch (params_rate(params)) {
42 case 8000:
43 case 16000:
44 pll_out = 12288000;
45 break;
46 case 48000:
47 bclk = WM8753_BCLK_DIV_4;
48 pll_out = 12288000;
49 break;
50 case 96000:
51 bclk = WM8753_BCLK_DIV_2;
52 pll_out = 12288000;
53 break;
54 case 11025:
55 bclk = WM8753_BCLK_DIV_16;
56 pll_out = 11289600;
57 break;
58 case 22050:
59 bclk = WM8753_BCLK_DIV_8;
60 pll_out = 11289600;
61 break;
62 case 44100:
63 bclk = WM8753_BCLK_DIV_4;
64 pll_out = 11289600;
65 break;
66 case 88200:
67 bclk = WM8753_BCLK_DIV_2;
68 pll_out = 11289600;
69 break;
72 /* set codec DAI configuration */
73 ret = snd_soc_dai_set_fmt(codec_dai,
74 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
75 SND_SOC_DAIFMT_CBM_CFM);
76 if (ret < 0)
77 return ret;
79 /* set cpu DAI configuration */
80 ret = snd_soc_dai_set_fmt(cpu_dai,
81 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
82 SND_SOC_DAIFMT_CBM_CFM);
83 if (ret < 0)
84 return ret;
86 /* set the codec system clock for DAC and ADC */
87 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
88 SND_SOC_CLOCK_IN);
89 if (ret < 0)
90 return ret;
92 /* set MCLK division for sample rate */
93 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
94 S3C2410_IISMOD_32FS);
95 if (ret < 0)
96 return ret;
98 /* set codec BCLK division for sample rate */
99 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
100 if (ret < 0)
101 return ret;
103 /* set prescaler division for sample rate */
104 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
105 S3C24XX_PRESCALE(4, 4));
106 if (ret < 0)
107 return ret;
109 /* codec PLL input is PCLK/4 */
110 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
111 iis_clkrate / 4, pll_out);
112 if (ret < 0)
113 return ret;
115 return 0;
118 static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
120 struct snd_soc_pcm_runtime *rtd = substream->private_data;
121 struct snd_soc_dai *codec_dai = rtd->codec_dai;
123 /* disable the PLL */
124 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0, 0);
128 * Neo1973 WM8753 HiFi DAI opserations.
130 static struct snd_soc_ops neo1973_hifi_ops = {
131 .hw_params = neo1973_hifi_hw_params,
132 .hw_free = neo1973_hifi_hw_free,
135 static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
136 struct snd_pcm_hw_params *params)
138 struct snd_soc_pcm_runtime *rtd = substream->private_data;
139 struct snd_soc_dai *codec_dai = rtd->codec_dai;
140 unsigned int pcmdiv = 0;
141 int ret = 0;
142 unsigned long iis_clkrate;
144 iis_clkrate = s3c24xx_i2s_get_clockrate();
146 if (params_rate(params) != 8000)
147 return -EINVAL;
148 if (params_channels(params) != 1)
149 return -EINVAL;
151 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
153 /* todo: gg check mode (DSP_B) against CSR datasheet */
154 /* set codec DAI configuration */
155 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
156 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
157 if (ret < 0)
158 return ret;
160 /* set the codec system clock for DAC and ADC */
161 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
162 SND_SOC_CLOCK_IN);
163 if (ret < 0)
164 return ret;
166 /* set codec PCM division for sample rate */
167 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
168 if (ret < 0)
169 return ret;
171 /* configure and enable PLL for 12.288MHz output */
172 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
173 iis_clkrate / 4, 12288000);
174 if (ret < 0)
175 return ret;
177 return 0;
180 static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
182 struct snd_soc_pcm_runtime *rtd = substream->private_data;
183 struct snd_soc_dai *codec_dai = rtd->codec_dai;
185 /* disable the PLL */
186 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0, 0);
189 static struct snd_soc_ops neo1973_voice_ops = {
190 .hw_params = neo1973_voice_hw_params,
191 .hw_free = neo1973_voice_hw_free,
194 /* Shared routes and controls */
196 static const struct snd_soc_dapm_widget neo1973_wm8753_dapm_widgets[] = {
197 SND_SOC_DAPM_LINE("GSM Line Out", NULL),
198 SND_SOC_DAPM_LINE("GSM Line In", NULL),
199 SND_SOC_DAPM_MIC("Headset Mic", NULL),
200 SND_SOC_DAPM_MIC("Handset Mic", NULL),
203 static const struct snd_soc_dapm_route neo1973_wm8753_routes[] = {
204 /* Connections to the GSM Module */
205 {"GSM Line Out", NULL, "MONO1"},
206 {"GSM Line Out", NULL, "MONO2"},
207 {"RXP", NULL, "GSM Line In"},
208 {"RXN", NULL, "GSM Line In"},
210 /* Connections to Headset */
211 {"MIC1", NULL, "Mic Bias"},
212 {"Mic Bias", NULL, "Headset Mic"},
214 /* Call Mic */
215 {"MIC2", NULL, "Mic Bias"},
216 {"MIC2N", NULL, "Mic Bias"},
217 {"Mic Bias", NULL, "Handset Mic"},
219 /* Connect the ALC pins */
220 {"ACIN", NULL, "ACOP"},
223 static const struct snd_kcontrol_new neo1973_wm8753_controls[] = {
224 SOC_DAPM_PIN_SWITCH("GSM Line Out"),
225 SOC_DAPM_PIN_SWITCH("GSM Line In"),
226 SOC_DAPM_PIN_SWITCH("Headset Mic"),
227 SOC_DAPM_PIN_SWITCH("Handset Mic"),
230 /* GTA02 specific routes and controls */
232 static int gta02_speaker_enabled;
234 static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
235 struct snd_ctl_elem_value *ucontrol)
237 gta02_speaker_enabled = ucontrol->value.integer.value[0];
239 gpio_set_value(S3C2410_GPJ(2), !gta02_speaker_enabled);
241 return 0;
244 static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
245 struct snd_ctl_elem_value *ucontrol)
247 ucontrol->value.integer.value[0] = gta02_speaker_enabled;
248 return 0;
251 static int lm4853_event(struct snd_soc_dapm_widget *w,
252 struct snd_kcontrol *k, int event)
254 gpio_set_value(S3C2410_GPJ(1), SND_SOC_DAPM_EVENT_OFF(event));
256 return 0;
259 static const struct snd_soc_dapm_route neo1973_gta02_routes[] = {
260 /* Connections to the amp */
261 {"Stereo Out", NULL, "LOUT1"},
262 {"Stereo Out", NULL, "ROUT1"},
264 /* Call Speaker */
265 {"Handset Spk", NULL, "LOUT2"},
266 {"Handset Spk", NULL, "ROUT2"},
269 static const struct snd_kcontrol_new neo1973_gta02_wm8753_controls[] = {
270 SOC_DAPM_PIN_SWITCH("Handset Spk"),
271 SOC_DAPM_PIN_SWITCH("Stereo Out"),
273 SOC_SINGLE_BOOL_EXT("Amp Spk Switch", 0,
274 lm4853_get_spk,
275 lm4853_set_spk),
278 static const struct snd_soc_dapm_widget neo1973_gta02_wm8753_dapm_widgets[] = {
279 SND_SOC_DAPM_SPK("Handset Spk", NULL),
280 SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
283 static int neo1973_gta02_wm8753_init(struct snd_soc_codec *codec)
285 struct snd_soc_dapm_context *dapm = &codec->dapm;
286 int ret;
288 ret = snd_soc_dapm_new_controls(dapm, neo1973_gta02_wm8753_dapm_widgets,
289 ARRAY_SIZE(neo1973_gta02_wm8753_dapm_widgets));
290 if (ret)
291 return ret;
293 ret = snd_soc_dapm_add_routes(dapm, neo1973_gta02_routes,
294 ARRAY_SIZE(neo1973_gta02_routes));
295 if (ret)
296 return ret;
298 ret = snd_soc_add_card_controls(codec->card, neo1973_gta02_wm8753_controls,
299 ARRAY_SIZE(neo1973_gta02_wm8753_controls));
300 if (ret)
301 return ret;
303 snd_soc_dapm_disable_pin(dapm, "Stereo Out");
304 snd_soc_dapm_disable_pin(dapm, "Handset Spk");
305 snd_soc_dapm_ignore_suspend(dapm, "Stereo Out");
306 snd_soc_dapm_ignore_suspend(dapm, "Handset Spk");
308 return 0;
311 static int neo1973_wm8753_init(struct snd_soc_pcm_runtime *rtd)
313 struct snd_soc_codec *codec = rtd->codec;
314 struct snd_soc_dapm_context *dapm = &codec->dapm;
315 int ret;
317 /* set up NC codec pins */
318 snd_soc_dapm_nc_pin(dapm, "OUT3");
319 snd_soc_dapm_nc_pin(dapm, "OUT4");
320 snd_soc_dapm_nc_pin(dapm, "LINE1");
321 snd_soc_dapm_nc_pin(dapm, "LINE2");
323 /* Add neo1973 specific widgets */
324 ret = snd_soc_dapm_new_controls(dapm, neo1973_wm8753_dapm_widgets,
325 ARRAY_SIZE(neo1973_wm8753_dapm_widgets));
326 if (ret)
327 return ret;
329 /* add neo1973 specific controls */
330 ret = snd_soc_add_card_controls(rtd->card, neo1973_wm8753_controls,
331 ARRAY_SIZE(neo1973_wm8753_controls));
332 if (ret)
333 return ret;
335 /* set up neo1973 specific audio routes */
336 ret = snd_soc_dapm_add_routes(dapm, neo1973_wm8753_routes,
337 ARRAY_SIZE(neo1973_wm8753_routes));
338 if (ret)
339 return ret;
341 /* set endpoints to default off mode */
342 snd_soc_dapm_disable_pin(dapm, "GSM Line Out");
343 snd_soc_dapm_disable_pin(dapm, "GSM Line In");
344 snd_soc_dapm_disable_pin(dapm, "Headset Mic");
345 snd_soc_dapm_disable_pin(dapm, "Handset Mic");
347 /* allow audio paths from the GSM modem to run during suspend */
348 snd_soc_dapm_ignore_suspend(dapm, "GSM Line Out");
349 snd_soc_dapm_ignore_suspend(dapm, "GSM Line In");
350 snd_soc_dapm_ignore_suspend(dapm, "Headset Mic");
351 snd_soc_dapm_ignore_suspend(dapm, "Handset Mic");
353 if (machine_is_neo1973_gta02()) {
354 ret = neo1973_gta02_wm8753_init(codec);
355 if (ret)
356 return ret;
359 return 0;
362 static struct snd_soc_dai_link neo1973_dai[] = {
363 { /* Hifi Playback - for similatious use with voice below */
364 .name = "WM8753",
365 .stream_name = "WM8753 HiFi",
366 .platform_name = "s3c24xx-iis",
367 .cpu_dai_name = "s3c24xx-iis",
368 .codec_dai_name = "wm8753-hifi",
369 .codec_name = "wm8753.0-001a",
370 .init = neo1973_wm8753_init,
371 .ops = &neo1973_hifi_ops,
373 { /* Voice via BT */
374 .name = "Bluetooth",
375 .stream_name = "Voice",
376 .cpu_dai_name = "bt-sco-pcm",
377 .codec_dai_name = "wm8753-voice",
378 .codec_name = "wm8753.0-001a",
379 .ops = &neo1973_voice_ops,
383 static struct snd_soc_aux_dev neo1973_aux_devs[] = {
385 .name = "dfbmcs320",
386 .codec_name = "dfbmcs320.0",
390 static struct snd_soc_codec_conf neo1973_codec_conf[] = {
392 .dev_name = "lm4857.0-007c",
393 .name_prefix = "Amp",
397 static const struct gpio neo1973_gta02_gpios[] = {
398 { S3C2410_GPJ(2), GPIOF_OUT_INIT_HIGH, "GTA02_HP_IN" },
399 { S3C2410_GPJ(1), GPIOF_OUT_INIT_HIGH, "GTA02_AMP_SHUT" },
402 static struct snd_soc_card neo1973 = {
403 .name = "neo1973",
404 .owner = THIS_MODULE,
405 .dai_link = neo1973_dai,
406 .num_links = ARRAY_SIZE(neo1973_dai),
407 .aux_dev = neo1973_aux_devs,
408 .num_aux_devs = ARRAY_SIZE(neo1973_aux_devs),
409 .codec_conf = neo1973_codec_conf,
410 .num_configs = ARRAY_SIZE(neo1973_codec_conf),
413 static struct platform_device *neo1973_snd_device;
415 static int __init neo1973_init(void)
417 int ret;
419 if (!machine_is_neo1973_gta02())
420 return -ENODEV;
422 if (machine_is_neo1973_gta02()) {
423 neo1973.name = "neo1973gta02";
424 neo1973.num_aux_devs = 1;
426 ret = gpio_request_array(neo1973_gta02_gpios,
427 ARRAY_SIZE(neo1973_gta02_gpios));
428 if (ret)
429 return ret;
432 neo1973_snd_device = platform_device_alloc("soc-audio", -1);
433 if (!neo1973_snd_device) {
434 ret = -ENOMEM;
435 goto err_gpio_free;
438 platform_set_drvdata(neo1973_snd_device, &neo1973);
439 ret = platform_device_add(neo1973_snd_device);
441 if (ret)
442 goto err_put_device;
444 return 0;
446 err_put_device:
447 platform_device_put(neo1973_snd_device);
448 err_gpio_free:
449 if (machine_is_neo1973_gta02()) {
450 gpio_free_array(neo1973_gta02_gpios,
451 ARRAY_SIZE(neo1973_gta02_gpios));
453 return ret;
455 module_init(neo1973_init);
457 static void __exit neo1973_exit(void)
459 platform_device_unregister(neo1973_snd_device);
461 if (machine_is_neo1973_gta02()) {
462 gpio_free_array(neo1973_gta02_gpios,
463 ARRAY_SIZE(neo1973_gta02_gpios));
466 module_exit(neo1973_exit);
468 /* Module information */
469 MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
470 MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 and Frerunner");
471 MODULE_LICENSE("GPL");