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/core.h>
26 #include <sound/pcm.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
30 #include <asm/mach-types.h>
31 #include <asm/hardware/scoop.h>
32 #include <asm/arch/regs-clock.h>
33 #include <asm/arch/regs-gpio.h>
34 #include <asm/hardware.h>
35 #include <asm/arch/audio.h>
37 #include <asm/arch/spi-gpio.h>
39 #include <asm/plat-s3c24xx/regs-iis.h>
41 #include "../codecs/wm8753.h"
43 #include "s3c24xx-pcm.h"
44 #include "s3c24xx-i2s.h"
46 /* define the scenarios */
47 #define NEO_AUDIO_OFF 0
48 #define NEO_GSM_CALL_AUDIO_HANDSET 1
49 #define NEO_GSM_CALL_AUDIO_HEADSET 2
50 #define NEO_GSM_CALL_AUDIO_BLUETOOTH 3
51 #define NEO_STEREO_TO_SPEAKERS 4
52 #define NEO_STEREO_TO_HEADPHONES 5
53 #define NEO_CAPTURE_HANDSET 6
54 #define NEO_CAPTURE_HEADSET 7
55 #define NEO_CAPTURE_BLUETOOTH 8
57 static struct snd_soc_machine neo1973;
58 static struct i2c_client *i2c;
60 static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
61 struct snd_pcm_hw_params *params)
63 struct snd_soc_pcm_runtime *rtd = substream->private_data;
64 struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
65 struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
66 unsigned int pll_out = 0, bclk = 0;
68 unsigned long iis_clkrate;
70 iis_clkrate = s3c24xx_i2s_get_clockrate();
72 switch (params_rate(params)) {
78 bclk = WM8753_BCLK_DIV_4;
82 bclk = WM8753_BCLK_DIV_2;
86 bclk = WM8753_BCLK_DIV_16;
90 bclk = WM8753_BCLK_DIV_8;
94 bclk = WM8753_BCLK_DIV_4;
98 bclk = WM8753_BCLK_DIV_2;
103 /* set codec DAI configuration */
104 ret = codec_dai->dai_ops.set_fmt(codec_dai,
105 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
106 SND_SOC_DAIFMT_CBM_CFM);
110 /* set cpu DAI configuration */
111 ret = cpu_dai->dai_ops.set_fmt(cpu_dai,
112 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
113 SND_SOC_DAIFMT_CBM_CFM);
117 /* set the codec system clock for DAC and ADC */
118 ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8753_MCLK, pll_out,
123 /* set MCLK division for sample rate */
124 ret = cpu_dai->dai_ops.set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
125 S3C2410_IISMOD_32FS);
129 /* set codec BCLK division for sample rate */
130 ret = codec_dai->dai_ops.set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
134 /* set prescaler division for sample rate */
135 ret = cpu_dai->dai_ops.set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
136 S3C24XX_PRESCALE(4, 4));
140 /* codec PLL input is PCLK/4 */
141 ret = codec_dai->dai_ops.set_pll(codec_dai, WM8753_PLL1,
142 iis_clkrate / 4, pll_out);
149 static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
151 struct snd_soc_pcm_runtime *rtd = substream->private_data;
152 struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
154 /* disable the PLL */
155 return codec_dai->dai_ops.set_pll(codec_dai, WM8753_PLL1, 0, 0);
159 * Neo1973 WM8753 HiFi DAI opserations.
161 static struct snd_soc_ops neo1973_hifi_ops = {
162 .hw_params = neo1973_hifi_hw_params,
163 .hw_free = neo1973_hifi_hw_free,
166 static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
167 struct snd_pcm_hw_params *params)
169 struct snd_soc_pcm_runtime *rtd = substream->private_data;
170 struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
171 unsigned int pcmdiv = 0;
173 unsigned long iis_clkrate;
175 iis_clkrate = s3c24xx_i2s_get_clockrate();
177 if (params_rate(params) != 8000)
179 if (params_channels(params) != 1)
182 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
184 /* todo: gg check mode (DSP_B) against CSR datasheet */
185 /* set codec DAI configuration */
186 ret = codec_dai->dai_ops.set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
187 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
191 /* set the codec system clock for DAC and ADC */
192 ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
197 /* set codec PCM division for sample rate */
198 ret = codec_dai->dai_ops.set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
202 /* configue and enable PLL for 12.288MHz output */
203 ret = codec_dai->dai_ops.set_pll(codec_dai, WM8753_PLL2,
204 iis_clkrate / 4, 12288000);
211 static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
213 struct snd_soc_pcm_runtime *rtd = substream->private_data;
214 struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
216 /* disable the PLL */
217 return codec_dai->dai_ops.set_pll(codec_dai, WM8753_PLL2, 0, 0);
220 static struct snd_soc_ops neo1973_voice_ops = {
221 .hw_params = neo1973_voice_hw_params,
222 .hw_free = neo1973_voice_hw_free,
225 static int neo1973_scenario;
227 static int neo1973_get_scenario(struct snd_kcontrol *kcontrol,
228 struct snd_ctl_elem_value *ucontrol)
230 ucontrol->value.integer.value[0] = neo1973_scenario;
234 static int set_scenario_endpoints(struct snd_soc_codec *codec, int scenario)
236 switch (neo1973_scenario) {
238 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
239 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
240 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
241 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
242 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
244 case NEO_GSM_CALL_AUDIO_HANDSET:
245 snd_soc_dapm_set_endpoint(codec, "Audio Out", 1);
246 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 1);
247 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 1);
248 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
249 snd_soc_dapm_set_endpoint(codec, "Call Mic", 1);
251 case NEO_GSM_CALL_AUDIO_HEADSET:
252 snd_soc_dapm_set_endpoint(codec, "Audio Out", 1);
253 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 1);
254 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 1);
255 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 1);
256 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
258 case NEO_GSM_CALL_AUDIO_BLUETOOTH:
259 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
260 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 1);
261 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 1);
262 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
263 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
265 case NEO_STEREO_TO_SPEAKERS:
266 snd_soc_dapm_set_endpoint(codec, "Audio Out", 1);
267 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
268 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
269 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
270 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
272 case NEO_STEREO_TO_HEADPHONES:
273 snd_soc_dapm_set_endpoint(codec, "Audio Out", 1);
274 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
275 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
276 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
277 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
279 case NEO_CAPTURE_HANDSET:
280 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
281 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
282 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
283 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
284 snd_soc_dapm_set_endpoint(codec, "Call Mic", 1);
286 case NEO_CAPTURE_HEADSET:
287 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
288 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
289 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
290 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 1);
291 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
293 case NEO_CAPTURE_BLUETOOTH:
294 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
295 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
296 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
297 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
298 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
301 snd_soc_dapm_set_endpoint(codec, "Audio Out", 0);
302 snd_soc_dapm_set_endpoint(codec, "GSM Line Out", 0);
303 snd_soc_dapm_set_endpoint(codec, "GSM Line In", 0);
304 snd_soc_dapm_set_endpoint(codec, "Headset Mic", 0);
305 snd_soc_dapm_set_endpoint(codec, "Call Mic", 0);
308 snd_soc_dapm_sync_endpoints(codec);
313 static int neo1973_set_scenario(struct snd_kcontrol *kcontrol,
314 struct snd_ctl_elem_value *ucontrol)
316 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
318 if (neo1973_scenario == ucontrol->value.integer.value[0])
321 neo1973_scenario = ucontrol->value.integer.value[0];
322 set_scenario_endpoints(codec, neo1973_scenario);
326 static u8 lm4857_regs[4] = {0x00, 0x40, 0x80, 0xC0};
328 static void lm4857_write_regs(void)
330 if (i2c_master_send(i2c, lm4857_regs, 4) != 4)
331 printk(KERN_ERR "lm4857: i2c write failed\n");
334 static int lm4857_get_reg(struct snd_kcontrol *kcontrol,
335 struct snd_ctl_elem_value *ucontrol)
337 int reg = kcontrol->private_value & 0xFF;
338 int shift = (kcontrol->private_value >> 8) & 0x0F;
339 int mask = (kcontrol->private_value >> 16) & 0xFF;
341 ucontrol->value.integer.value[0] = (lm4857_regs[reg] >> shift) & mask;
345 static int lm4857_set_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 if (((lm4857_regs[reg] >> shift) & mask) ==
353 ucontrol->value.integer.value[0])
356 lm4857_regs[reg] &= ~(mask << shift);
357 lm4857_regs[reg] |= ucontrol->value.integer.value[0] << shift;
362 static int lm4857_get_mode(struct snd_kcontrol *kcontrol,
363 struct snd_ctl_elem_value *ucontrol)
365 u8 value = lm4857_regs[LM4857_CTRL] & 0x0F;
370 ucontrol->value.integer.value[0] = value;
374 static int lm4857_set_mode(struct snd_kcontrol *kcontrol,
375 struct snd_ctl_elem_value *ucontrol)
377 u8 value = ucontrol->value.integer.value[0];
382 if ((lm4857_regs[LM4857_CTRL] & 0x0F) == value)
385 lm4857_regs[LM4857_CTRL] &= 0xF0;
386 lm4857_regs[LM4857_CTRL] |= value;
391 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
392 SND_SOC_DAPM_LINE("Audio Out", NULL),
393 SND_SOC_DAPM_LINE("GSM Line Out", NULL),
394 SND_SOC_DAPM_LINE("GSM Line In", NULL),
395 SND_SOC_DAPM_MIC("Headset Mic", NULL),
396 SND_SOC_DAPM_MIC("Call Mic", NULL),
400 /* example machine audio_mapnections */
401 static const char *audio_map[][3] = {
403 /* Connections to the lm4857 amp */
404 {"Audio Out", NULL, "LOUT1"},
405 {"Audio Out", NULL, "ROUT1"},
407 /* Connections to the GSM Module */
408 {"GSM Line Out", NULL, "MONO1"},
409 {"GSM Line Out", NULL, "MONO2"},
410 {"RXP", NULL, "GSM Line In"},
411 {"RXN", NULL, "GSM Line In"},
413 /* Connections to Headset */
414 {"MIC1", NULL, "Mic Bias"},
415 {"Mic Bias", NULL, "Headset Mic"},
418 {"MIC2", NULL, "Mic Bias"},
419 {"MIC2N", NULL, "Mic Bias"},
420 {"Mic Bias", NULL, "Call Mic"},
422 /* Connect the ALC pins */
423 {"ACIN", NULL, "ACOP"},
428 static const char *lm4857_mode[] = {
432 "Stereo Speakers + Headphones",
436 static const struct soc_enum lm4857_mode_enum[] = {
437 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lm4857_mode), lm4857_mode),
440 static const char *neo_scenarios[] = {
452 static const struct soc_enum neo_scenario_enum[] = {
453 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(neo_scenarios), neo_scenarios),
456 static const struct snd_kcontrol_new wm8753_neo1973_controls[] = {
457 SOC_SINGLE_EXT("Amp Left Playback Volume", LM4857_LVOL, 0, 31, 0,
458 lm4857_get_reg, lm4857_set_reg),
459 SOC_SINGLE_EXT("Amp Right Playback Volume", LM4857_RVOL, 0, 31, 0,
460 lm4857_get_reg, lm4857_set_reg),
461 SOC_SINGLE_EXT("Amp Mono Playback Volume", LM4857_MVOL, 0, 31, 0,
462 lm4857_get_reg, lm4857_set_reg),
463 SOC_ENUM_EXT("Amp Mode", lm4857_mode_enum[0],
464 lm4857_get_mode, lm4857_set_mode),
465 SOC_ENUM_EXT("Neo Mode", neo_scenario_enum[0],
466 neo1973_get_scenario, neo1973_set_scenario),
467 SOC_SINGLE_EXT("Amp Spk 3D Playback Switch", LM4857_LVOL, 5, 1, 0,
468 lm4857_get_reg, lm4857_set_reg),
469 SOC_SINGLE_EXT("Amp HP 3d Playback Switch", LM4857_RVOL, 5, 1, 0,
470 lm4857_get_reg, lm4857_set_reg),
471 SOC_SINGLE_EXT("Amp Fast Wakeup Playback Switch", LM4857_CTRL, 5, 1, 0,
472 lm4857_get_reg, lm4857_set_reg),
473 SOC_SINGLE_EXT("Amp Earpiece 6dB Playback Switch", LM4857_CTRL, 4, 1, 0,
474 lm4857_get_reg, lm4857_set_reg),
478 * This is an example machine initialisation for a wm8753 connected to a
479 * neo1973 II. It is missing logic to detect hp/mic insertions and logic
480 * to re-route the audio in such an event.
482 static int neo1973_wm8753_init(struct snd_soc_codec *codec)
486 /* set up NC codec pins */
487 snd_soc_dapm_set_endpoint(codec, "LOUT2", 0);
488 snd_soc_dapm_set_endpoint(codec, "ROUT2", 0);
489 snd_soc_dapm_set_endpoint(codec, "OUT3", 0);
490 snd_soc_dapm_set_endpoint(codec, "OUT4", 0);
491 snd_soc_dapm_set_endpoint(codec, "LINE1", 0);
492 snd_soc_dapm_set_endpoint(codec, "LINE2", 0);
495 /* set endpoints to default mode */
496 set_scenario_endpoints(codec, NEO_AUDIO_OFF);
498 /* Add neo1973 specific widgets */
499 for (i = 0; i < ARRAY_SIZE(wm8753_dapm_widgets); i++)
500 snd_soc_dapm_new_control(codec, &wm8753_dapm_widgets[i]);
502 /* add neo1973 specific controls */
503 for (i = 0; i < ARRAY_SIZE(wm8753_neo1973_controls); i++) {
504 err = snd_ctl_add(codec->card,
505 snd_soc_cnew(&wm8753_neo1973_controls[i],
511 /* set up neo1973 specific audio path audio_mapnects */
512 for (i = 0; audio_map[i][0] != NULL; i++) {
513 snd_soc_dapm_connect_input(codec, audio_map[i][0],
514 audio_map[i][1], audio_map[i][2]);
517 snd_soc_dapm_sync_endpoints(codec);
524 static struct snd_soc_cpu_dai bt_dai = {
527 .type = SND_SOC_DAI_PCM,
531 .rates = SNDRV_PCM_RATE_8000,
532 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
536 .rates = SNDRV_PCM_RATE_8000,
537 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
540 static struct snd_soc_dai_link neo1973_dai[] = {
541 { /* Hifi Playback - for similatious use with voice below */
543 .stream_name = "WM8753 HiFi",
544 .cpu_dai = &s3c24xx_i2s_dai,
545 .codec_dai = &wm8753_dai[WM8753_DAI_HIFI],
546 .init = neo1973_wm8753_init,
547 .ops = &neo1973_hifi_ops,
551 .stream_name = "Voice",
553 .codec_dai = &wm8753_dai[WM8753_DAI_VOICE],
554 .ops = &neo1973_voice_ops,
558 static struct snd_soc_machine neo1973 = {
560 .dai_link = neo1973_dai,
561 .num_links = ARRAY_SIZE(neo1973_dai),
564 static struct wm8753_setup_data neo1973_wm8753_setup = {
568 static struct snd_soc_device neo1973_snd_devdata = {
570 .platform = &s3c24xx_soc_platform,
571 .codec_dev = &soc_codec_dev_wm8753,
572 .codec_data = &neo1973_wm8753_setup,
575 static struct i2c_client client_template;
577 static const unsigned short normal_i2c[] = { 0x7C, I2C_CLIENT_END };
579 /* Magic definition of all other variables and things */
582 static int lm4857_amp_probe(struct i2c_adapter *adap, int addr, int kind)
586 client_template.adapter = adap;
587 client_template.addr = addr;
589 i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
593 ret = i2c_attach_client(i2c);
595 printk(KERN_ERR "LM4857 failed to attach at addr %x\n", addr);
607 static int lm4857_i2c_detach(struct i2c_client *client)
609 i2c_detach_client(client);
614 static int lm4857_i2c_attach(struct i2c_adapter *adap)
616 return i2c_probe(adap, &addr_data, lm4857_amp_probe);
619 static u8 lm4857_state;
621 static int lm4857_suspend(struct i2c_client *dev, pm_message_t state)
623 dev_dbg(&dev->dev, "lm4857_suspend\n");
624 lm4857_state = lm4857_regs[LM4857_CTRL] & 0xf;
626 lm4857_regs[LM4857_CTRL] &= 0xf0;
632 static int lm4857_resume(struct i2c_client *dev)
635 lm4857_regs[LM4857_CTRL] |= (lm4857_state & 0x0f);
641 static void lm4857_shutdown(struct i2c_client *dev)
643 dev_dbg(&dev->dev, "lm4857_shutdown\n");
644 lm4857_regs[LM4857_CTRL] &= 0xf0;
648 /* corgi i2c codec control layer */
649 static struct i2c_driver lm4857_i2c_driver = {
651 .name = "LM4857 I2C Amp",
652 .owner = THIS_MODULE,
654 .id = I2C_DRIVERID_LM4857,
655 .suspend = lm4857_suspend,
656 .resume = lm4857_resume,
657 .shutdown = lm4857_shutdown,
658 .attach_adapter = lm4857_i2c_attach,
659 .detach_client = lm4857_i2c_detach,
663 static struct i2c_client client_template = {
665 .driver = &lm4857_i2c_driver,
668 static struct platform_device *neo1973_snd_device;
670 static int __init neo1973_init(void)
674 neo1973_snd_device = platform_device_alloc("soc-audio", -1);
675 if (!neo1973_snd_device)
678 platform_set_drvdata(neo1973_snd_device, &neo1973_snd_devdata);
679 neo1973_snd_devdata.dev = &neo1973_snd_device->dev;
680 ret = platform_device_add(neo1973_snd_device);
683 platform_device_put(neo1973_snd_device);
685 ret = i2c_add_driver(&lm4857_i2c_driver);
687 printk(KERN_ERR "can't add i2c driver");
692 static void __exit neo1973_exit(void)
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");