2 * neo1973_wm8753.c -- SoC audio for Neo1973
4 * Copyright 2007 Wolfson Microelectronics PLC.
5 * Author: Graeme Gregory
6 * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 * 20th Jan 2007 Initial version.
15 * 05th Feb 2007 Rename all to Neo1973
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/timer.h>
22 #include <linux/interrupt.h>
23 #include <linux/platform_device.h>
24 #include <linux/i2c.h>
25 #include <sound/driver.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
31 #include <asm/mach-types.h>
32 #include <asm/hardware/scoop.h>
33 #include <asm/arch/regs-clock.h>
34 #include <asm/arch/regs-gpio.h>
35 #include <asm/hardware.h>
36 #include <asm/arch/audio.h>
38 #include <asm/arch/spi-gpio.h>
40 #include <asm/plat-s3c24xx/regs-iis.h>
42 #include "../codecs/wm8753.h"
44 #include "s3c24xx-pcm.h"
45 #include "s3c24xx-i2s.h"
47 /* define the scenarios */
48 #define NEO_AUDIO_OFF 0
49 #define NEO_GSM_CALL_AUDIO_HANDSET 1
50 #define NEO_GSM_CALL_AUDIO_HEADSET 2
51 #define NEO_GSM_CALL_AUDIO_BLUETOOTH 3
52 #define NEO_STEREO_TO_SPEAKERS 4
53 #define NEO_STEREO_TO_HEADPHONES 5
54 #define NEO_CAPTURE_HANDSET 6
55 #define NEO_CAPTURE_HEADSET 7
56 #define NEO_CAPTURE_BLUETOOTH 8
58 static struct snd_soc_machine neo1973;
59 static struct i2c_client *i2c;
61 static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
62 struct snd_pcm_hw_params *params)
64 struct snd_soc_pcm_runtime *rtd = substream->private_data;
65 struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
66 struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
67 unsigned int pll_out = 0, bclk = 0;
69 unsigned long iis_clkrate;
71 iis_clkrate = s3c24xx_i2s_get_clockrate();
73 switch (params_rate(params)) {
79 bclk = WM8753_BCLK_DIV_4;
83 bclk = WM8753_BCLK_DIV_2;
87 bclk = WM8753_BCLK_DIV_16;
91 bclk = WM8753_BCLK_DIV_8;
95 bclk = WM8753_BCLK_DIV_4;
99 bclk = WM8753_BCLK_DIV_2;
104 /* set codec DAI configuration */
105 ret = codec_dai->dai_ops.set_fmt(codec_dai,
106 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
107 SND_SOC_DAIFMT_CBM_CFM);
111 /* set cpu DAI configuration */
112 ret = cpu_dai->dai_ops.set_fmt(cpu_dai,
113 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
114 SND_SOC_DAIFMT_CBM_CFM);
118 /* set the codec system clock for DAC and ADC */
119 ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8753_MCLK, pll_out,
124 /* set MCLK division for sample rate */
125 ret = cpu_dai->dai_ops.set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
126 S3C2410_IISMOD_32FS );
130 /* set codec BCLK division for sample rate */
131 ret = codec_dai->dai_ops.set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
135 /* set prescaler division for sample rate */
136 ret = cpu_dai->dai_ops.set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
137 S3C24XX_PRESCALE(4,4));
141 /* codec PLL input is PCLK/4 */
142 ret = codec_dai->dai_ops.set_pll(codec_dai, WM8753_PLL1,
143 iis_clkrate / 4, pll_out);
150 static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
152 struct snd_soc_pcm_runtime *rtd = substream->private_data;
153 struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
155 /* disable the PLL */
156 return codec_dai->dai_ops.set_pll(codec_dai, WM8753_PLL1, 0, 0);
160 * Neo1973 WM8753 HiFi DAI opserations.
162 static struct snd_soc_ops neo1973_hifi_ops = {
163 .hw_params = neo1973_hifi_hw_params,
164 .hw_free = neo1973_hifi_hw_free,
167 static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
168 struct snd_pcm_hw_params *params)
170 struct snd_soc_pcm_runtime *rtd = substream->private_data;
171 struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
172 unsigned int pcmdiv = 0;
174 unsigned long iis_clkrate;
176 iis_clkrate = s3c24xx_i2s_get_clockrate();
178 if (params_rate(params) != 8000)
180 if (params_channels(params) != 1)
183 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
185 /* todo: gg check mode (DSP_B) against CSR datasheet */
186 /* set codec DAI configuration */
187 ret = codec_dai->dai_ops.set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
188 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
192 /* set the codec system clock for DAC and ADC */
193 ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
198 /* set codec PCM division for sample rate */
199 ret = codec_dai->dai_ops.set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
203 /* configue and enable PLL for 12.288MHz output */
204 ret = codec_dai->dai_ops.set_pll(codec_dai, WM8753_PLL2,
205 iis_clkrate / 4, 12288000);
212 static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
214 struct snd_soc_pcm_runtime *rtd = substream->private_data;
215 struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
217 /* disable the PLL */
218 return codec_dai->dai_ops.set_pll(codec_dai, WM8753_PLL2, 0, 0);
221 static struct snd_soc_ops neo1973_voice_ops = {
222 .hw_params = neo1973_voice_hw_params,
223 .hw_free = neo1973_voice_hw_free,
226 static int neo1973_scenario = 0;
228 static int neo1973_get_scenario(struct snd_kcontrol *kcontrol,
229 struct snd_ctl_elem_value *ucontrol)
231 ucontrol->value.integer.value[0] = neo1973_scenario;
235 static int set_scenario_endpoints(struct snd_soc_codec *codec, int scenario)
237 switch(neo1973_scenario) {
239 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
240 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
241 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
242 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
243 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
245 case NEO_GSM_CALL_AUDIO_HANDSET:
246 snd_soc_dapm_set_endpoint(codec, "Audio Out", 1);
247 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 1);
248 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 1);
249 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
250 snd_soc_dapm_set_endpoint(codec, "Call Mic", 1);
252 case NEO_GSM_CALL_AUDIO_HEADSET:
253 snd_soc_dapm_set_endpoint(codec, "Audio Out", 1);
254 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 1);
255 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 1);
256 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 1);
257 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
259 case NEO_GSM_CALL_AUDIO_BLUETOOTH:
260 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
261 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 1);
262 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 1);
263 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
264 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
266 case NEO_STEREO_TO_SPEAKERS:
267 snd_soc_dapm_set_endpoint(codec, "Audio Out", 1);
268 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
269 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
270 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
271 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
273 case NEO_STEREO_TO_HEADPHONES:
274 snd_soc_dapm_set_endpoint(codec, "Audio Out", 1);
275 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
276 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
277 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
278 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
280 case NEO_CAPTURE_HANDSET:
281 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
282 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
283 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
284 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
285 snd_soc_dapm_set_endpoint(codec, "Call Mic", 1);
287 case NEO_CAPTURE_HEADSET:
288 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
289 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
290 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
291 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 1);
292 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
294 case NEO_CAPTURE_BLUETOOTH:
295 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
296 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
297 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
298 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
299 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
302 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
303 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
304 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
305 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
306 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
309 snd_soc_dapm_sync_endpoints(codec);
314 static int neo1973_set_scenario(struct snd_kcontrol *kcontrol,
315 struct snd_ctl_elem_value *ucontrol)
317 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
319 if (neo1973_scenario == ucontrol->value.integer.value[0])
322 neo1973_scenario = ucontrol->value.integer.value[0];
323 set_scenario_endpoints(codec, neo1973_scenario);
327 static u8 lm4857_regs[4] = {0x00, 0x40, 0x80, 0xC0};
329 static void lm4857_write_regs(void)
331 if (i2c_master_send(i2c, lm4857_regs, 4) != 4)
332 printk(KERN_ERR "lm4857: i2c write failed\n");
335 static int lm4857_get_reg(struct snd_kcontrol *kcontrol,
336 struct snd_ctl_elem_value *ucontrol)
338 int reg=kcontrol->private_value & 0xFF;
339 int shift = (kcontrol->private_value >> 8) & 0x0F;
340 int mask = (kcontrol->private_value >> 16) & 0xFF;
342 ucontrol->value.integer.value[0] = (lm4857_regs[reg] >> shift) & mask;
346 static int lm4857_set_reg(struct snd_kcontrol *kcontrol,
347 struct snd_ctl_elem_value *ucontrol)
349 int reg = kcontrol->private_value & 0xFF;
350 int shift = (kcontrol->private_value >> 8) & 0x0F;
351 int mask = (kcontrol->private_value >> 16) & 0xFF;
353 if (((lm4857_regs[reg] >> shift ) & mask) ==
354 ucontrol->value.integer.value[0])
357 lm4857_regs[reg] &= ~ (mask << shift);
358 lm4857_regs[reg] |= ucontrol->value.integer.value[0] << shift;
363 static int lm4857_get_mode(struct snd_kcontrol *kcontrol,
364 struct snd_ctl_elem_value *ucontrol)
366 u8 value = lm4857_regs[LM4857_CTRL] & 0x0F;
371 ucontrol->value.integer.value[0] = value;
375 static int lm4857_set_mode(struct snd_kcontrol *kcontrol,
376 struct snd_ctl_elem_value *ucontrol)
378 u8 value = ucontrol->value.integer.value[0];
383 if ((lm4857_regs[LM4857_CTRL] & 0x0F) == value)
386 lm4857_regs[LM4857_CTRL] &= 0xF0;
387 lm4857_regs[LM4857_CTRL] |= value;
392 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
393 SND_SOC_DAPM_LINE("Audio Out", NULL),
394 SND_SOC_DAPM_LINE("GSM Line Out", NULL),
395 SND_SOC_DAPM_LINE("GSM Line In", NULL),
396 SND_SOC_DAPM_MIC("Headset Mic", NULL),
397 SND_SOC_DAPM_MIC("Call Mic", NULL),
401 /* example machine audio_mapnections */
402 static const char* audio_map[][3] = {
404 /* Connections to the lm4857 amp */
405 {"Audio Out", NULL, "LOUT1"},
406 {"Audio Out", NULL, "ROUT1"},
408 /* Connections to the GSM Module */
409 {"GSM Line Out", NULL, "MONO1"},
410 {"GSM Line Out", NULL, "MONO2"},
411 {"RXP", NULL, "GSM Line In"},
412 {"RXN", NULL, "GSM Line In"},
414 /* Connections to Headset */
415 {"MIC1", NULL, "Mic Bias"},
416 {"Mic Bias", NULL, "Headset Mic"},
419 {"MIC2", NULL, "Mic Bias"},
420 {"MIC2N", NULL, "Mic Bias"},
421 {"Mic Bias", NULL, "Call Mic"},
423 /* Connect the ALC pins */
424 {"ACIN", NULL, "ACOP"},
429 static const char *lm4857_mode[] = {
433 "Stereo Speakers + Headphones",
437 static const struct soc_enum lm4857_mode_enum[] = {
438 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lm4857_mode), lm4857_mode),
441 static const char *neo_scenarios[] = {
453 static const struct soc_enum neo_scenario_enum[] = {
454 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(neo_scenarios),neo_scenarios),
457 static const struct snd_kcontrol_new wm8753_neo1973_controls[] = {
458 SOC_SINGLE_EXT("Amp Left Playback Volume", LM4857_LVOL, 0, 31, 0,
459 lm4857_get_reg, lm4857_set_reg),
460 SOC_SINGLE_EXT("Amp Right Playback Volume", LM4857_RVOL, 0, 31, 0,
461 lm4857_get_reg, lm4857_set_reg),
462 SOC_SINGLE_EXT("Amp Mono Playback Volume", LM4857_MVOL, 0, 31, 0,
463 lm4857_get_reg, lm4857_set_reg),
464 SOC_ENUM_EXT("Amp Mode", lm4857_mode_enum[0],
465 lm4857_get_mode, lm4857_set_mode),
466 SOC_ENUM_EXT("Neo Mode", neo_scenario_enum[0],
467 neo1973_get_scenario, neo1973_set_scenario),
468 SOC_SINGLE_EXT("Amp Spk 3D Playback Switch", LM4857_LVOL, 5, 1, 0,
469 lm4857_get_reg, lm4857_set_reg),
470 SOC_SINGLE_EXT("Amp HP 3d Playback Switch", LM4857_RVOL, 5, 1, 0,
471 lm4857_get_reg, lm4857_set_reg),
472 SOC_SINGLE_EXT("Amp Fast Wakeup Playback Switch", LM4857_CTRL, 5, 1, 0,
473 lm4857_get_reg, lm4857_set_reg),
474 SOC_SINGLE_EXT("Amp Earpiece 6dB Playback Switch", LM4857_CTRL, 4, 1, 0,
475 lm4857_get_reg, lm4857_set_reg),
479 * This is an example machine initialisation for a wm8753 connected to a
480 * neo1973 II. It is missing logic to detect hp/mic insertions and logic
481 * to re-route the audio in such an event.
483 static int neo1973_wm8753_init(struct snd_soc_codec *codec)
487 /* set up NC codec pins */
488 snd_soc_dapm_set_endpoint(codec, "LOUT2", 0);
489 snd_soc_dapm_set_endpoint(codec, "ROUT2", 0);
490 snd_soc_dapm_set_endpoint(codec, "OUT3", 0);
491 snd_soc_dapm_set_endpoint(codec, "OUT4", 0);
492 snd_soc_dapm_set_endpoint(codec, "LINE1", 0);
493 snd_soc_dapm_set_endpoint(codec, "LINE2", 0);
496 /* set endpoints to default mode */
497 set_scenario_endpoints(codec, NEO_AUDIO_OFF);
499 /* Add neo1973 specific widgets */
500 for (i = 0; i < ARRAY_SIZE(wm8753_dapm_widgets); i++)
501 snd_soc_dapm_new_control(codec, &wm8753_dapm_widgets[i]);
503 /* add neo1973 specific controls */
504 for (i = 0; i < ARRAY_SIZE(wm8753_neo1973_controls); i++) {
505 err = snd_ctl_add(codec->card,
506 snd_soc_cnew(&wm8753_neo1973_controls[i],
512 /* set up neo1973 specific audio path audio_mapnects */
513 for (i = 0; audio_map[i][0] != NULL; i++) {
514 snd_soc_dapm_connect_input(codec, audio_map[i][0],
515 audio_map[i][1], audio_map[i][2]);
518 snd_soc_dapm_sync_endpoints(codec);
525 static struct snd_soc_cpu_dai bt_dai =
526 { .name = "Bluetooth",
528 .type = SND_SOC_DAI_PCM,
532 .rates = SNDRV_PCM_RATE_8000,
533 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
537 .rates = SNDRV_PCM_RATE_8000,
538 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
541 static struct snd_soc_dai_link neo1973_dai[] = {
542 { /* Hifi Playback - for similatious use with voice below */
544 .stream_name = "WM8753 HiFi",
545 .cpu_dai = &s3c24xx_i2s_dai,
546 .codec_dai = &wm8753_dai[WM8753_DAI_HIFI],
547 .init = neo1973_wm8753_init,
548 .ops = &neo1973_hifi_ops,
552 .stream_name = "Voice",
554 .codec_dai = &wm8753_dai[WM8753_DAI_VOICE],
555 .ops = &neo1973_voice_ops,
559 static struct snd_soc_machine neo1973 = {
561 .dai_link = neo1973_dai,
562 .num_links = ARRAY_SIZE(neo1973_dai),
565 static struct wm8753_setup_data neo1973_wm8753_setup = {
569 static struct snd_soc_device neo1973_snd_devdata = {
571 .platform = &s3c24xx_soc_platform,
572 .codec_dev = &soc_codec_dev_wm8753,
573 .codec_data = &neo1973_wm8753_setup,
576 static struct i2c_client client_template;
578 static const unsigned short normal_i2c[] = { 0x7C, I2C_CLIENT_END };
580 /* Magic definition of all other variables and things */
583 static int lm4857_amp_probe(struct i2c_adapter *adap, int addr, int kind)
587 client_template.adapter = adap;
588 client_template.addr = addr;
590 i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
594 ret = i2c_attach_client(i2c);
596 printk(KERN_ERR "LM4857 failed to attach at addr %x\n", addr);
608 static int lm4857_i2c_detach(struct i2c_client *client)
610 i2c_detach_client(client);
615 static int lm4857_i2c_attach(struct i2c_adapter *adap)
617 return i2c_probe(adap, &addr_data, lm4857_amp_probe);
620 /* corgi i2c codec control layer */
621 static struct i2c_driver lm4857_i2c_driver = {
623 .name = "LM4857 I2C Amp",
624 .owner = THIS_MODULE,
626 .id = I2C_DRIVERID_LM4857,
627 .attach_adapter = lm4857_i2c_attach,
628 .detach_client = lm4857_i2c_detach,
632 static struct i2c_client client_template = {
634 .driver = &lm4857_i2c_driver,
637 static struct platform_device *neo1973_snd_device;
639 static int __init neo1973_init(void)
643 neo1973_snd_device = platform_device_alloc("soc-audio", -1);
644 if (!neo1973_snd_device)
647 platform_set_drvdata(neo1973_snd_device, &neo1973_snd_devdata);
648 neo1973_snd_devdata.dev = &neo1973_snd_device->dev;
649 ret = platform_device_add(neo1973_snd_device);
652 platform_device_put(neo1973_snd_device);
654 ret = i2c_add_driver(&lm4857_i2c_driver);
656 printk(KERN_ERR "can't add i2c driver");
661 static void __exit neo1973_exit(void)
663 platform_device_unregister(neo1973_snd_device);
666 module_init(neo1973_init);
667 module_exit(neo1973_exit);
669 /* Module information */
670 MODULE_AUTHOR("Graeme Gregory, graeme.gregory@wolfsonmicro.com, www.wolfsonmicro.com");
671 MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973");
672 MODULE_LICENSE("GPL");