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.
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/timer.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/i2c.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/soc.h>
24 #include <sound/soc-dapm.h>
25 #include <sound/tlv.h>
27 #include <asm/mach-types.h>
28 #include <asm/hardware/scoop.h>
29 #include <asm/arch/regs-clock.h>
30 #include <asm/arch/regs-gpio.h>
31 #include <asm/hardware.h>
32 #include <asm/arch/audio.h>
34 #include <asm/arch/spi-gpio.h>
36 #include <asm/plat-s3c24xx/regs-iis.h>
38 #include "../codecs/wm8753.h"
40 #include "s3c24xx-pcm.h"
41 #include "s3c24xx-i2s.h"
44 #define S3C24XX_SOC_NEO1973_WM8753_DEBUG 0
45 #if S3C24XX_SOC_NEO1973_WM8753_DEBUG
46 #define DBG(x...) printk(KERN_DEBUG "s3c24xx-soc-neo1973-wm8753: " x)
51 /* define the scenarios */
52 #define NEO_AUDIO_OFF 0
53 #define NEO_GSM_CALL_AUDIO_HANDSET 1
54 #define NEO_GSM_CALL_AUDIO_HEADSET 2
55 #define NEO_GSM_CALL_AUDIO_BLUETOOTH 3
56 #define NEO_STEREO_TO_SPEAKERS 4
57 #define NEO_STEREO_TO_HEADPHONES 5
58 #define NEO_CAPTURE_HANDSET 6
59 #define NEO_CAPTURE_HEADSET 7
60 #define NEO_CAPTURE_BLUETOOTH 8
62 static struct snd_soc_machine neo1973;
63 static struct i2c_client *i2c;
65 static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params *params)
68 struct snd_soc_pcm_runtime *rtd = substream->private_data;
69 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
70 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
71 unsigned int pll_out = 0, bclk = 0;
73 unsigned long iis_clkrate;
75 DBG("Entered %s\n", __func__);
77 iis_clkrate = s3c24xx_i2s_get_clockrate();
79 switch (params_rate(params)) {
85 bclk = WM8753_BCLK_DIV_4;
89 bclk = WM8753_BCLK_DIV_2;
93 bclk = WM8753_BCLK_DIV_16;
97 bclk = WM8753_BCLK_DIV_8;
101 bclk = WM8753_BCLK_DIV_4;
105 bclk = WM8753_BCLK_DIV_2;
110 /* set codec DAI configuration */
111 ret = snd_soc_dai_set_fmt(codec_dai,
112 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
113 SND_SOC_DAIFMT_CBM_CFM);
117 /* set cpu DAI configuration */
118 ret = snd_soc_dai_set_fmt(cpu_dai,
119 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
120 SND_SOC_DAIFMT_CBM_CFM);
124 /* set the codec system clock for DAC and ADC */
125 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
130 /* set MCLK division for sample rate */
131 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
132 S3C2410_IISMOD_32FS);
136 /* set codec BCLK division for sample rate */
137 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
141 /* set prescaler division for sample rate */
142 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
143 S3C24XX_PRESCALE(4, 4));
147 /* codec PLL input is PCLK/4 */
148 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1,
149 iis_clkrate / 4, pll_out);
156 static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
158 struct snd_soc_pcm_runtime *rtd = substream->private_data;
159 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
161 DBG("Entered %s\n", __func__);
163 /* disable the PLL */
164 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0);
168 * Neo1973 WM8753 HiFi DAI opserations.
170 static struct snd_soc_ops neo1973_hifi_ops = {
171 .hw_params = neo1973_hifi_hw_params,
172 .hw_free = neo1973_hifi_hw_free,
175 static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
176 struct snd_pcm_hw_params *params)
178 struct snd_soc_pcm_runtime *rtd = substream->private_data;
179 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
180 unsigned int pcmdiv = 0;
182 unsigned long iis_clkrate;
184 DBG("Entered %s\n", __func__);
186 iis_clkrate = s3c24xx_i2s_get_clockrate();
188 if (params_rate(params) != 8000)
190 if (params_channels(params) != 1)
193 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
195 /* todo: gg check mode (DSP_B) against CSR datasheet */
196 /* set codec DAI configuration */
197 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
198 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
202 /* set the codec system clock for DAC and ADC */
203 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
208 /* set codec PCM division for sample rate */
209 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
213 /* configue and enable PLL for 12.288MHz output */
214 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2,
215 iis_clkrate / 4, 12288000);
222 static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
224 struct snd_soc_pcm_runtime *rtd = substream->private_data;
225 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
227 DBG("Entered %s\n", __func__);
229 /* disable the PLL */
230 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0);
233 static struct snd_soc_ops neo1973_voice_ops = {
234 .hw_params = neo1973_voice_hw_params,
235 .hw_free = neo1973_voice_hw_free,
238 static int neo1973_scenario;
240 static int neo1973_get_scenario(struct snd_kcontrol *kcontrol,
241 struct snd_ctl_elem_value *ucontrol)
243 ucontrol->value.integer.value[0] = neo1973_scenario;
247 static int set_scenario_endpoints(struct snd_soc_codec *codec, int scenario)
249 DBG("Entered %s\n", __func__);
251 switch (neo1973_scenario) {
253 snd_soc_dapm_disable_pin(codec, "Audio Out");
254 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
255 snd_soc_dapm_disable_pin(codec, "GSM Line In");
256 snd_soc_dapm_disable_pin(codec, "Headset Mic");
257 snd_soc_dapm_disable_pin(codec, "Call Mic");
259 case NEO_GSM_CALL_AUDIO_HANDSET:
260 snd_soc_dapm_enable_pin(codec, "Audio Out");
261 snd_soc_dapm_enable_pin(codec, "GSM Line Out");
262 snd_soc_dapm_enable_pin(codec, "GSM Line In");
263 snd_soc_dapm_disable_pin(codec, "Headset Mic");
264 snd_soc_dapm_enable_pin(codec, "Call Mic");
266 case NEO_GSM_CALL_AUDIO_HEADSET:
267 snd_soc_dapm_enable_pin(codec, "Audio Out");
268 snd_soc_dapm_enable_pin(codec, "GSM Line Out");
269 snd_soc_dapm_enable_pin(codec, "GSM Line In");
270 snd_soc_dapm_enable_pin(codec, "Headset Mic");
271 snd_soc_dapm_disable_pin(codec, "Call Mic");
273 case NEO_GSM_CALL_AUDIO_BLUETOOTH:
274 snd_soc_dapm_disable_pin(codec, "Audio Out");
275 snd_soc_dapm_enable_pin(codec, "GSM Line Out");
276 snd_soc_dapm_enable_pin(codec, "GSM Line In");
277 snd_soc_dapm_disable_pin(codec, "Headset Mic");
278 snd_soc_dapm_disable_pin(codec, "Call Mic");
280 case NEO_STEREO_TO_SPEAKERS:
281 snd_soc_dapm_enable_pin(codec, "Audio Out");
282 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
283 snd_soc_dapm_disable_pin(codec, "GSM Line In");
284 snd_soc_dapm_disable_pin(codec, "Headset Mic");
285 snd_soc_dapm_disable_pin(codec, "Call Mic");
287 case NEO_STEREO_TO_HEADPHONES:
288 snd_soc_dapm_enable_pin(codec, "Audio Out");
289 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
290 snd_soc_dapm_disable_pin(codec, "GSM Line In");
291 snd_soc_dapm_disable_pin(codec, "Headset Mic");
292 snd_soc_dapm_disable_pin(codec, "Call Mic");
294 case NEO_CAPTURE_HANDSET:
295 snd_soc_dapm_disable_pin(codec, "Audio Out");
296 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
297 snd_soc_dapm_disable_pin(codec, "GSM Line In");
298 snd_soc_dapm_disable_pin(codec, "Headset Mic");
299 snd_soc_dapm_enable_pin(codec, "Call Mic");
301 case NEO_CAPTURE_HEADSET:
302 snd_soc_dapm_disable_pin(codec, "Audio Out");
303 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
304 snd_soc_dapm_disable_pin(codec, "GSM Line In");
305 snd_soc_dapm_enable_pin(codec, "Headset Mic");
306 snd_soc_dapm_disable_pin(codec, "Call Mic");
308 case NEO_CAPTURE_BLUETOOTH:
309 snd_soc_dapm_disable_pin(codec, "Audio Out");
310 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
311 snd_soc_dapm_disable_pin(codec, "GSM Line In");
312 snd_soc_dapm_disable_pin(codec, "Headset Mic");
313 snd_soc_dapm_disable_pin(codec, "Call Mic");
316 snd_soc_dapm_disable_pin(codec, "Audio Out");
317 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
318 snd_soc_dapm_disable_pin(codec, "GSM Line In");
319 snd_soc_dapm_disable_pin(codec, "Headset Mic");
320 snd_soc_dapm_disable_pin(codec, "Call Mic");
323 snd_soc_dapm_sync(codec);
328 static int neo1973_set_scenario(struct snd_kcontrol *kcontrol,
329 struct snd_ctl_elem_value *ucontrol)
331 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
333 DBG("Entered %s\n", __func__);
335 if (neo1973_scenario == ucontrol->value.integer.value[0])
338 neo1973_scenario = ucontrol->value.integer.value[0];
339 set_scenario_endpoints(codec, neo1973_scenario);
343 static u8 lm4857_regs[4] = {0x00, 0x40, 0x80, 0xC0};
345 static void lm4857_write_regs(void)
347 DBG("Entered %s\n", __func__);
349 if (i2c_master_send(i2c, lm4857_regs, 4) != 4)
350 printk(KERN_ERR "lm4857: i2c write failed\n");
353 static int lm4857_get_reg(struct snd_kcontrol *kcontrol,
354 struct snd_ctl_elem_value *ucontrol)
356 int reg = kcontrol->private_value & 0xFF;
357 int shift = (kcontrol->private_value >> 8) & 0x0F;
358 int mask = (kcontrol->private_value >> 16) & 0xFF;
360 DBG("Entered %s\n", __func__);
362 ucontrol->value.integer.value[0] = (lm4857_regs[reg] >> shift) & mask;
366 static int lm4857_set_reg(struct snd_kcontrol *kcontrol,
367 struct snd_ctl_elem_value *ucontrol)
369 int reg = kcontrol->private_value & 0xFF;
370 int shift = (kcontrol->private_value >> 8) & 0x0F;
371 int mask = (kcontrol->private_value >> 16) & 0xFF;
373 if (((lm4857_regs[reg] >> shift) & mask) ==
374 ucontrol->value.integer.value[0])
377 lm4857_regs[reg] &= ~(mask << shift);
378 lm4857_regs[reg] |= ucontrol->value.integer.value[0] << shift;
383 static int lm4857_get_mode(struct snd_kcontrol *kcontrol,
384 struct snd_ctl_elem_value *ucontrol)
386 u8 value = lm4857_regs[LM4857_CTRL] & 0x0F;
388 DBG("Entered %s\n", __func__);
393 ucontrol->value.integer.value[0] = value;
397 static int lm4857_set_mode(struct snd_kcontrol *kcontrol,
398 struct snd_ctl_elem_value *ucontrol)
400 u8 value = ucontrol->value.integer.value[0];
402 DBG("Entered %s\n", __func__);
407 if ((lm4857_regs[LM4857_CTRL] & 0x0F) == value)
410 lm4857_regs[LM4857_CTRL] &= 0xF0;
411 lm4857_regs[LM4857_CTRL] |= value;
416 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
417 SND_SOC_DAPM_LINE("Audio Out", NULL),
418 SND_SOC_DAPM_LINE("GSM Line Out", NULL),
419 SND_SOC_DAPM_LINE("GSM Line In", NULL),
420 SND_SOC_DAPM_MIC("Headset Mic", NULL),
421 SND_SOC_DAPM_MIC("Call Mic", NULL),
425 static const struct snd_soc_dapm_route dapm_routes[] = {
427 /* Connections to the lm4857 amp */
428 {"Audio Out", NULL, "LOUT1"},
429 {"Audio Out", NULL, "ROUT1"},
431 /* Connections to the GSM Module */
432 {"GSM Line Out", NULL, "MONO1"},
433 {"GSM Line Out", NULL, "MONO2"},
434 {"RXP", NULL, "GSM Line In"},
435 {"RXN", NULL, "GSM Line In"},
437 /* Connections to Headset */
438 {"MIC1", NULL, "Mic Bias"},
439 {"Mic Bias", NULL, "Headset Mic"},
442 {"MIC2", NULL, "Mic Bias"},
443 {"MIC2N", NULL, "Mic Bias"},
444 {"Mic Bias", NULL, "Call Mic"},
446 /* Connect the ALC pins */
447 {"ACIN", NULL, "ACOP"},
450 static const char *lm4857_mode[] = {
454 "Stereo Speakers + Headphones",
458 static const struct soc_enum lm4857_mode_enum[] = {
459 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lm4857_mode), lm4857_mode),
462 static const char *neo_scenarios[] = {
474 static const struct soc_enum neo_scenario_enum[] = {
475 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(neo_scenarios), neo_scenarios),
478 static const DECLARE_TLV_DB_SCALE(stereo_tlv, -4050, 150, 0);
479 static const DECLARE_TLV_DB_SCALE(mono_tlv, -3450, 150, 0);
481 static const struct snd_kcontrol_new wm8753_neo1973_controls[] = {
482 SOC_SINGLE_EXT_TLV("Amp Left Playback Volume", LM4857_LVOL, 0, 31, 0,
483 lm4857_get_reg, lm4857_set_reg, stereo_tlv),
484 SOC_SINGLE_EXT_TLV("Amp Right Playback Volume", LM4857_RVOL, 0, 31, 0,
485 lm4857_get_reg, lm4857_set_reg, stereo_tlv),
486 SOC_SINGLE_EXT_TLV("Amp Mono Playback Volume", LM4857_MVOL, 0, 31, 0,
487 lm4857_get_reg, lm4857_set_reg, mono_tlv),
488 SOC_ENUM_EXT("Amp Mode", lm4857_mode_enum[0],
489 lm4857_get_mode, lm4857_set_mode),
490 SOC_ENUM_EXT("Neo Mode", neo_scenario_enum[0],
491 neo1973_get_scenario, neo1973_set_scenario),
492 SOC_SINGLE_EXT("Amp Spk 3D Playback Switch", LM4857_LVOL, 5, 1, 0,
493 lm4857_get_reg, lm4857_set_reg),
494 SOC_SINGLE_EXT("Amp HP 3d Playback Switch", LM4857_RVOL, 5, 1, 0,
495 lm4857_get_reg, lm4857_set_reg),
496 SOC_SINGLE_EXT("Amp Fast Wakeup Playback Switch", LM4857_CTRL, 5, 1, 0,
497 lm4857_get_reg, lm4857_set_reg),
498 SOC_SINGLE_EXT("Amp Earpiece 6dB Playback Switch", LM4857_CTRL, 4, 1, 0,
499 lm4857_get_reg, lm4857_set_reg),
503 * This is an example machine initialisation for a wm8753 connected to a
504 * neo1973 II. It is missing logic to detect hp/mic insertions and logic
505 * to re-route the audio in such an event.
507 static int neo1973_wm8753_init(struct snd_soc_codec *codec)
511 DBG("Entered %s\n", __func__);
513 /* set up NC codec pins */
514 snd_soc_dapm_disable_pin(codec, "LOUT2");
515 snd_soc_dapm_disable_pin(codec, "ROUT2");
516 snd_soc_dapm_disable_pin(codec, "OUT3");
517 snd_soc_dapm_disable_pin(codec, "OUT4");
518 snd_soc_dapm_disable_pin(codec, "LINE1");
519 snd_soc_dapm_disable_pin(codec, "LINE2");
522 /* set endpoints to default mode */
523 set_scenario_endpoints(codec, NEO_AUDIO_OFF);
525 /* Add neo1973 specific widgets */
526 snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
527 ARRAY_SIZE(wm8753_dapm_widgets));
529 /* add neo1973 specific controls */
530 for (i = 0; i < ARRAY_SIZE(wm8753_neo1973_controls); i++) {
531 err = snd_ctl_add(codec->card,
532 snd_soc_cnew(&wm8753_neo1973_controls[i],
538 /* set up neo1973 specific audio routes */
539 err = snd_soc_dapm_add_routes(codec, dapm_routes,
540 ARRAY_SIZE(dapm_routes));
542 snd_soc_dapm_sync(codec);
549 static struct snd_soc_dai bt_dai = {
552 .type = SND_SOC_DAI_PCM,
556 .rates = SNDRV_PCM_RATE_8000,
557 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
561 .rates = SNDRV_PCM_RATE_8000,
562 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
565 static struct snd_soc_dai_link neo1973_dai[] = {
566 { /* Hifi Playback - for similatious use with voice below */
568 .stream_name = "WM8753 HiFi",
569 .cpu_dai = &s3c24xx_i2s_dai,
570 .codec_dai = &wm8753_dai[WM8753_DAI_HIFI],
571 .init = neo1973_wm8753_init,
572 .ops = &neo1973_hifi_ops,
576 .stream_name = "Voice",
578 .codec_dai = &wm8753_dai[WM8753_DAI_VOICE],
579 .ops = &neo1973_voice_ops,
583 static struct snd_soc_machine neo1973 = {
585 .dai_link = neo1973_dai,
586 .num_links = ARRAY_SIZE(neo1973_dai),
589 static struct wm8753_setup_data neo1973_wm8753_setup = {
593 static struct snd_soc_device neo1973_snd_devdata = {
595 .platform = &s3c24xx_soc_platform,
596 .codec_dev = &soc_codec_dev_wm8753,
597 .codec_data = &neo1973_wm8753_setup,
600 static struct i2c_client client_template;
602 static const unsigned short normal_i2c[] = { 0x7C, I2C_CLIENT_END };
604 /* Magic definition of all other variables and things */
607 static int lm4857_amp_probe(struct i2c_adapter *adap, int addr, int kind)
611 DBG("Entered %s\n", __func__);
613 client_template.adapter = adap;
614 client_template.addr = addr;
616 i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
620 ret = i2c_attach_client(i2c);
622 printk(KERN_ERR "LM4857 failed to attach at addr %x\n", addr);
634 static int lm4857_i2c_detach(struct i2c_client *client)
636 DBG("Entered %s\n", __func__);
638 i2c_detach_client(client);
643 static int lm4857_i2c_attach(struct i2c_adapter *adap)
645 DBG("Entered %s\n", __func__);
647 return i2c_probe(adap, &addr_data, lm4857_amp_probe);
650 static u8 lm4857_state;
652 static int lm4857_suspend(struct i2c_client *dev, pm_message_t state)
654 DBG("Entered %s\n", __func__);
656 dev_dbg(&dev->dev, "lm4857_suspend\n");
657 lm4857_state = lm4857_regs[LM4857_CTRL] & 0xf;
659 lm4857_regs[LM4857_CTRL] &= 0xf0;
665 static int lm4857_resume(struct i2c_client *dev)
667 DBG("Entered %s\n", __func__);
670 lm4857_regs[LM4857_CTRL] |= (lm4857_state & 0x0f);
676 static void lm4857_shutdown(struct i2c_client *dev)
678 DBG("Entered %s\n", __func__);
680 dev_dbg(&dev->dev, "lm4857_shutdown\n");
681 lm4857_regs[LM4857_CTRL] &= 0xf0;
685 /* corgi i2c codec control layer */
686 static struct i2c_driver lm4857_i2c_driver = {
688 .name = "LM4857 I2C Amp",
689 .owner = THIS_MODULE,
691 .id = I2C_DRIVERID_LM4857,
692 .suspend = lm4857_suspend,
693 .resume = lm4857_resume,
694 .shutdown = lm4857_shutdown,
695 .attach_adapter = lm4857_i2c_attach,
696 .detach_client = lm4857_i2c_detach,
700 static struct i2c_client client_template = {
702 .driver = &lm4857_i2c_driver,
705 static struct platform_device *neo1973_snd_device;
707 static int __init neo1973_init(void)
711 DBG("Entered %s\n", __func__);
713 neo1973_snd_device = platform_device_alloc("soc-audio", -1);
714 if (!neo1973_snd_device)
717 platform_set_drvdata(neo1973_snd_device, &neo1973_snd_devdata);
718 neo1973_snd_devdata.dev = &neo1973_snd_device->dev;
719 ret = platform_device_add(neo1973_snd_device);
722 platform_device_put(neo1973_snd_device);
724 ret = i2c_add_driver(&lm4857_i2c_driver);
726 printk(KERN_ERR "can't add i2c driver");
731 static void __exit neo1973_exit(void)
733 DBG("Entered %s\n", __func__);
735 i2c_del_driver(&lm4857_i2c_driver);
736 platform_device_unregister(neo1973_snd_device);
739 module_init(neo1973_init);
740 module_exit(neo1973_exit);
742 /* Module information */
743 MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
744 MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973");
745 MODULE_LICENSE("GPL");