blob: 8089f8ee05c0413f2bf4ff7f9b365a26228a8895 [file] [log] [blame]
Graeme Gregory74930bb2007-05-14 11:03:52 +02001/*
2 * neo1973_wm8753.c -- SoC audio for Neo1973
3 *
4 * Copyright 2007 Wolfson Microelectronics PLC.
5 * Author: Graeme Gregory
6 * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
7 *
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.
12 *
Graeme Gregory74930bb2007-05-14 11:03:52 +020013 */
14
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>
Graeme Gregory74930bb2007-05-14 11:03:52 +020021#include <sound/core.h>
22#include <sound/pcm.h>
23#include <sound/soc.h>
24#include <sound/soc-dapm.h>
Mike Montoura2e31a52008-06-11 13:47:14 +010025#include <sound/tlv.h>
Graeme Gregory74930bb2007-05-14 11:03:52 +020026
Graeme Gregory74930bb2007-05-14 11:03:52 +020027#include <asm/hardware/scoop.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/regs-clock.h>
29#include <mach/regs-gpio.h>
30#include <mach/hardware.h>
31#include <mach/audio.h>
Graeme Gregory8ba02ac2008-04-30 20:24:54 +020032#include <linux/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/spi-gpio.h>
Harald Welteaa9673c2007-12-19 15:37:49 +010034
35#include <asm/plat-s3c24xx/regs-iis.h>
36
Graeme Gregory74930bb2007-05-14 11:03:52 +020037#include "../codecs/wm8753.h"
38#include "lm4857.h"
39#include "s3c24xx-pcm.h"
40#include "s3c24xx-i2s.h"
41
Tim Niemeyer1894c592008-05-05 14:16:12 +020042/* Debugging stuff */
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)
46#else
47#define DBG(x...)
48#endif
49
Graeme Gregory74930bb2007-05-14 11:03:52 +020050/* 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
60
61static struct snd_soc_machine neo1973;
62static struct i2c_client *i2c;
63
64static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params *params)
66{
67 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood1992a6f2008-07-07 16:08:24 +010068 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
69 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Graeme Gregory74930bb2007-05-14 11:03:52 +020070 unsigned int pll_out = 0, bclk = 0;
71 int ret = 0;
72 unsigned long iis_clkrate;
73
Tim Niemeyer1894c592008-05-05 14:16:12 +020074 DBG("Entered %s\n", __func__);
75
Graeme Gregory74930bb2007-05-14 11:03:52 +020076 iis_clkrate = s3c24xx_i2s_get_clockrate();
77
78 switch (params_rate(params)) {
79 case 8000:
80 case 16000:
81 pll_out = 12288000;
82 break;
83 case 48000:
84 bclk = WM8753_BCLK_DIV_4;
85 pll_out = 12288000;
86 break;
87 case 96000:
88 bclk = WM8753_BCLK_DIV_2;
89 pll_out = 12288000;
90 break;
91 case 11025:
92 bclk = WM8753_BCLK_DIV_16;
93 pll_out = 11289600;
94 break;
95 case 22050:
96 bclk = WM8753_BCLK_DIV_8;
97 pll_out = 11289600;
98 break;
99 case 44100:
100 bclk = WM8753_BCLK_DIV_4;
101 pll_out = 11289600;
102 break;
103 case 88200:
104 bclk = WM8753_BCLK_DIV_2;
105 pll_out = 11289600;
106 break;
107 }
108
109 /* set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100110 ret = snd_soc_dai_set_fmt(codec_dai,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200111 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
112 SND_SOC_DAIFMT_CBM_CFM);
113 if (ret < 0)
114 return ret;
115
116 /* set cpu DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100117 ret = snd_soc_dai_set_fmt(cpu_dai,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200118 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
119 SND_SOC_DAIFMT_CBM_CFM);
120 if (ret < 0)
121 return ret;
122
123 /* set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100124 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200125 SND_SOC_CLOCK_IN);
126 if (ret < 0)
127 return ret;
128
129 /* set MCLK division for sample rate */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100130 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
Graeme Gregory8ba02ac2008-04-30 20:24:54 +0200131 S3C2410_IISMOD_32FS);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200132 if (ret < 0)
133 return ret;
134
135 /* set codec BCLK division for sample rate */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100136 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200137 if (ret < 0)
138 return ret;
139
140 /* set prescaler division for sample rate */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100141 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
Graeme Gregory8ba02ac2008-04-30 20:24:54 +0200142 S3C24XX_PRESCALE(4, 4));
Graeme Gregory74930bb2007-05-14 11:03:52 +0200143 if (ret < 0)
144 return ret;
145
146 /* codec PLL input is PCLK/4 */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100147 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200148 iis_clkrate / 4, pll_out);
149 if (ret < 0)
150 return ret;
151
152 return 0;
153}
154
155static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
156{
157 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood1992a6f2008-07-07 16:08:24 +0100158 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
Graeme Gregory74930bb2007-05-14 11:03:52 +0200159
Tim Niemeyer1894c592008-05-05 14:16:12 +0200160 DBG("Entered %s\n", __func__);
161
Graeme Gregory74930bb2007-05-14 11:03:52 +0200162 /* disable the PLL */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100163 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200164}
165
166/*
167 * Neo1973 WM8753 HiFi DAI opserations.
168 */
169static struct snd_soc_ops neo1973_hifi_ops = {
170 .hw_params = neo1973_hifi_hw_params,
171 .hw_free = neo1973_hifi_hw_free,
172};
173
174static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
175 struct snd_pcm_hw_params *params)
176{
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood1992a6f2008-07-07 16:08:24 +0100178 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
Graeme Gregory74930bb2007-05-14 11:03:52 +0200179 unsigned int pcmdiv = 0;
180 int ret = 0;
181 unsigned long iis_clkrate;
182
Tim Niemeyer1894c592008-05-05 14:16:12 +0200183 DBG("Entered %s\n", __func__);
184
Graeme Gregory74930bb2007-05-14 11:03:52 +0200185 iis_clkrate = s3c24xx_i2s_get_clockrate();
186
187 if (params_rate(params) != 8000)
188 return -EINVAL;
189 if (params_channels(params) != 1)
190 return -EINVAL;
191
192 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
193
194 /* todo: gg check mode (DSP_B) against CSR datasheet */
195 /* set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100196 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
Graeme Gregory74930bb2007-05-14 11:03:52 +0200197 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
198 if (ret < 0)
199 return ret;
200
201 /* set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100202 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200203 SND_SOC_CLOCK_IN);
204 if (ret < 0)
205 return ret;
206
207 /* set codec PCM division for sample rate */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100208 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200209 if (ret < 0)
210 return ret;
211
212 /* configue and enable PLL for 12.288MHz output */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100213 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200214 iis_clkrate / 4, 12288000);
215 if (ret < 0)
216 return ret;
217
218 return 0;
219}
220
221static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
222{
223 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood1992a6f2008-07-07 16:08:24 +0100224 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
Graeme Gregory74930bb2007-05-14 11:03:52 +0200225
Tim Niemeyer1894c592008-05-05 14:16:12 +0200226 DBG("Entered %s\n", __func__);
227
Graeme Gregory74930bb2007-05-14 11:03:52 +0200228 /* disable the PLL */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100229 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200230}
231
232static struct snd_soc_ops neo1973_voice_ops = {
233 .hw_params = neo1973_voice_hw_params,
234 .hw_free = neo1973_voice_hw_free,
235};
236
Graeme Gregory8ba02ac2008-04-30 20:24:54 +0200237static int neo1973_scenario;
Graeme Gregory74930bb2007-05-14 11:03:52 +0200238
239static int neo1973_get_scenario(struct snd_kcontrol *kcontrol,
240 struct snd_ctl_elem_value *ucontrol)
241{
242 ucontrol->value.integer.value[0] = neo1973_scenario;
243 return 0;
244}
245
246static int set_scenario_endpoints(struct snd_soc_codec *codec, int scenario)
247{
Tim Niemeyer1894c592008-05-05 14:16:12 +0200248 DBG("Entered %s\n", __func__);
249
Graeme Gregory8ba02ac2008-04-30 20:24:54 +0200250 switch (neo1973_scenario) {
Graeme Gregory74930bb2007-05-14 11:03:52 +0200251 case NEO_AUDIO_OFF:
Liam Girdwooda5302182008-07-07 13:35:17 +0100252 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");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200257 break;
258 case NEO_GSM_CALL_AUDIO_HANDSET:
Liam Girdwooda5302182008-07-07 13:35:17 +0100259 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");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200264 break;
265 case NEO_GSM_CALL_AUDIO_HEADSET:
Liam Girdwooda5302182008-07-07 13:35:17 +0100266 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");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200271 break;
272 case NEO_GSM_CALL_AUDIO_BLUETOOTH:
Liam Girdwooda5302182008-07-07 13:35:17 +0100273 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");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200278 break;
279 case NEO_STEREO_TO_SPEAKERS:
Liam Girdwooda5302182008-07-07 13:35:17 +0100280 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");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200285 break;
286 case NEO_STEREO_TO_HEADPHONES:
Liam Girdwooda5302182008-07-07 13:35:17 +0100287 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");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200292 break;
293 case NEO_CAPTURE_HANDSET:
Liam Girdwooda5302182008-07-07 13:35:17 +0100294 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");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200299 break;
300 case NEO_CAPTURE_HEADSET:
Liam Girdwooda5302182008-07-07 13:35:17 +0100301 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");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200306 break;
307 case NEO_CAPTURE_BLUETOOTH:
Liam Girdwooda5302182008-07-07 13:35:17 +0100308 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");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200313 break;
314 default:
Liam Girdwooda5302182008-07-07 13:35:17 +0100315 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");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200320 }
321
Liam Girdwooda5302182008-07-07 13:35:17 +0100322 snd_soc_dapm_sync(codec);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200323
324 return 0;
325}
326
327static int neo1973_set_scenario(struct snd_kcontrol *kcontrol,
328 struct snd_ctl_elem_value *ucontrol)
329{
330 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
331
Tim Niemeyer1894c592008-05-05 14:16:12 +0200332 DBG("Entered %s\n", __func__);
333
Graeme Gregory74930bb2007-05-14 11:03:52 +0200334 if (neo1973_scenario == ucontrol->value.integer.value[0])
335 return 0;
336
337 neo1973_scenario = ucontrol->value.integer.value[0];
338 set_scenario_endpoints(codec, neo1973_scenario);
339 return 1;
340}
341
342static u8 lm4857_regs[4] = {0x00, 0x40, 0x80, 0xC0};
343
344static void lm4857_write_regs(void)
345{
Tim Niemeyer1894c592008-05-05 14:16:12 +0200346 DBG("Entered %s\n", __func__);
347
Graeme Gregory74930bb2007-05-14 11:03:52 +0200348 if (i2c_master_send(i2c, lm4857_regs, 4) != 4)
349 printk(KERN_ERR "lm4857: i2c write failed\n");
350}
351
352static int lm4857_get_reg(struct snd_kcontrol *kcontrol,
353 struct snd_ctl_elem_value *ucontrol)
354{
Graeme Gregory8ba02ac2008-04-30 20:24:54 +0200355 int reg = kcontrol->private_value & 0xFF;
Graeme Gregory74930bb2007-05-14 11:03:52 +0200356 int shift = (kcontrol->private_value >> 8) & 0x0F;
357 int mask = (kcontrol->private_value >> 16) & 0xFF;
358
Tim Niemeyer1894c592008-05-05 14:16:12 +0200359 DBG("Entered %s\n", __func__);
360
Graeme Gregory74930bb2007-05-14 11:03:52 +0200361 ucontrol->value.integer.value[0] = (lm4857_regs[reg] >> shift) & mask;
362 return 0;
363}
364
365static int lm4857_set_reg(struct snd_kcontrol *kcontrol,
366 struct snd_ctl_elem_value *ucontrol)
367{
368 int reg = kcontrol->private_value & 0xFF;
369 int shift = (kcontrol->private_value >> 8) & 0x0F;
370 int mask = (kcontrol->private_value >> 16) & 0xFF;
371
Graeme Gregory8ba02ac2008-04-30 20:24:54 +0200372 if (((lm4857_regs[reg] >> shift) & mask) ==
Graeme Gregory74930bb2007-05-14 11:03:52 +0200373 ucontrol->value.integer.value[0])
374 return 0;
375
Graeme Gregory8ba02ac2008-04-30 20:24:54 +0200376 lm4857_regs[reg] &= ~(mask << shift);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200377 lm4857_regs[reg] |= ucontrol->value.integer.value[0] << shift;
378 lm4857_write_regs();
379 return 1;
380}
381
382static int lm4857_get_mode(struct snd_kcontrol *kcontrol,
383 struct snd_ctl_elem_value *ucontrol)
384{
385 u8 value = lm4857_regs[LM4857_CTRL] & 0x0F;
386
Tim Niemeyer1894c592008-05-05 14:16:12 +0200387 DBG("Entered %s\n", __func__);
388
Graeme Gregory74930bb2007-05-14 11:03:52 +0200389 if (value)
390 value -= 5;
391
392 ucontrol->value.integer.value[0] = value;
393 return 0;
394}
395
396static int lm4857_set_mode(struct snd_kcontrol *kcontrol,
397 struct snd_ctl_elem_value *ucontrol)
398{
399 u8 value = ucontrol->value.integer.value[0];
400
Tim Niemeyer1894c592008-05-05 14:16:12 +0200401 DBG("Entered %s\n", __func__);
402
Graeme Gregory74930bb2007-05-14 11:03:52 +0200403 if (value)
404 value += 5;
405
406 if ((lm4857_regs[LM4857_CTRL] & 0x0F) == value)
407 return 0;
408
409 lm4857_regs[LM4857_CTRL] &= 0xF0;
410 lm4857_regs[LM4857_CTRL] |= value;
411 lm4857_write_regs();
412 return 1;
413}
414
415static 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),
421};
422
423
Mark Brown8f3112d2008-05-13 14:58:03 +0200424static const struct snd_soc_dapm_route dapm_routes[] = {
Graeme Gregory74930bb2007-05-14 11:03:52 +0200425
426 /* Connections to the lm4857 amp */
427 {"Audio Out", NULL, "LOUT1"},
428 {"Audio Out", NULL, "ROUT1"},
429
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"},
435
436 /* Connections to Headset */
437 {"MIC1", NULL, "Mic Bias"},
438 {"Mic Bias", NULL, "Headset Mic"},
439
440 /* Call Mic */
441 {"MIC2", NULL, "Mic Bias"},
442 {"MIC2N", NULL, "Mic Bias"},
443 {"Mic Bias", NULL, "Call Mic"},
444
445 /* Connect the ALC pins */
446 {"ACIN", NULL, "ACOP"},
Graeme Gregory74930bb2007-05-14 11:03:52 +0200447};
448
449static const char *lm4857_mode[] = {
450 "Off",
451 "Call Speaker",
452 "Stereo Speakers",
453 "Stereo Speakers + Headphones",
454 "Headphones"
455};
456
457static const struct soc_enum lm4857_mode_enum[] = {
458 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lm4857_mode), lm4857_mode),
459};
460
461static const char *neo_scenarios[] = {
462 "Off",
463 "GSM Handset",
464 "GSM Headset",
465 "GSM Bluetooth",
466 "Speakers",
467 "Headphones",
468 "Capture Handset",
469 "Capture Headset",
470 "Capture Bluetooth"
471};
472
473static const struct soc_enum neo_scenario_enum[] = {
Graeme Gregory8ba02ac2008-04-30 20:24:54 +0200474 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(neo_scenarios), neo_scenarios),
Graeme Gregory74930bb2007-05-14 11:03:52 +0200475};
476
Mike Montoura2e31a52008-06-11 13:47:14 +0100477static const DECLARE_TLV_DB_SCALE(stereo_tlv, -4050, 150, 0);
478static const DECLARE_TLV_DB_SCALE(mono_tlv, -3450, 150, 0);
479
Graeme Gregory74930bb2007-05-14 11:03:52 +0200480static const struct snd_kcontrol_new wm8753_neo1973_controls[] = {
Mike Montoura2e31a52008-06-11 13:47:14 +0100481 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),
Graeme Gregory74930bb2007-05-14 11:03:52 +0200487 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),
499};
500
501/*
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.
505 */
506static int neo1973_wm8753_init(struct snd_soc_codec *codec)
507{
508 int i, err;
509
Tim Niemeyer1894c592008-05-05 14:16:12 +0200510 DBG("Entered %s\n", __func__);
511
Graeme Gregory74930bb2007-05-14 11:03:52 +0200512 /* set up NC codec pins */
Liam Girdwooda5302182008-07-07 13:35:17 +0100513 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");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200519
520
521 /* set endpoints to default mode */
522 set_scenario_endpoints(codec, NEO_AUDIO_OFF);
523
524 /* Add neo1973 specific widgets */
Mark Brown8f3112d2008-05-13 14:58:03 +0200525 snd_soc_dapm_new_controls(codec, wm8753_dapm_widgets,
526 ARRAY_SIZE(wm8753_dapm_widgets));
Graeme Gregory74930bb2007-05-14 11:03:52 +0200527
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],
532 codec, NULL));
533 if (err < 0)
534 return err;
535 }
536
Mark Brown8f3112d2008-05-13 14:58:03 +0200537 /* set up neo1973 specific audio routes */
538 err = snd_soc_dapm_add_routes(codec, dapm_routes,
539 ARRAY_SIZE(dapm_routes));
Graeme Gregory74930bb2007-05-14 11:03:52 +0200540
Liam Girdwooda5302182008-07-07 13:35:17 +0100541 snd_soc_dapm_sync(codec);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200542 return 0;
543}
544
545/*
546 * BT Codec DAI
547 */
Liam Girdwood1992a6f2008-07-07 16:08:24 +0100548static struct snd_soc_dai bt_dai = {
Graeme Gregory8ba02ac2008-04-30 20:24:54 +0200549 .name = "Bluetooth",
Graeme Gregory74930bb2007-05-14 11:03:52 +0200550 .id = 0,
551 .type = SND_SOC_DAI_PCM,
552 .playback = {
553 .channels_min = 1,
554 .channels_max = 1,
555 .rates = SNDRV_PCM_RATE_8000,
556 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
557 .capture = {
558 .channels_min = 1,
559 .channels_max = 1,
560 .rates = SNDRV_PCM_RATE_8000,
561 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
562};
563
564static struct snd_soc_dai_link neo1973_dai[] = {
565{ /* Hifi Playback - for similatious use with voice below */
566 .name = "WM8753",
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,
572},
573{ /* Voice via BT */
574 .name = "Bluetooth",
575 .stream_name = "Voice",
576 .cpu_dai = &bt_dai,
577 .codec_dai = &wm8753_dai[WM8753_DAI_VOICE],
578 .ops = &neo1973_voice_ops,
579},
580};
581
582static struct snd_soc_machine neo1973 = {
583 .name = "neo1973",
584 .dai_link = neo1973_dai,
585 .num_links = ARRAY_SIZE(neo1973_dai),
586};
587
588static struct wm8753_setup_data neo1973_wm8753_setup = {
589 .i2c_address = 0x1a,
590};
591
592static struct snd_soc_device neo1973_snd_devdata = {
593 .machine = &neo1973,
594 .platform = &s3c24xx_soc_platform,
595 .codec_dev = &soc_codec_dev_wm8753,
596 .codec_data = &neo1973_wm8753_setup,
597};
598
599static struct i2c_client client_template;
600
Jean Delvare2cdddeb2008-01-27 18:14:47 +0100601static const unsigned short normal_i2c[] = { 0x7C, I2C_CLIENT_END };
Graeme Gregory74930bb2007-05-14 11:03:52 +0200602
603/* Magic definition of all other variables and things */
604I2C_CLIENT_INSMOD;
605
606static int lm4857_amp_probe(struct i2c_adapter *adap, int addr, int kind)
607{
608 int ret;
609
Tim Niemeyer1894c592008-05-05 14:16:12 +0200610 DBG("Entered %s\n", __func__);
611
Graeme Gregory74930bb2007-05-14 11:03:52 +0200612 client_template.adapter = adap;
613 client_template.addr = addr;
614
615 i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
616 if (i2c == NULL)
617 return -ENOMEM;
618
619 ret = i2c_attach_client(i2c);
620 if (ret < 0) {
621 printk(KERN_ERR "LM4857 failed to attach at addr %x\n", addr);
622 goto exit_err;
623 }
624
625 lm4857_write_regs();
626 return ret;
627
628exit_err:
629 kfree(i2c);
630 return ret;
631}
632
633static int lm4857_i2c_detach(struct i2c_client *client)
634{
Tim Niemeyer1894c592008-05-05 14:16:12 +0200635 DBG("Entered %s\n", __func__);
636
Graeme Gregory74930bb2007-05-14 11:03:52 +0200637 i2c_detach_client(client);
638 kfree(client);
639 return 0;
640}
641
642static int lm4857_i2c_attach(struct i2c_adapter *adap)
643{
Tim Niemeyer1894c592008-05-05 14:16:12 +0200644 DBG("Entered %s\n", __func__);
645
Graeme Gregory74930bb2007-05-14 11:03:52 +0200646 return i2c_probe(adap, &addr_data, lm4857_amp_probe);
647}
648
Graeme Gregoryfd403dc2008-04-30 20:26:45 +0200649static u8 lm4857_state;
650
651static int lm4857_suspend(struct i2c_client *dev, pm_message_t state)
652{
Tim Niemeyer1894c592008-05-05 14:16:12 +0200653 DBG("Entered %s\n", __func__);
654
Graeme Gregoryfd403dc2008-04-30 20:26:45 +0200655 dev_dbg(&dev->dev, "lm4857_suspend\n");
656 lm4857_state = lm4857_regs[LM4857_CTRL] & 0xf;
657 if (lm4857_state) {
658 lm4857_regs[LM4857_CTRL] &= 0xf0;
659 lm4857_write_regs();
660 }
661 return 0;
662}
663
664static int lm4857_resume(struct i2c_client *dev)
665{
Tim Niemeyer1894c592008-05-05 14:16:12 +0200666 DBG("Entered %s\n", __func__);
667
Graeme Gregoryfd403dc2008-04-30 20:26:45 +0200668 if (lm4857_state) {
669 lm4857_regs[LM4857_CTRL] |= (lm4857_state & 0x0f);
670 lm4857_write_regs();
671 }
672 return 0;
673}
674
675static void lm4857_shutdown(struct i2c_client *dev)
676{
Tim Niemeyer1894c592008-05-05 14:16:12 +0200677 DBG("Entered %s\n", __func__);
678
Graeme Gregoryfd403dc2008-04-30 20:26:45 +0200679 dev_dbg(&dev->dev, "lm4857_shutdown\n");
680 lm4857_regs[LM4857_CTRL] &= 0xf0;
681 lm4857_write_regs();
682}
683
Graeme Gregory74930bb2007-05-14 11:03:52 +0200684/* corgi i2c codec control layer */
685static struct i2c_driver lm4857_i2c_driver = {
686 .driver = {
687 .name = "LM4857 I2C Amp",
688 .owner = THIS_MODULE,
689 },
690 .id = I2C_DRIVERID_LM4857,
Graeme Gregoryfd403dc2008-04-30 20:26:45 +0200691 .suspend = lm4857_suspend,
692 .resume = lm4857_resume,
693 .shutdown = lm4857_shutdown,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200694 .attach_adapter = lm4857_i2c_attach,
695 .detach_client = lm4857_i2c_detach,
696 .command = NULL,
697};
698
699static struct i2c_client client_template = {
700 .name = "LM4857",
701 .driver = &lm4857_i2c_driver,
702};
703
704static struct platform_device *neo1973_snd_device;
705
706static int __init neo1973_init(void)
707{
708 int ret;
709
Tim Niemeyer1894c592008-05-05 14:16:12 +0200710 DBG("Entered %s\n", __func__);
711
Graeme Gregory74930bb2007-05-14 11:03:52 +0200712 neo1973_snd_device = platform_device_alloc("soc-audio", -1);
713 if (!neo1973_snd_device)
714 return -ENOMEM;
715
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);
719
720 if (ret)
721 platform_device_put(neo1973_snd_device);
722
723 ret = i2c_add_driver(&lm4857_i2c_driver);
724 if (ret != 0)
725 printk(KERN_ERR "can't add i2c driver");
726
727 return ret;
728}
729
730static void __exit neo1973_exit(void)
731{
Tim Niemeyer1894c592008-05-05 14:16:12 +0200732 DBG("Entered %s\n", __func__);
733
Tim Niemeyer6b9a9b32008-04-22 17:10:23 +0200734 i2c_del_driver(&lm4857_i2c_driver);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200735 platform_device_unregister(neo1973_snd_device);
736}
737
738module_init(neo1973_init);
739module_exit(neo1973_exit);
740
741/* Module information */
Graeme Gregory443590e2008-04-30 20:25:23 +0200742MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200743MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973");
744MODULE_LICENSE("GPL");