Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[deliverable/linux.git] / sound / soc / samsung / neo1973_wm8753.c
CommitLineData
74930bb6 1/*
f5c4ffbd 2 * neo1973_wm8753.c -- SoC audio for Openmoko Neo1973 and Freerunner devices
74930bb6 3 *
f5c4ffbd
LPC
4 * Copyright 2007 Openmoko Inc
5 * Author: Graeme Gregory <graeme@openmoko.org>
74930bb6
GG
6 * Copyright 2007 Wolfson Microelectronics PLC.
7 * Author: Graeme Gregory
8 * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
f5c4ffbd 9 * Copyright 2009 Wolfson Microelectronics
74930bb6
GG
10 *
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.
74930bb6
GG
15 */
16
17#include <linux/module.h>
74930bb6 18#include <linux/platform_device.h>
f5c4ffbd
LPC
19#include <linux/gpio.h>
20
74930bb6 21#include <sound/soc.h>
74930bb6 22
abffae64 23#include <mach/gpio-samsung.h>
fb2aa074 24#include <asm/mach-types.h>
5d229ce5 25#include "regs-iis.h"
aa9673cf 26
74930bb6 27#include "../codecs/wm8753.h"
74930bb6
GG
28#include "s3c24xx-i2s.h"
29
74930bb6
GG
30static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
31 struct snd_pcm_hw_params *params)
32{
33 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad
LG
34 struct snd_soc_dai *codec_dai = rtd->codec_dai;
35 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
74930bb6
GG
36 unsigned int pll_out = 0, bclk = 0;
37 int ret = 0;
38 unsigned long iis_clkrate;
39
40 iis_clkrate = s3c24xx_i2s_get_clockrate();
41
42 switch (params_rate(params)) {
43 case 8000:
44 case 16000:
45 pll_out = 12288000;
46 break;
47 case 48000:
48 bclk = WM8753_BCLK_DIV_4;
49 pll_out = 12288000;
50 break;
51 case 96000:
52 bclk = WM8753_BCLK_DIV_2;
53 pll_out = 12288000;
54 break;
55 case 11025:
56 bclk = WM8753_BCLK_DIV_16;
57 pll_out = 11289600;
58 break;
59 case 22050:
60 bclk = WM8753_BCLK_DIV_8;
61 pll_out = 11289600;
62 break;
63 case 44100:
64 bclk = WM8753_BCLK_DIV_4;
65 pll_out = 11289600;
66 break;
67 case 88200:
68 bclk = WM8753_BCLK_DIV_2;
69 pll_out = 11289600;
70 break;
71 }
72
73 /* set codec DAI configuration */
64105cfd 74 ret = snd_soc_dai_set_fmt(codec_dai,
74930bb6
GG
75 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
76 SND_SOC_DAIFMT_CBM_CFM);
77 if (ret < 0)
78 return ret;
79
80 /* set cpu DAI configuration */
64105cfd 81 ret = snd_soc_dai_set_fmt(cpu_dai,
74930bb6
GG
82 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
83 SND_SOC_DAIFMT_CBM_CFM);
84 if (ret < 0)
85 return ret;
86
87 /* set the codec system clock for DAC and ADC */
64105cfd 88 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
74930bb6
GG
89 SND_SOC_CLOCK_IN);
90 if (ret < 0)
91 return ret;
92
93 /* set MCLK division for sample rate */
64105cfd 94 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
8ba02ace 95 S3C2410_IISMOD_32FS);
74930bb6
GG
96 if (ret < 0)
97 return ret;
98
99 /* set codec BCLK division for sample rate */
64105cfd 100 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
74930bb6
GG
101 if (ret < 0)
102 return ret;
103
104 /* set prescaler division for sample rate */
64105cfd 105 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
8ba02ace 106 S3C24XX_PRESCALE(4, 4));
74930bb6
GG
107 if (ret < 0)
108 return ret;
109
110 /* codec PLL input is PCLK/4 */
85488037 111 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
74930bb6
GG
112 iis_clkrate / 4, pll_out);
113 if (ret < 0)
114 return ret;
115
116 return 0;
117}
118
119static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
120{
121 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 122 struct snd_soc_dai *codec_dai = rtd->codec_dai;
74930bb6
GG
123
124 /* disable the PLL */
140318aa 125 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0, 0);
74930bb6
GG
126}
127
128/*
129 * Neo1973 WM8753 HiFi DAI opserations.
130 */
131static struct snd_soc_ops neo1973_hifi_ops = {
132 .hw_params = neo1973_hifi_hw_params,
133 .hw_free = neo1973_hifi_hw_free,
134};
135
136static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
137 struct snd_pcm_hw_params *params)
138{
139 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 140 struct snd_soc_dai *codec_dai = rtd->codec_dai;
74930bb6
GG
141 unsigned int pcmdiv = 0;
142 int ret = 0;
143 unsigned long iis_clkrate;
144
145 iis_clkrate = s3c24xx_i2s_get_clockrate();
146
147 if (params_rate(params) != 8000)
148 return -EINVAL;
149 if (params_channels(params) != 1)
150 return -EINVAL;
151
152 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
153
154 /* todo: gg check mode (DSP_B) against CSR datasheet */
155 /* set codec DAI configuration */
64105cfd 156 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
74930bb6
GG
157 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
158 if (ret < 0)
159 return ret;
160
161 /* set the codec system clock for DAC and ADC */
64105cfd 162 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
74930bb6
GG
163 SND_SOC_CLOCK_IN);
164 if (ret < 0)
165 return ret;
166
167 /* set codec PCM division for sample rate */
64105cfd 168 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
74930bb6
GG
169 if (ret < 0)
170 return ret;
171
fa2eb005 172 /* configure and enable PLL for 12.288MHz output */
140318aa 173 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
74930bb6
GG
174 iis_clkrate / 4, 12288000);
175 if (ret < 0)
176 return ret;
177
178 return 0;
179}
180
181static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
182{
183 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 184 struct snd_soc_dai *codec_dai = rtd->codec_dai;
74930bb6
GG
185
186 /* disable the PLL */
140318aa 187 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0, 0);
74930bb6
GG
188}
189
190static struct snd_soc_ops neo1973_voice_ops = {
191 .hw_params = neo1973_voice_hw_params,
192 .hw_free = neo1973_voice_hw_free,
193};
194
f5c4ffbd
LPC
195static int gta02_speaker_enabled;
196
197static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
198 struct snd_ctl_elem_value *ucontrol)
199{
200 gta02_speaker_enabled = ucontrol->value.integer.value[0];
201
b2ca7871 202 gpio_set_value(S3C2410_GPJ(2), !gta02_speaker_enabled);
f5c4ffbd
LPC
203
204 return 0;
205}
206
207static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
208 struct snd_ctl_elem_value *ucontrol)
209{
210 ucontrol->value.integer.value[0] = gta02_speaker_enabled;
211 return 0;
212}
213
214static int lm4853_event(struct snd_soc_dapm_widget *w,
215 struct snd_kcontrol *k, int event)
216{
b2ca7871 217 gpio_set_value(S3C2410_GPJ(1), SND_SOC_DAPM_EVENT_OFF(event));
f5c4ffbd
LPC
218
219 return 0;
220}
221
61e7fe25
LPC
222static const struct snd_soc_dapm_widget neo1973_wm8753_dapm_widgets[] = {
223 SND_SOC_DAPM_LINE("GSM Line Out", NULL),
224 SND_SOC_DAPM_LINE("GSM Line In", NULL),
225 SND_SOC_DAPM_MIC("Headset Mic", NULL),
226 SND_SOC_DAPM_MIC("Handset Mic", NULL),
227 SND_SOC_DAPM_SPK("Handset Spk", NULL),
228 SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
229};
230
231static const struct snd_soc_dapm_route neo1973_wm8753_routes[] = {
232 /* Connections to the GSM Module */
233 {"GSM Line Out", NULL, "MONO1"},
234 {"GSM Line Out", NULL, "MONO2"},
235 {"RXP", NULL, "GSM Line In"},
236 {"RXN", NULL, "GSM Line In"},
237
238 /* Connections to Headset */
239 {"MIC1", NULL, "Mic Bias"},
240 {"Mic Bias", NULL, "Headset Mic"},
241
242 /* Call Mic */
243 {"MIC2", NULL, "Mic Bias"},
244 {"MIC2N", NULL, "Mic Bias"},
245 {"Mic Bias", NULL, "Handset Mic"},
246
247 /* Connect the ALC pins */
248 {"ACIN", NULL, "ACOP"},
249
f5c4ffbd
LPC
250 /* Connections to the amp */
251 {"Stereo Out", NULL, "LOUT1"},
252 {"Stereo Out", NULL, "ROUT1"},
253
254 /* Call Speaker */
255 {"Handset Spk", NULL, "LOUT2"},
256 {"Handset Spk", NULL, "ROUT2"},
257};
258
61e7fe25
LPC
259static const struct snd_kcontrol_new neo1973_wm8753_controls[] = {
260 SOC_DAPM_PIN_SWITCH("GSM Line Out"),
261 SOC_DAPM_PIN_SWITCH("GSM Line In"),
262 SOC_DAPM_PIN_SWITCH("Headset Mic"),
263 SOC_DAPM_PIN_SWITCH("Handset Mic"),
f5c4ffbd
LPC
264 SOC_DAPM_PIN_SWITCH("Handset Spk"),
265 SOC_DAPM_PIN_SWITCH("Stereo Out"),
266
267 SOC_SINGLE_BOOL_EXT("Amp Spk Switch", 0,
268 lm4853_get_spk,
269 lm4853_set_spk),
270};
271
f0fba2ad 272static int neo1973_wm8753_init(struct snd_soc_pcm_runtime *rtd)
74930bb6 273{
f0fba2ad 274 struct snd_soc_codec *codec = rtd->codec;
4f07c9cc 275 struct snd_soc_card *card = rtd->card;
1894c59f 276
74930bb6 277 /* set up NC codec pins */
4f07c9cc
LPC
278 snd_soc_dapm_nc_pin(&codec->dapm, "OUT3");
279 snd_soc_dapm_nc_pin(&codec->dapm, "OUT4");
280 snd_soc_dapm_nc_pin(&codec->dapm, "LINE1");
281 snd_soc_dapm_nc_pin(&codec->dapm, "LINE2");
f5c4ffbd
LPC
282
283 /* set endpoints to default off mode */
4f07c9cc
LPC
284 snd_soc_dapm_disable_pin(&card->dapm, "GSM Line Out");
285 snd_soc_dapm_disable_pin(&card->dapm, "GSM Line In");
286 snd_soc_dapm_disable_pin(&card->dapm, "Headset Mic");
287 snd_soc_dapm_disable_pin(&card->dapm, "Handset Mic");
288 snd_soc_dapm_disable_pin(&card->dapm, "Stereo Out");
289 snd_soc_dapm_disable_pin(&card->dapm, "Handset Spk");
f5c4ffbd
LPC
290
291 /* allow audio paths from the GSM modem to run during suspend */
4f07c9cc
LPC
292 snd_soc_dapm_ignore_suspend(&card->dapm, "GSM Line Out");
293 snd_soc_dapm_ignore_suspend(&card->dapm, "GSM Line In");
294 snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic");
295 snd_soc_dapm_ignore_suspend(&card->dapm, "Handset Mic");
296 snd_soc_dapm_ignore_suspend(&card->dapm, "Stereo Out");
297 snd_soc_dapm_ignore_suspend(&card->dapm, "Handset Spk");
74930bb6 298
74930bb6
GG
299 return 0;
300}
301
74930bb6
GG
302static struct snd_soc_dai_link neo1973_dai[] = {
303{ /* Hifi Playback - for similatious use with voice below */
304 .name = "WM8753",
305 .stream_name = "WM8753 HiFi",
a08485d8 306 .platform_name = "s3c24xx-iis",
518aa59f 307 .cpu_dai_name = "s3c24xx-iis",
f0fba2ad 308 .codec_dai_name = "wm8753-hifi",
b2ccf065 309 .codec_name = "wm8753.0-001a",
74930bb6
GG
310 .init = neo1973_wm8753_init,
311 .ops = &neo1973_hifi_ops,
312},
313{ /* Voice via BT */
314 .name = "Bluetooth",
315 .stream_name = "Voice",
200ceb96 316 .cpu_dai_name = "bt-sco-pcm",
f0fba2ad 317 .codec_dai_name = "wm8753-voice",
b2ccf065 318 .codec_name = "wm8753.0-001a",
74930bb6
GG
319 .ops = &neo1973_voice_ops,
320},
321};
322
9b0a25f0 323static struct snd_soc_aux_dev neo1973_aux_devs[] = {
a077ff90
LPC
324 {
325 .name = "dfbmcs320",
326 .codec_name = "dfbmcs320.0",
327 },
74930bb6
GG
328};
329
9b0a25f0
LPC
330static struct snd_soc_codec_conf neo1973_codec_conf[] = {
331 {
332 .dev_name = "lm4857.0-007c",
333 .name_prefix = "Amp",
334 },
b6471305
JD
335};
336
f5c4ffbd 337static const struct gpio neo1973_gta02_gpios[] = {
b2ca7871
KK
338 { S3C2410_GPJ(2), GPIOF_OUT_INIT_HIGH, "GTA02_HP_IN" },
339 { S3C2410_GPJ(1), GPIOF_OUT_INIT_HIGH, "GTA02_AMP_SHUT" },
f5c4ffbd 340};
f5c4ffbd 341
9b0a25f0
LPC
342static struct snd_soc_card neo1973 = {
343 .name = "neo1973",
095d79dc 344 .owner = THIS_MODULE,
9b0a25f0
LPC
345 .dai_link = neo1973_dai,
346 .num_links = ARRAY_SIZE(neo1973_dai),
347 .aux_dev = neo1973_aux_devs,
348 .num_aux_devs = ARRAY_SIZE(neo1973_aux_devs),
349 .codec_conf = neo1973_codec_conf,
350 .num_configs = ARRAY_SIZE(neo1973_codec_conf),
4f07c9cc
LPC
351
352 .controls = neo1973_wm8753_controls,
353 .num_controls = ARRAY_SIZE(neo1973_wm8753_controls),
354 .dapm_widgets = neo1973_wm8753_dapm_widgets,
355 .num_dapm_widgets = ARRAY_SIZE(neo1973_wm8753_dapm_widgets),
356 .dapm_routes = neo1973_wm8753_routes,
357 .num_dapm_routes = ARRAY_SIZE(neo1973_wm8753_routes),
74930bb6
GG
358};
359
360static struct platform_device *neo1973_snd_device;
361
362static int __init neo1973_init(void)
363{
364 int ret;
365
1ae5cbc5 366 if (!machine_is_neo1973_gta02())
fb2aa074 367 return -ENODEV;
f5c4ffbd
LPC
368
369 if (machine_is_neo1973_gta02()) {
370 neo1973.name = "neo1973gta02";
a077ff90 371 neo1973.num_aux_devs = 1;
f5c4ffbd
LPC
372
373 ret = gpio_request_array(neo1973_gta02_gpios,
374 ARRAY_SIZE(neo1973_gta02_gpios));
375 if (ret)
376 return ret;
fb2aa074
MB
377 }
378
74930bb6 379 neo1973_snd_device = platform_device_alloc("soc-audio", -1);
f5c4ffbd
LPC
380 if (!neo1973_snd_device) {
381 ret = -ENOMEM;
382 goto err_gpio_free;
383 }
384
f0fba2ad 385 platform_set_drvdata(neo1973_snd_device, &neo1973);
74930bb6
GG
386 ret = platform_device_add(neo1973_snd_device);
387
f5c4ffbd 388 if (ret)
a077ff90 389 goto err_put_device;
74930bb6 390
f5c4ffbd 391 return 0;
74930bb6 392
f5c4ffbd
LPC
393err_put_device:
394 platform_device_put(neo1973_snd_device);
395err_gpio_free:
396 if (machine_is_neo1973_gta02()) {
397 gpio_free_array(neo1973_gta02_gpios,
398 ARRAY_SIZE(neo1973_gta02_gpios));
399 }
74930bb6
GG
400 return ret;
401}
f5c4ffbd 402module_init(neo1973_init);
74930bb6
GG
403
404static void __exit neo1973_exit(void)
405{
406 platform_device_unregister(neo1973_snd_device);
74930bb6 407
f5c4ffbd
LPC
408 if (machine_is_neo1973_gta02()) {
409 gpio_free_array(neo1973_gta02_gpios,
410 ARRAY_SIZE(neo1973_gta02_gpios));
411 }
412}
74930bb6
GG
413module_exit(neo1973_exit);
414
415/* Module information */
443590e6 416MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
f5c4ffbd 417MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 and Frerunner");
74930bb6 418MODULE_LICENSE("GPL");
This page took 0.395367 seconds and 5 git commands to generate.