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 <mach/regs-clock.h>
30 #include <mach/regs-gpio.h>
31 #include <mach/hardware.h>
32 #include <plat/audio.h>
34 #include <mach/spi-gpio.h>
36 #include <plat/regs-iis.h>
38 #include "../codecs/wm8753.h"
40 #include "s3c24xx-pcm.h"
41 #include "s3c24xx-i2s.h"
43 /* define the scenarios */
44 #define NEO_AUDIO_OFF 0
45 #define NEO_GSM_CALL_AUDIO_HANDSET 1
46 #define NEO_GSM_CALL_AUDIO_HEADSET 2
47 #define NEO_GSM_CALL_AUDIO_BLUETOOTH 3
48 #define NEO_STEREO_TO_SPEAKERS 4
49 #define NEO_STEREO_TO_HEADPHONES 5
50 #define NEO_CAPTURE_HANDSET 6
51 #define NEO_CAPTURE_HEADSET 7
52 #define NEO_CAPTURE_BLUETOOTH 8
54 static struct snd_soc_card neo1973;
55 static struct i2c_client *i2c;
57 static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
58 struct snd_pcm_hw_params *params)
60 struct snd_soc_pcm_runtime *rtd = substream->private_data;
61 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
62 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
63 unsigned int pll_out = 0, bclk = 0;
65 unsigned long iis_clkrate;
67 pr_debug("Entered %s\n", __func__);
69 iis_clkrate = s3c24xx_i2s_get_clockrate();
71 switch (params_rate(params)) {
77 bclk = WM8753_BCLK_DIV_4;
81 bclk = WM8753_BCLK_DIV_2;
85 bclk = WM8753_BCLK_DIV_16;
89 bclk = WM8753_BCLK_DIV_8;
93 bclk = WM8753_BCLK_DIV_4;
97 bclk = WM8753_BCLK_DIV_2;
102 /* set codec DAI configuration */
103 ret = snd_soc_dai_set_fmt(codec_dai,
104 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
105 SND_SOC_DAIFMT_CBM_CFM);
109 /* set cpu DAI configuration */
110 ret = snd_soc_dai_set_fmt(cpu_dai,
111 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
112 SND_SOC_DAIFMT_CBM_CFM);
116 /* set the codec system clock for DAC and ADC */
117 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
122 /* set MCLK division for sample rate */
123 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
124 S3C2410_IISMOD_32FS);
128 /* set codec BCLK division for sample rate */
129 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
133 /* set prescaler division for sample rate */
134 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
135 S3C24XX_PRESCALE(4, 4));
139 /* codec PLL input is PCLK/4 */
140 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1,
141 iis_clkrate / 4, pll_out);
148 static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
150 struct snd_soc_pcm_runtime *rtd = substream->private_data;
151 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
153 pr_debug("Entered %s\n", __func__);
155 /* disable the PLL */
156 return snd_soc_dai_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_dai *codec_dai = rtd->dai->codec_dai;
172 unsigned int pcmdiv = 0;
174 unsigned long iis_clkrate;
176 pr_debug("Entered %s\n", __func__);
178 iis_clkrate = s3c24xx_i2s_get_clockrate();
180 if (params_rate(params) != 8000)
182 if (params_channels(params) != 1)
185 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
187 /* todo: gg check mode (DSP_B) against CSR datasheet */
188 /* set codec DAI configuration */
189 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
190 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
194 /* set the codec system clock for DAC and ADC */
195 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
200 /* set codec PCM division for sample rate */
201 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
205 /* configue and enable PLL for 12.288MHz output */
206 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2,
207 iis_clkrate / 4, 12288000);
214 static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
216 struct snd_soc_pcm_runtime *rtd = substream->private_data;
217 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
219 pr_debug("Entered %s\n", __func__);
221 /* disable the PLL */
222 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0);
225 static struct snd_soc_ops neo1973_voice_ops = {
226 .hw_params = neo1973_voice_hw_params,
227 .hw_free = neo1973_voice_hw_free,
230 static int neo1973_scenario;
232 static int neo1973_get_scenario(struct snd_kcontrol *kcontrol,
233 struct snd_ctl_elem_value *ucontrol)
235 ucontrol->value.integer.value[0] = neo1973_scenario;
239 static int set_scenario_endpoints(struct snd_soc_codec *codec, int scenario)
241 pr_debug("Entered %s\n", __func__);
243 switch (neo1973_scenario) {
245 snd_soc_dapm_disable_pin(codec, "Audio Out");
246 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
247 snd_soc_dapm_disable_pin(codec, "GSM Line In");
248 snd_soc_dapm_disable_pin(codec, "Headset Mic");
249 snd_soc_dapm_disable_pin(codec, "Call Mic");
251 case NEO_GSM_CALL_AUDIO_HANDSET:
252 snd_soc_dapm_enable_pin(codec, "Audio Out");
253 snd_soc_dapm_enable_pin(codec, "GSM Line Out");
254 snd_soc_dapm_enable_pin(codec, "GSM Line In");
255 snd_soc_dapm_disable_pin(codec, "Headset Mic");
256 snd_soc_dapm_enable_pin(codec, "Call Mic");
258 case NEO_GSM_CALL_AUDIO_HEADSET:
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_enable_pin(codec, "Headset Mic");
263 snd_soc_dapm_disable_pin(codec, "Call Mic");
265 case NEO_GSM_CALL_AUDIO_BLUETOOTH:
266 snd_soc_dapm_disable_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_disable_pin(codec, "Headset Mic");
270 snd_soc_dapm_disable_pin(codec, "Call Mic");
272 case NEO_STEREO_TO_SPEAKERS:
273 snd_soc_dapm_enable_pin(codec, "Audio Out");
274 snd_soc_dapm_disable_pin(codec, "GSM Line Out");
275 snd_soc_dapm_disable_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_HEADPHONES:
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_CAPTURE_HANDSET:
287 snd_soc_dapm_disable_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_enable_pin(codec, "Call Mic");
293 case NEO_CAPTURE_HEADSET:
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_enable_pin(codec, "Headset Mic");
298 snd_soc_dapm_disable_pin(codec, "Call Mic");
300 case NEO_CAPTURE_BLUETOOTH:
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_disable_pin(codec, "Headset Mic");
305 snd_soc_dapm_disable_pin(codec, "Call Mic");
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_sync(codec);
320 static int neo1973_set_scenario(struct snd_kcontrol *kcontrol,
321 struct snd_ctl_elem_value *ucontrol)
323 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
325 pr_debug("Entered %s\n", __func__);
327 if (neo1973_scenario == ucontrol->value.integer.value[0])
330 neo1973_scenario = ucontrol->value.integer.value[0];
331 set_scenario_endpoints(codec, neo1973_scenario);
335 static u8 lm4857_regs[4] = {0x00, 0x40, 0x80, 0xC0};
337 static void lm4857_write_regs(void)
339 pr_debug("Entered %s\n", __func__);
341 if (i2c_master_send(i2c, lm4857_regs, 4) != 4)
342 printk(KERN_ERR "lm4857: i2c write failed\n");
345 static int lm4857_get_reg(struct snd_kcontrol *kcontrol,
346 struct snd_ctl_elem_value *ucontrol)
348 int reg = kcontrol->private_value & 0xFF;
349 int shift = (kcontrol->private_value >> 8) & 0x0F;
350 int mask = (kcontrol->private_value >> 16) & 0xFF;
352 pr_debug("Entered %s\n", __func__);
354 ucontrol->value.integer.value[0] = (lm4857_regs[reg] >> shift) & mask;
358 static int lm4857_set_reg(struct snd_kcontrol *kcontrol,
359 struct snd_ctl_elem_value *ucontrol)
361 int reg = kcontrol->private_value & 0xFF;
362 int shift = (kcontrol->private_value >> 8) & 0x0F;
363 int mask = (kcontrol->private_value >> 16) & 0xFF;
365 if (((lm4857_regs[reg] >> shift) & mask) ==
366 ucontrol->value.integer.value[0])
369 lm4857_regs[reg] &= ~(mask << shift);
370 lm4857_regs[reg] |= ucontrol->value.integer.value[0] << shift;
375 static int lm4857_get_mode(struct snd_kcontrol *kcontrol,
376 struct snd_ctl_elem_value *ucontrol)
378 u8 value = lm4857_regs[LM4857_CTRL] & 0x0F;
380 pr_debug("Entered %s\n", __func__);
385 ucontrol->value.integer.value[0] = value;
389 static int lm4857_set_mode(struct snd_kcontrol *kcontrol,
390 struct snd_ctl_elem_value *ucontrol)
392 u8 value = ucontrol->value.integer.value[0];
394 pr_debug("Entered %s\n", __func__);
399 if ((lm4857_regs[LM4857_CTRL] & 0x0F) == value)
402 lm4857_regs[LM4857_CTRL] &= 0xF0;
403 lm4857_regs[LM4857_CTRL] |= value;
408 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
409 SND_SOC_DAPM_LINE("Audio Out", NULL),
410 SND_SOC_DAPM_LINE("GSM Line Out", NULL),
411 SND_SOC_DAPM_LINE("GSM Line In", NULL),
412 SND_SOC_DAPM_MIC("Headset Mic", NULL),
413 SND_SOC_DAPM_MIC("Call Mic", NULL),
417 static const struct snd_soc_dapm_route dapm_routes[] = {
419 /* Connections to the lm4857 amp */
420 {"Audio Out", NULL, "LOUT1"},
421 {"Audio Out", NULL, "ROUT1"},
423 /* Connections to the GSM Module */
424 {"GSM Line Out", NULL, "MONO1"},
425 {"GSM Line Out", NULL, "MONO2"},
426 {"RXP", NULL, "GSM Line In"},
427 {"RXN", NULL, "GSM Line In"},
429 /* Connections to Headset */
430 {"MIC1", NULL, "Mic Bias"},
431 {"Mic Bias", NULL, "Headset Mic"},
434 {"MIC2", NULL, "Mic Bias"},
435 {"MIC2N", NULL, "Mic Bias"},
436 {"Mic Bias", NULL, "Call Mic"},
438 /* Connect the ALC pins */
439 {"ACIN", NULL, "ACOP"},
442 static const char *lm4857_mode[] = {
446 "Stereo Speakers + Headphones",
450 static const struct soc_enum lm4857_mode_enum[] = {
451 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lm4857_mode), lm4857_mode),
454 static const char *neo_scenarios[] = {
466 static const struct soc_enum neo_scenario_enum[] = {
467 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(neo_scenarios), neo_scenarios),
470 static const DECLARE_TLV_DB_SCALE(stereo_tlv, -4050, 150, 0);
471 static const DECLARE_TLV_DB_SCALE(mono_tlv, -3450, 150, 0);
473 static const struct snd_kcontrol_new wm8753_neo1973_controls[] = {
474 SOC_SINGLE_EXT_TLV("Amp Left Playback Volume", LM4857_LVOL, 0, 31, 0,
475 lm4857_get_reg, lm4857_set_reg, stereo_tlv),
476 SOC_SINGLE_EXT_TLV("Amp Right Playback Volume", LM4857_RVOL, 0, 31, 0,
477 lm4857_get_reg, lm4857_set_reg, stereo_tlv),
478 SOC_SINGLE_EXT_TLV("Amp Mono Playback Volume", LM4857_MVOL, 0, 31, 0,
479 lm4857_get_reg, lm4857_set_reg, mono_tlv),
480 SOC_ENUM_EXT("Amp Mode", lm4857_mode_enum[0],
481 lm4857_get_mode, lm4857_set_mode),
482 SOC_ENUM_EXT("Neo Mode", neo_scenario_enum[0],
483 neo1973_get_scenario, neo1973_set_scenario),
484 SOC_SINGLE_EXT("Amp Spk 3D Playback Switch", LM4857_LVOL, 5, 1, 0,
485 lm4857_get_reg, lm4857_set_reg),
486 SOC_SINGLE_EXT("Amp HP 3d Playback Switch", LM4857_RVOL, 5, 1, 0,
487 lm4857_get_reg, lm4857_set_reg),
488 SOC_SINGLE_EXT("Amp Fast Wakeup Playback Switch", LM4857_CTRL, 5, 1, 0,
489 lm4857_get_reg, lm4857_set_reg),
490 SOC_SINGLE_EXT("Amp Earpiece 6dB Playback Switch", LM4857_CTRL, 4, 1, 0,
491 lm4857_get_reg, lm4857_set_reg),
495 * This is an example machine initialisation for a wm8753 connected to a
496 * neo1973 II. It is missing logic to detect hp/mic insertions and logic
497 * to re-route the audio in such an event.
499 static int neo1973_wm8753_init(struct snd_soc_codec *codec)
503 pr_debug("Entered %s\n", __func__);
505 /* set up NC codec pins */
506 snd_soc_dapm_nc_pin(codec, "LOUT2");
507 snd_soc_dapm_nc_pin(codec, "ROUT2");
508 snd_soc_dapm_nc_pin(codec, "OUT3");
509 snd_soc_dapm_nc_pin(codec, "OUT4");
510 snd_soc_dapm_nc_pin(codec, "LINE1");
511 snd_soc_dapm_nc_pin(codec, "LINE2");
513 /* Add neo1973 specific widgets */
514 snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
515 ARRAY_SIZE(wm8753_dapm_widgets));
517 /* set endpoints to default mode */
518 set_scenario_endpoints(codec, NEO_AUDIO_OFF);
520 /* add neo1973 specific controls */
521 err = snd_soc_add_controls(codec, wm8753_neo1973_controls,
522 ARRAY_SIZE(8753_neo1973_controls));
526 /* set up neo1973 specific audio routes */
527 err = snd_soc_dapm_add_routes(codec, dapm_routes,
528 ARRAY_SIZE(dapm_routes));
530 snd_soc_dapm_sync(codec);
537 static struct snd_soc_dai bt_dai = {
543 .rates = SNDRV_PCM_RATE_8000,
544 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
548 .rates = SNDRV_PCM_RATE_8000,
549 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
552 static struct snd_soc_dai_link neo1973_dai[] = {
553 { /* Hifi Playback - for similatious use with voice below */
555 .stream_name = "WM8753 HiFi",
556 .cpu_dai = &s3c24xx_i2s_dai,
557 .codec_dai = &wm8753_dai[WM8753_DAI_HIFI],
558 .init = neo1973_wm8753_init,
559 .ops = &neo1973_hifi_ops,
563 .stream_name = "Voice",
565 .codec_dai = &wm8753_dai[WM8753_DAI_VOICE],
566 .ops = &neo1973_voice_ops,
570 static struct snd_soc_card neo1973 = {
572 .platform = &s3c24xx_soc_platform,
573 .dai_link = neo1973_dai,
574 .num_links = ARRAY_SIZE(neo1973_dai),
577 static struct snd_soc_device neo1973_snd_devdata = {
579 .codec_dev = &soc_codec_dev_wm8753,
582 static int lm4857_i2c_probe(struct i2c_client *client,
583 const struct i2c_device_id *id)
585 pr_debug("Entered %s\n", __func__);
593 static int lm4857_i2c_remove(struct i2c_client *client)
595 pr_debug("Entered %s\n", __func__);
602 static u8 lm4857_state;
604 static int lm4857_suspend(struct i2c_client *dev, pm_message_t state)
606 pr_debug("Entered %s\n", __func__);
608 dev_dbg(&dev->dev, "lm4857_suspend\n");
609 lm4857_state = lm4857_regs[LM4857_CTRL] & 0xf;
611 lm4857_regs[LM4857_CTRL] &= 0xf0;
617 static int lm4857_resume(struct i2c_client *dev)
619 pr_debug("Entered %s\n", __func__);
622 lm4857_regs[LM4857_CTRL] |= (lm4857_state & 0x0f);
628 static void lm4857_shutdown(struct i2c_client *dev)
630 pr_debug("Entered %s\n", __func__);
632 dev_dbg(&dev->dev, "lm4857_shutdown\n");
633 lm4857_regs[LM4857_CTRL] &= 0xf0;
637 static const struct i2c_device_id lm4857_i2c_id[] = {
638 { "neo1973_lm4857", 0 },
642 static struct i2c_driver lm4857_i2c_driver = {
644 .name = "LM4857 I2C Amp",
645 .owner = THIS_MODULE,
647 .suspend = lm4857_suspend,
648 .resume = lm4857_resume,
649 .shutdown = lm4857_shutdown,
650 .probe = lm4857_i2c_probe,
651 .remove = lm4857_i2c_remove,
652 .id_table = lm4857_i2c_id,
655 static struct platform_device *neo1973_snd_device;
657 static int __init neo1973_init(void)
661 pr_debug("Entered %s\n", __func__);
663 if (!machine_is_neo1973_gta01()) {
665 "Only GTA01 hardware supported by ASoC driver\n");
669 neo1973_snd_device = platform_device_alloc("soc-audio", -1);
670 if (!neo1973_snd_device)
673 platform_set_drvdata(neo1973_snd_device, &neo1973_snd_devdata);
674 neo1973_snd_devdata.dev = &neo1973_snd_device->dev;
675 ret = platform_device_add(neo1973_snd_device);
678 platform_device_put(neo1973_snd_device);
682 ret = i2c_add_driver(&lm4857_i2c_driver);
685 platform_device_unregister(neo1973_snd_device);
690 static void __exit neo1973_exit(void)
692 pr_debug("Entered %s\n", __func__);
694 i2c_del_driver(&lm4857_i2c_driver);
695 platform_device_unregister(neo1973_snd_device);
698 module_init(neo1973_init);
699 module_exit(neo1973_exit);
701 /* Module information */
702 MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
703 MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973");
704 MODULE_LICENSE("GPL");