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/hardware/scoop.h>
28 #include <mach/regs-clock.h>
29 #include <mach/regs-gpio.h>
30 #include <mach/hardware.h>
31 #include <mach/audio.h>
33 #include <mach/spi-gpio.h>
35 #include <asm/plat-s3c24xx/regs-iis.h>
37 #include "../codecs/wm8753.h"
39 #include "s3c24xx-pcm.h"
40 #include "s3c24xx-i2s.h"
43 #define S3C24XX_SOC_NEO1973_WM8753_DEBUG 0
44 #if S3C24XX_SOC_NEO1973_WM8753_DEBUG
45 #define DBG(x...) printk(KERN_DEBUG "s3c24xx-soc-neo1973-wm8753: " x)
50 /* define the scenarios */
51 #define NEO_AUDIO_OFF 0
52 #define NEO_GSM_CALL_AUDIO_HANDSET 1
53 #define NEO_GSM_CALL_AUDIO_HEADSET 2
54 #define NEO_GSM_CALL_AUDIO_BLUETOOTH 3
55 #define NEO_STEREO_TO_SPEAKERS 4
56 #define NEO_STEREO_TO_HEADPHONES 5
57 #define NEO_CAPTURE_HANDSET 6
58 #define NEO_CAPTURE_HEADSET 7
59 #define NEO_CAPTURE_BLUETOOTH 8
61 static struct snd_soc_machine neo1973;
62 static struct i2c_client *i2c;
64 static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params *params)
67 struct snd_soc_pcm_runtime *rtd = substream->private_data;
68 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
69 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
70 unsigned int pll_out = 0, bclk = 0;
72 unsigned long iis_clkrate;
74 DBG("Entered %s\n", __func__);
76 iis_clkrate = s3c24xx_i2s_get_clockrate();
78 switch (params_rate(params)) {
84 bclk = WM8753_BCLK_DIV_4;
88 bclk = WM8753_BCLK_DIV_2;
92 bclk = WM8753_BCLK_DIV_16;
96 bclk = WM8753_BCLK_DIV_8;
100 bclk = WM8753_BCLK_DIV_4;
104 bclk = WM8753_BCLK_DIV_2;
109 /* set codec DAI configuration */
110 ret = snd_soc_dai_set_fmt(codec_dai,
111 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
112 SND_SOC_DAIFMT_CBM_CFM);
116 /* set cpu DAI configuration */
117 ret = snd_soc_dai_set_fmt(cpu_dai,
118 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
119 SND_SOC_DAIFMT_CBM_CFM);
123 /* set the codec system clock for DAC and ADC */
124 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
129 /* set MCLK division for sample rate */
130 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
131 S3C2410_IISMOD_32FS);
135 /* set codec BCLK division for sample rate */
136 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
140 /* set prescaler division for sample rate */
141 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
142 S3C24XX_PRESCALE(4, 4));
146 /* codec PLL input is PCLK/4 */
147 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1,
148 iis_clkrate / 4, pll_out);
155 static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
157 struct snd_soc_pcm_runtime *rtd = substream->private_data;
158 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
160 DBG("Entered %s\n", __func__);
162 /* disable the PLL */
163 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0);
167 * Neo1973 WM8753 HiFi DAI opserations.
169 static struct snd_soc_ops neo1973_hifi_ops = {
170 .hw_params = neo1973_hifi_hw_params,
171 .hw_free = neo1973_hifi_hw_free,
174 static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
175 struct snd_pcm_hw_params *params)
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
178 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
179 unsigned int pcmdiv = 0;
181 unsigned long iis_clkrate;
183 DBG("Entered %s\n", __func__);
185 iis_clkrate = s3c24xx_i2s_get_clockrate();
187 if (params_rate(params) != 8000)
189 if (params_channels(params) != 1)
192 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
194 /* todo: gg check mode (DSP_B) against CSR datasheet */
195 /* set codec DAI configuration */
196 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
197 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
201 /* set the codec system clock for DAC and ADC */
202 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
207 /* set codec PCM division for sample rate */
208 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
212 /* configue and enable PLL for 12.288MHz output */
213 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2,
214 iis_clkrate / 4, 12288000);
221 static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
223 struct snd_soc_pcm_runtime *rtd = substream->private_data;
224 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
226 DBG("Entered %s\n", __func__);
228 /* disable the PLL */
229 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0);
232 static struct snd_soc_ops neo1973_voice_ops = {
233 .hw_params = neo1973_voice_hw_params,
234 .hw_free = neo1973_voice_hw_free,
237 static int neo1973_scenario;
239 static int neo1973_get_scenario(struct snd_kcontrol *kcontrol,
240 struct snd_ctl_elem_value *ucontrol)
242 ucontrol->value.integer.value[0] = neo1973_scenario;
246 static int set_scenario_endpoints(struct snd_soc_codec *codec, int scenario)
248 DBG("Entered %s\n", __func__);
250 switch (neo1973_scenario) {
252 snd_soc_dapm_disable_pin(codec, "Audio Out");
253 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
254 snd_soc_dapm_disable_pin(codec, "GSM Line In");
255 snd_soc_dapm_disable_pin(codec, "Headset Mic");
256 snd_soc_dapm_disable_pin(codec, "Call Mic");
258 case NEO_GSM_CALL_AUDIO_HANDSET:
259 snd_soc_dapm_enable_pin(codec, "Audio Out");
260 snd_soc_dapm_enable_pin(codec, "GSM Line Out");
261 snd_soc_dapm_enable_pin(codec, "GSM Line In");
262 snd_soc_dapm_disable_pin(codec, "Headset Mic");
263 snd_soc_dapm_enable_pin(codec, "Call Mic");
265 case NEO_GSM_CALL_AUDIO_HEADSET:
266 snd_soc_dapm_enable_pin(codec, "Audio Out");
267 snd_soc_dapm_enable_pin(codec, "GSM Line Out");
268 snd_soc_dapm_enable_pin(codec, "GSM Line In");
269 snd_soc_dapm_enable_pin(codec, "Headset Mic");
270 snd_soc_dapm_disable_pin(codec, "Call Mic");
272 case NEO_GSM_CALL_AUDIO_BLUETOOTH:
273 snd_soc_dapm_disable_pin(codec, "Audio Out");
274 snd_soc_dapm_enable_pin(codec, "GSM Line Out");
275 snd_soc_dapm_enable_pin(codec, "GSM Line In");
276 snd_soc_dapm_disable_pin(codec, "Headset Mic");
277 snd_soc_dapm_disable_pin(codec, "Call Mic");
279 case NEO_STEREO_TO_SPEAKERS:
280 snd_soc_dapm_enable_pin(codec, "Audio Out");
281 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
282 snd_soc_dapm_disable_pin(codec, "GSM Line In");
283 snd_soc_dapm_disable_pin(codec, "Headset Mic");
284 snd_soc_dapm_disable_pin(codec, "Call Mic");
286 case NEO_STEREO_TO_HEADPHONES:
287 snd_soc_dapm_enable_pin(codec, "Audio Out");
288 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
289 snd_soc_dapm_disable_pin(codec, "GSM Line In");
290 snd_soc_dapm_disable_pin(codec, "Headset Mic");
291 snd_soc_dapm_disable_pin(codec, "Call Mic");
293 case NEO_CAPTURE_HANDSET:
294 snd_soc_dapm_disable_pin(codec, "Audio Out");
295 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
296 snd_soc_dapm_disable_pin(codec, "GSM Line In");
297 snd_soc_dapm_disable_pin(codec, "Headset Mic");
298 snd_soc_dapm_enable_pin(codec, "Call Mic");
300 case NEO_CAPTURE_HEADSET:
301 snd_soc_dapm_disable_pin(codec, "Audio Out");
302 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
303 snd_soc_dapm_disable_pin(codec, "GSM Line In");
304 snd_soc_dapm_enable_pin(codec, "Headset Mic");
305 snd_soc_dapm_disable_pin(codec, "Call Mic");
307 case NEO_CAPTURE_BLUETOOTH:
308 snd_soc_dapm_disable_pin(codec, "Audio Out");
309 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
310 snd_soc_dapm_disable_pin(codec, "GSM Line In");
311 snd_soc_dapm_disable_pin(codec, "Headset Mic");
312 snd_soc_dapm_disable_pin(codec, "Call Mic");
315 snd_soc_dapm_disable_pin(codec, "Audio Out");
316 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
317 snd_soc_dapm_disable_pin(codec, "GSM Line In");
318 snd_soc_dapm_disable_pin(codec, "Headset Mic");
319 snd_soc_dapm_disable_pin(codec, "Call Mic");
322 snd_soc_dapm_sync(codec);
327 static int neo1973_set_scenario(struct snd_kcontrol *kcontrol,
328 struct snd_ctl_elem_value *ucontrol)
330 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
332 DBG("Entered %s\n", __func__);
334 if (neo1973_scenario == ucontrol->value.integer.value[0])
337 neo1973_scenario = ucontrol->value.integer.value[0];
338 set_scenario_endpoints(codec, neo1973_scenario);
342 static u8 lm4857_regs[4] = {0x00, 0x40, 0x80, 0xC0};
344 static void lm4857_write_regs(void)
346 DBG("Entered %s\n", __func__);
348 if (i2c_master_send(i2c, lm4857_regs, 4) != 4)
349 printk(KERN_ERR "lm4857: i2c write failed\n");
352 static int lm4857_get_reg(struct snd_kcontrol *kcontrol,
353 struct snd_ctl_elem_value *ucontrol)
355 int reg = kcontrol->private_value & 0xFF;
356 int shift = (kcontrol->private_value >> 8) & 0x0F;
357 int mask = (kcontrol->private_value >> 16) & 0xFF;
359 DBG("Entered %s\n", __func__);
361 ucontrol->value.integer.value[0] = (lm4857_regs[reg] >> shift) & mask;
365 static int lm4857_set_reg(struct snd_kcontrol *kcontrol,
366 struct snd_ctl_elem_value *ucontrol)
368 int reg = kcontrol->private_value & 0xFF;
369 int shift = (kcontrol->private_value >> 8) & 0x0F;
370 int mask = (kcontrol->private_value >> 16) & 0xFF;
372 if (((lm4857_regs[reg] >> shift) & mask) ==
373 ucontrol->value.integer.value[0])
376 lm4857_regs[reg] &= ~(mask << shift);
377 lm4857_regs[reg] |= ucontrol->value.integer.value[0] << shift;
382 static int lm4857_get_mode(struct snd_kcontrol *kcontrol,
383 struct snd_ctl_elem_value *ucontrol)
385 u8 value = lm4857_regs[LM4857_CTRL] & 0x0F;
387 DBG("Entered %s\n", __func__);
392 ucontrol->value.integer.value[0] = value;
396 static int lm4857_set_mode(struct snd_kcontrol *kcontrol,
397 struct snd_ctl_elem_value *ucontrol)
399 u8 value = ucontrol->value.integer.value[0];
401 DBG("Entered %s\n", __func__);
406 if ((lm4857_regs[LM4857_CTRL] & 0x0F) == value)
409 lm4857_regs[LM4857_CTRL] &= 0xF0;
410 lm4857_regs[LM4857_CTRL] |= value;
415 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
416 SND_SOC_DAPM_LINE("Audio Out", NULL),
417 SND_SOC_DAPM_LINE("GSM Line Out", NULL),
418 SND_SOC_DAPM_LINE("GSM Line In", NULL),
419 SND_SOC_DAPM_MIC("Headset Mic", NULL),
420 SND_SOC_DAPM_MIC("Call Mic", NULL),
424 static const struct snd_soc_dapm_route dapm_routes[] = {
426 /* Connections to the lm4857 amp */
427 {"Audio Out", NULL, "LOUT1"},
428 {"Audio Out", NULL, "ROUT1"},
430 /* Connections to the GSM Module */
431 {"GSM Line Out", NULL, "MONO1"},
432 {"GSM Line Out", NULL, "MONO2"},
433 {"RXP", NULL, "GSM Line In"},
434 {"RXN", NULL, "GSM Line In"},
436 /* Connections to Headset */
437 {"MIC1", NULL, "Mic Bias"},
438 {"Mic Bias", NULL, "Headset Mic"},
441 {"MIC2", NULL, "Mic Bias"},
442 {"MIC2N", NULL, "Mic Bias"},
443 {"Mic Bias", NULL, "Call Mic"},
445 /* Connect the ALC pins */
446 {"ACIN", NULL, "ACOP"},
449 static const char *lm4857_mode[] = {
453 "Stereo Speakers + Headphones",
457 static const struct soc_enum lm4857_mode_enum[] = {
458 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lm4857_mode), lm4857_mode),
461 static const char *neo_scenarios[] = {
473 static const struct soc_enum neo_scenario_enum[] = {
474 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(neo_scenarios), neo_scenarios),
477 static const DECLARE_TLV_DB_SCALE(stereo_tlv, -4050, 150, 0);
478 static const DECLARE_TLV_DB_SCALE(mono_tlv, -3450, 150, 0);
480 static const struct snd_kcontrol_new wm8753_neo1973_controls[] = {
481 SOC_SINGLE_EXT_TLV("Amp Left Playback Volume", LM4857_LVOL, 0, 31, 0,
482 lm4857_get_reg, lm4857_set_reg, stereo_tlv),
483 SOC_SINGLE_EXT_TLV("Amp Right Playback Volume", LM4857_RVOL, 0, 31, 0,
484 lm4857_get_reg, lm4857_set_reg, stereo_tlv),
485 SOC_SINGLE_EXT_TLV("Amp Mono Playback Volume", LM4857_MVOL, 0, 31, 0,
486 lm4857_get_reg, lm4857_set_reg, mono_tlv),
487 SOC_ENUM_EXT("Amp Mode", lm4857_mode_enum[0],
488 lm4857_get_mode, lm4857_set_mode),
489 SOC_ENUM_EXT("Neo Mode", neo_scenario_enum[0],
490 neo1973_get_scenario, neo1973_set_scenario),
491 SOC_SINGLE_EXT("Amp Spk 3D Playback Switch", LM4857_LVOL, 5, 1, 0,
492 lm4857_get_reg, lm4857_set_reg),
493 SOC_SINGLE_EXT("Amp HP 3d Playback Switch", LM4857_RVOL, 5, 1, 0,
494 lm4857_get_reg, lm4857_set_reg),
495 SOC_SINGLE_EXT("Amp Fast Wakeup Playback Switch", LM4857_CTRL, 5, 1, 0,
496 lm4857_get_reg, lm4857_set_reg),
497 SOC_SINGLE_EXT("Amp Earpiece 6dB Playback Switch", LM4857_CTRL, 4, 1, 0,
498 lm4857_get_reg, lm4857_set_reg),
502 * This is an example machine initialisation for a wm8753 connected to a
503 * neo1973 II. It is missing logic to detect hp/mic insertions and logic
504 * to re-route the audio in such an event.
506 static int neo1973_wm8753_init(struct snd_soc_codec *codec)
510 DBG("Entered %s\n", __func__);
512 /* set up NC codec pins */
513 snd_soc_dapm_disable_pin(codec, "LOUT2");
514 snd_soc_dapm_disable_pin(codec, "ROUT2");
515 snd_soc_dapm_disable_pin(codec, "OUT3");
516 snd_soc_dapm_disable_pin(codec, "OUT4");
517 snd_soc_dapm_disable_pin(codec, "LINE1");
518 snd_soc_dapm_disable_pin(codec, "LINE2");
521 /* set endpoints to default mode */
522 set_scenario_endpoints(codec, NEO_AUDIO_OFF);
524 /* Add neo1973 specific widgets */
525 snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
526 ARRAY_SIZE(wm8753_dapm_widgets));
528 /* add neo1973 specific controls */
529 for (i = 0; i < ARRAY_SIZE(wm8753_neo1973_controls); i++) {
530 err = snd_ctl_add(codec->card,
531 snd_soc_cnew(&wm8753_neo1973_controls[i],
537 /* set up neo1973 specific audio routes */
538 err = snd_soc_dapm_add_routes(codec, dapm_routes,
539 ARRAY_SIZE(dapm_routes));
541 snd_soc_dapm_sync(codec);
548 static struct snd_soc_dai bt_dai = {
551 .type = SND_SOC_DAI_PCM,
555 .rates = SNDRV_PCM_RATE_8000,
556 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
560 .rates = SNDRV_PCM_RATE_8000,
561 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
564 static struct snd_soc_dai_link neo1973_dai[] = {
565 { /* Hifi Playback - for similatious use with voice below */
567 .stream_name = "WM8753 HiFi",
568 .cpu_dai = &s3c24xx_i2s_dai,
569 .codec_dai = &wm8753_dai[WM8753_DAI_HIFI],
570 .init = neo1973_wm8753_init,
571 .ops = &neo1973_hifi_ops,
575 .stream_name = "Voice",
577 .codec_dai = &wm8753_dai[WM8753_DAI_VOICE],
578 .ops = &neo1973_voice_ops,
582 static struct snd_soc_machine neo1973 = {
584 .dai_link = neo1973_dai,
585 .num_links = ARRAY_SIZE(neo1973_dai),
588 static struct wm8753_setup_data neo1973_wm8753_setup = {
592 static struct snd_soc_device neo1973_snd_devdata = {
594 .platform = &s3c24xx_soc_platform,
595 .codec_dev = &soc_codec_dev_wm8753,
596 .codec_data = &neo1973_wm8753_setup,
599 static struct i2c_client client_template;
601 static const unsigned short normal_i2c[] = { 0x7C, I2C_CLIENT_END };
603 /* Magic definition of all other variables and things */
606 static int lm4857_amp_probe(struct i2c_adapter *adap, int addr, int kind)
610 DBG("Entered %s\n", __func__);
612 client_template.adapter = adap;
613 client_template.addr = addr;
615 i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
619 ret = i2c_attach_client(i2c);
621 printk(KERN_ERR "LM4857 failed to attach at addr %x\n", addr);
633 static int lm4857_i2c_detach(struct i2c_client *client)
635 DBG("Entered %s\n", __func__);
637 i2c_detach_client(client);
642 static int lm4857_i2c_attach(struct i2c_adapter *adap)
644 DBG("Entered %s\n", __func__);
646 return i2c_probe(adap, &addr_data, lm4857_amp_probe);
649 static u8 lm4857_state;
651 static int lm4857_suspend(struct i2c_client *dev, pm_message_t state)
653 DBG("Entered %s\n", __func__);
655 dev_dbg(&dev->dev, "lm4857_suspend\n");
656 lm4857_state = lm4857_regs[LM4857_CTRL] & 0xf;
658 lm4857_regs[LM4857_CTRL] &= 0xf0;
664 static int lm4857_resume(struct i2c_client *dev)
666 DBG("Entered %s\n", __func__);
669 lm4857_regs[LM4857_CTRL] |= (lm4857_state & 0x0f);
675 static void lm4857_shutdown(struct i2c_client *dev)
677 DBG("Entered %s\n", __func__);
679 dev_dbg(&dev->dev, "lm4857_shutdown\n");
680 lm4857_regs[LM4857_CTRL] &= 0xf0;
684 /* corgi i2c codec control layer */
685 static struct i2c_driver lm4857_i2c_driver = {
687 .name = "LM4857 I2C Amp",
688 .owner = THIS_MODULE,
690 .id = I2C_DRIVERID_LM4857,
691 .suspend = lm4857_suspend,
692 .resume = lm4857_resume,
693 .shutdown = lm4857_shutdown,
694 .attach_adapter = lm4857_i2c_attach,
695 .detach_client = lm4857_i2c_detach,
699 static struct i2c_client client_template = {
701 .driver = &lm4857_i2c_driver,
704 static struct platform_device *neo1973_snd_device;
706 static int __init neo1973_init(void)
710 DBG("Entered %s\n", __func__);
712 neo1973_snd_device = platform_device_alloc("soc-audio", -1);
713 if (!neo1973_snd_device)
716 platform_set_drvdata(neo1973_snd_device, &neo1973_snd_devdata);
717 neo1973_snd_devdata.dev = &neo1973_snd_device->dev;
718 ret = platform_device_add(neo1973_snd_device);
721 platform_device_put(neo1973_snd_device);
723 ret = i2c_add_driver(&lm4857_i2c_driver);
725 printk(KERN_ERR "can't add i2c driver");
730 static void __exit neo1973_exit(void)
732 DBG("Entered %s\n", __func__);
734 i2c_del_driver(&lm4857_i2c_driver);
735 platform_device_unregister(neo1973_snd_device);
738 module_init(neo1973_init);
739 module_exit(neo1973_exit);
741 /* Module information */
742 MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
743 MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973");
744 MODULE_LICENSE("GPL");