ASoC: Decouple DAPM from CODECs
[deliverable/linux.git] / sound / soc / s3c24xx / neo1973_gta02_wm8753.c
CommitLineData
606689e9 1/*
82c4362e 2 * neo1973_gta02_wm8753.c -- SoC audio for Openmoko Freerunner(GTA02)
606689e9
MB
3 *
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
9 *
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.
14 */
15
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>
82c4362e 21#include <linux/gpio.h>
606689e9
MB
22#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/soc.h>
25#include <sound/soc-dapm.h>
26
27#include <asm/mach-types.h>
28
29#include <plat/regs-iis.h>
30
31#include <mach/regs-clock.h>
606689e9 32#include <asm/io.h>
606689e9
MB
33#include <mach/gta02.h>
34#include "../codecs/wm8753.h"
d3ff5a3e 35#include "s3c-dma.h"
606689e9
MB
36#include "s3c24xx-i2s.h"
37
38static struct snd_soc_card neo1973_gta02;
39
40static int neo1973_gta02_hifi_hw_params(struct snd_pcm_substream *substream,
41 struct snd_pcm_hw_params *params)
42{
43 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad
LG
44 struct snd_soc_dai *codec_dai = rtd->codec_dai;
45 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
606689e9
MB
46 unsigned int pll_out = 0, bclk = 0;
47 int ret = 0;
48 unsigned long iis_clkrate;
49
50 iis_clkrate = s3c24xx_i2s_get_clockrate();
51
52 switch (params_rate(params)) {
53 case 8000:
54 case 16000:
55 pll_out = 12288000;
56 break;
57 case 48000:
58 bclk = WM8753_BCLK_DIV_4;
59 pll_out = 12288000;
60 break;
61 case 96000:
62 bclk = WM8753_BCLK_DIV_2;
63 pll_out = 12288000;
64 break;
65 case 11025:
66 bclk = WM8753_BCLK_DIV_16;
67 pll_out = 11289600;
68 break;
69 case 22050:
70 bclk = WM8753_BCLK_DIV_8;
71 pll_out = 11289600;
72 break;
73 case 44100:
74 bclk = WM8753_BCLK_DIV_4;
75 pll_out = 11289600;
76 break;
77 case 88200:
78 bclk = WM8753_BCLK_DIV_2;
79 pll_out = 11289600;
80 break;
81 }
82
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);
87 if (ret < 0)
88 return ret;
89
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);
94 if (ret < 0)
95 return ret;
96
97 /* set the codec system clock for DAC and ADC */
98 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
99 SND_SOC_CLOCK_IN);
100 if (ret < 0)
101 return ret;
102
103 /* set MCLK division for sample rate */
104 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
105 S3C2410_IISMOD_32FS);
106 if (ret < 0)
107 return ret;
108
109 /* set codec BCLK division for sample rate */
110 ret = snd_soc_dai_set_clkdiv(codec_dai,
111 WM8753_BCLKDIV, bclk);
112 if (ret < 0)
113 return ret;
114
115 /* set prescaler division for sample rate */
116 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
117 S3C24XX_PRESCALE(4, 4));
118 if (ret < 0)
119 return ret;
120
121 /* codec PLL input is PCLK/4 */
85488037 122 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
606689e9
MB
123 iis_clkrate / 4, pll_out);
124 if (ret < 0)
125 return ret;
126
127 return 0;
128}
129
130static int neo1973_gta02_hifi_hw_free(struct snd_pcm_substream *substream)
131{
132 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 133 struct snd_soc_dai *codec_dai = rtd->codec_dai;
606689e9
MB
134
135 /* disable the PLL */
140318aa 136 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0, 0);
606689e9
MB
137}
138
139/*
140 * Neo1973 WM8753 HiFi DAI opserations.
141 */
142static struct snd_soc_ops neo1973_gta02_hifi_ops = {
143 .hw_params = neo1973_gta02_hifi_hw_params,
144 .hw_free = neo1973_gta02_hifi_hw_free,
145};
146
147static int neo1973_gta02_voice_hw_params(
148 struct snd_pcm_substream *substream,
149 struct snd_pcm_hw_params *params)
150{
151 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 152 struct snd_soc_dai *codec_dai = rtd->codec_dai;
606689e9
MB
153 unsigned int pcmdiv = 0;
154 int ret = 0;
155 unsigned long iis_clkrate;
156
157 iis_clkrate = s3c24xx_i2s_get_clockrate();
158
159 if (params_rate(params) != 8000)
160 return -EINVAL;
161 if (params_channels(params) != 1)
162 return -EINVAL;
163
164 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
165
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);
170 if (ret < 0)
171 return ret;
172
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);
176 if (ret < 0)
177 return ret;
178
179 /* set codec PCM division for sample rate */
180 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV,
181 pcmdiv);
182 if (ret < 0)
183 return ret;
184
fa2eb005 185 /* configure and enable PLL for 12.288MHz output */
140318aa 186 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
606689e9
MB
187 iis_clkrate / 4, 12288000);
188 if (ret < 0)
189 return ret;
190
191 return 0;
192}
193
194static int neo1973_gta02_voice_hw_free(struct snd_pcm_substream *substream)
195{
196 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 197 struct snd_soc_dai *codec_dai = rtd->codec_dai;
606689e9
MB
198
199 /* disable the PLL */
140318aa 200 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0, 0);
606689e9
MB
201}
202
203static struct snd_soc_ops neo1973_gta02_voice_ops = {
204 .hw_params = neo1973_gta02_voice_hw_params,
205 .hw_free = neo1973_gta02_voice_hw_free,
206};
207
208#define LM4853_AMP 1
209#define LM4853_SPK 2
210
211static u8 lm4853_state;
212
213/* This has no effect, it exists only to maintain compatibility with
214 * existing ALSA state files.
215 */
216static int lm4853_set_state(struct snd_kcontrol *kcontrol,
217 struct snd_ctl_elem_value *ucontrol)
218{
219 int val = ucontrol->value.integer.value[0];
220
221 if (val)
222 lm4853_state |= LM4853_AMP;
223 else
224 lm4853_state &= ~LM4853_AMP;
225
226 return 0;
227}
228
229static int lm4853_get_state(struct snd_kcontrol *kcontrol,
230 struct snd_ctl_elem_value *ucontrol)
231{
232 ucontrol->value.integer.value[0] = lm4853_state & LM4853_AMP;
233
234 return 0;
235}
236
237static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
238 struct snd_ctl_elem_value *ucontrol)
239{
240 int val = ucontrol->value.integer.value[0];
241
242 if (val) {
243 lm4853_state |= LM4853_SPK;
82c4362e 244 gpio_set_value(GTA02_GPIO_HP_IN, 0);
606689e9
MB
245 } else {
246 lm4853_state &= ~LM4853_SPK;
82c4362e 247 gpio_set_value(GTA02_GPIO_HP_IN, 1);
606689e9
MB
248 }
249
250 return 0;
251}
252
253static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
254 struct snd_ctl_elem_value *ucontrol)
255{
256 ucontrol->value.integer.value[0] = (lm4853_state & LM4853_SPK) >> 1;
257
258 return 0;
259}
260
261static int lm4853_event(struct snd_soc_dapm_widget *w,
262 struct snd_kcontrol *k,
263 int event)
264{
f0fba2ad 265 gpio_set_value(GTA02_GPIO_AMP_SHUT, SND_SOC_DAPM_EVENT_OFF(event));
606689e9
MB
266
267 return 0;
268}
269
270static 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),
277};
278
279
280/* example machine audio_mapnections */
281static const struct snd_soc_dapm_route audio_map[] = {
282
283 /* Connections to the lm4853 amp */
284 {"Stereo Out", NULL, "LOUT1"},
285 {"Stereo Out", NULL, "ROUT1"},
286
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"},
292
293 /* Connections to Headset */
294 {"MIC1", NULL, "Mic Bias"},
295 {"Mic Bias", NULL, "Headset Mic"},
296
297 /* Call Mic */
298 {"MIC2", NULL, "Mic Bias"},
299 {"MIC2N", NULL, "Mic Bias"},
300 {"Mic Bias", NULL, "Handset Mic"},
301
302 /* Call Speaker */
303 {"Handset Spk", NULL, "LOUT2"},
304 {"Handset Spk", NULL, "ROUT2"},
305
306 /* Connect the ALC pins */
307 {"ACIN", NULL, "ACOP"},
308};
309
310static 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"),
317
318 /* This has no effect, it exists only to maintain compatibility with
319 * existing ALSA state files.
320 */
321 SOC_SINGLE_EXT("Amp State Switch", 6, 0, 1, 0,
322 lm4853_get_state,
323 lm4853_set_state),
324 SOC_SINGLE_EXT("Amp Spk Switch", 7, 0, 1, 0,
325 lm4853_get_spk,
326 lm4853_set_spk),
327};
328
329/*
330 * This is an example machine initialisation for a wm8753 connected to a
331 * neo1973 GTA02.
332 */
f0fba2ad 333static int neo1973_gta02_wm8753_init(struct snd_soc_pcm_runtime *rtd)
606689e9 334{
f0fba2ad 335 struct snd_soc_codec *codec = rtd->codec;
ce6120cc 336 struct snd_soc_dapm_context *dapm = &codec->dapm;
69331fbd 337 int err;
606689e9
MB
338
339 /* set up NC codec pins */
ce6120cc
LG
340 snd_soc_dapm_nc_pin(dapm, "OUT3");
341 snd_soc_dapm_nc_pin(dapm, "OUT4");
342 snd_soc_dapm_nc_pin(dapm, "LINE1");
343 snd_soc_dapm_nc_pin(dapm, "LINE2");
606689e9
MB
344
345 /* Add neo1973 gta02 specific widgets */
ce6120cc 346 snd_soc_dapm_new_controls(dapm, wm8753_dapm_widgets,
606689e9
MB
347 ARRAY_SIZE(wm8753_dapm_widgets));
348
349 /* add neo1973 gta02 specific controls */
69331fbd
LPC
350 err = snd_soc_add_controls(codec, wm8753_neo1973_gta02_controls,
351 ARRAY_SIZE(wm8753_neo1973_gta02_controls));
352
353 if (err < 0)
354 return err;
606689e9
MB
355
356 /* set up neo1973 gta02 specific audio path audio_map */
ce6120cc 357 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
606689e9
MB
358
359 /* set endpoints to default off mode */
ce6120cc
LG
360 snd_soc_dapm_disable_pin(dapm, "Stereo Out");
361 snd_soc_dapm_disable_pin(dapm, "GSM Line Out");
362 snd_soc_dapm_disable_pin(dapm, "GSM Line In");
363 snd_soc_dapm_disable_pin(dapm, "Headset Mic");
364 snd_soc_dapm_disable_pin(dapm, "Handset Mic");
365 snd_soc_dapm_disable_pin(dapm, "Handset Spk");
606689e9 366
452a5fd6 367 /* allow audio paths from the GSM modem to run during suspend */
ce6120cc
LG
368 snd_soc_dapm_ignore_suspend(dapm, "Stereo Out");
369 snd_soc_dapm_ignore_suspend(dapm, "GSM Line Out");
370 snd_soc_dapm_ignore_suspend(dapm, "GSM Line In");
371 snd_soc_dapm_ignore_suspend(dapm, "Headset Mic");
372 snd_soc_dapm_ignore_suspend(dapm, "Handset Mic");
373 snd_soc_dapm_ignore_suspend(dapm, "Handset Spk");
374
375 snd_soc_dapm_sync(dapm);
606689e9
MB
376
377 return 0;
378}
379
380/*
381 * BT Codec DAI
382 */
f0fba2ad
LG
383static struct snd_soc_dai_driver bt_dai = {
384 .name = "bluetooth-dai",
606689e9
MB
385 .playback = {
386 .channels_min = 1,
387 .channels_max = 1,
388 .rates = SNDRV_PCM_RATE_8000,
389 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
390 .capture = {
391 .channels_min = 1,
392 .channels_max = 1,
393 .rates = SNDRV_PCM_RATE_8000,
394 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
395};
396
397static struct snd_soc_dai_link neo1973_gta02_dai[] = {
398{ /* Hifi Playback - for similatious use with voice below */
399 .name = "WM8753",
400 .stream_name = "WM8753 HiFi",
f0fba2ad
LG
401 .cpu_dai_name = "s3c24xx-i2s",
402 .codec_dai_name = "wm8753-hifi",
606689e9 403 .init = neo1973_gta02_wm8753_init,
f0fba2ad
LG
404 .platform_name = "s3c24xx-pcm-audio",
405 .codec_name = "wm8753-codec.0-0x1a",
606689e9
MB
406 .ops = &neo1973_gta02_hifi_ops,
407},
408{ /* Voice via BT */
409 .name = "Bluetooth",
410 .stream_name = "Voice",
f0fba2ad
LG
411 .cpu_dai_name = "bluetooth-dai",
412 .codec_dai_name = "wm8753-voice",
606689e9 413 .ops = &neo1973_gta02_voice_ops,
f0fba2ad
LG
414 .codec_name = "wm8753-codec.0-0x1a",
415 .platform_name = "s3c24xx-pcm-audio",
606689e9
MB
416},
417};
418
419static struct snd_soc_card neo1973_gta02 = {
420 .name = "neo1973-gta02",
606689e9
MB
421 .dai_link = neo1973_gta02_dai,
422 .num_links = ARRAY_SIZE(neo1973_gta02_dai),
423};
424
606689e9
MB
425static struct platform_device *neo1973_gta02_snd_device;
426
427static int __init neo1973_gta02_init(void)
428{
429 int ret;
430
431 if (!machine_is_neo1973_gta02()) {
432 printk(KERN_INFO
433 "Only GTA02 is supported by this ASoC driver\n");
434 return -ENODEV;
435 }
436
606689e9
MB
437 neo1973_gta02_snd_device = platform_device_alloc("soc-audio", -1);
438 if (!neo1973_gta02_snd_device)
439 return -ENOMEM;
440
f0fba2ad
LG
441 /* register bluetooth DAI here */
442 ret = snd_soc_register_dai(&neo1973_gta02_snd_device->dev, -1, &bt_dai);
443 if (ret) {
444 platform_device_put(neo1973_gta02_snd_device);
445 return ret;
446 }
447
448 platform_set_drvdata(neo1973_gta02_snd_device, &neo1973_gta02);
606689e9
MB
449 ret = platform_device_add(neo1973_gta02_snd_device);
450
451 if (ret) {
452 platform_device_put(neo1973_gta02_snd_device);
453 return ret;
454 }
455
456 /* Initialise GPIOs used by amp */
82c4362e
LPC
457 ret = gpio_request(GTA02_GPIO_HP_IN, "GTA02_HP_IN");
458 if (ret) {
459 pr_err("gta02_wm8753: Failed to register GPIO %d\n", GTA02_GPIO_HP_IN);
460 goto err_unregister_device;
461 }
606689e9 462
f0fba2ad 463 ret = gpio_direction_output(GTA02_GPIO_HP_IN, 1);
82c4362e
LPC
464 if (ret) {
465 pr_err("gta02_wm8753: Failed to configure GPIO %d\n", GTA02_GPIO_HP_IN);
466 goto err_free_gpio_hp_in;
467 }
606689e9 468
82c4362e
LPC
469 ret = gpio_request(GTA02_GPIO_AMP_SHUT, "GTA02_AMP_SHUT");
470 if (ret) {
471 pr_err("gta02_wm8753: Failed to register GPIO %d\n", GTA02_GPIO_AMP_SHUT);
472 goto err_free_gpio_hp_in;
473 }
606689e9 474
82c4362e
LPC
475 ret = gpio_direction_output(GTA02_GPIO_AMP_SHUT, 1);
476 if (ret) {
477 pr_err("gta02_wm8753: Failed to configure GPIO %d\n", GTA02_GPIO_AMP_SHUT);
478 goto err_free_gpio_amp_shut;
479 }
480
481 return 0;
482
483err_free_gpio_amp_shut:
484 gpio_free(GTA02_GPIO_AMP_SHUT);
485err_free_gpio_hp_in:
486 gpio_free(GTA02_GPIO_HP_IN);
487err_unregister_device:
488 platform_device_unregister(neo1973_gta02_snd_device);
606689e9
MB
489 return ret;
490}
491module_init(neo1973_gta02_init);
492
493static void __exit neo1973_gta02_exit(void)
494{
f0fba2ad 495 snd_soc_unregister_dai(&neo1973_gta02_snd_device->dev, -1);
606689e9 496 platform_device_unregister(neo1973_gta02_snd_device);
82c4362e
LPC
497 gpio_free(GTA02_GPIO_HP_IN);
498 gpio_free(GTA02_GPIO_AMP_SHUT);
606689e9
MB
499}
500module_exit(neo1973_gta02_exit);
501
502/* Module information */
503MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org");
504MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 GTA02");
505MODULE_LICENSE("GPL");
This page took 0.074068 seconds and 5 git commands to generate.