blob: 65602b935377df161562489f4b93ca4125a8a9cc [file] [log] [blame]
Graeme Gregory74930bb2007-05-14 11:03:52 +02001/*
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +01002 * neo1973_wm8753.c -- SoC audio for Openmoko Neo1973 and Freerunner devices
Graeme Gregory74930bb2007-05-14 11:03:52 +02003 *
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +01004 * Copyright 2007 Openmoko Inc
5 * Author: Graeme Gregory <graeme@openmoko.org>
Graeme Gregory74930bb2007-05-14 11:03:52 +02006 * Copyright 2007 Wolfson Microelectronics PLC.
7 * Author: Graeme Gregory
8 * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +01009 * Copyright 2009 Wolfson Microelectronics
Graeme Gregory74930bb2007-05-14 11:03:52 +020010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
Graeme Gregory74930bb2007-05-14 11:03:52 +020015 */
16
17#include <linux/module.h>
Graeme Gregory74930bb2007-05-14 11:03:52 +020018#include <linux/platform_device.h>
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +010019#include <linux/gpio.h>
20
Graeme Gregory74930bb2007-05-14 11:03:52 +020021#include <sound/soc.h>
Graeme Gregory74930bb2007-05-14 11:03:52 +020022
Sachin Kamatabffae62014-01-22 17:30:38 +053023#include <mach/gpio-samsung.h>
Mark Brownfb2aa072008-10-08 13:02:20 +010024#include <asm/mach-types.h>
Arnd Bergmann5d229ce52013-04-11 19:08:42 +020025#include "regs-iis.h"
Harald Welteaa9673c2007-12-19 15:37:49 +010026
Graeme Gregory74930bb2007-05-14 11:03:52 +020027#include "../codecs/wm8753.h"
Graeme Gregory74930bb2007-05-14 11:03:52 +020028#include "s3c24xx-i2s.h"
29
Graeme Gregory74930bb2007-05-14 11:03:52 +020030static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
31 struct snd_pcm_hw_params *params)
32{
33 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000034 struct snd_soc_dai *codec_dai = rtd->codec_dai;
35 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Graeme Gregory74930bb2007-05-14 11:03:52 +020036 unsigned int pll_out = 0, bclk = 0;
37 int ret = 0;
38 unsigned long iis_clkrate;
39
40 iis_clkrate = s3c24xx_i2s_get_clockrate();
41
42 switch (params_rate(params)) {
43 case 8000:
44 case 16000:
45 pll_out = 12288000;
46 break;
47 case 48000:
48 bclk = WM8753_BCLK_DIV_4;
49 pll_out = 12288000;
50 break;
51 case 96000:
52 bclk = WM8753_BCLK_DIV_2;
53 pll_out = 12288000;
54 break;
55 case 11025:
56 bclk = WM8753_BCLK_DIV_16;
57 pll_out = 11289600;
58 break;
59 case 22050:
60 bclk = WM8753_BCLK_DIV_8;
61 pll_out = 11289600;
62 break;
63 case 44100:
64 bclk = WM8753_BCLK_DIV_4;
65 pll_out = 11289600;
66 break;
67 case 88200:
68 bclk = WM8753_BCLK_DIV_2;
69 pll_out = 11289600;
70 break;
71 }
72
Graeme Gregory74930bb2007-05-14 11:03:52 +020073 /* set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +010074 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
Graeme Gregory74930bb2007-05-14 11:03:52 +020075 SND_SOC_CLOCK_IN);
76 if (ret < 0)
77 return ret;
78
79 /* set MCLK division for sample rate */
Liam Girdwood64105cf2008-07-08 13:19:18 +010080 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
Graeme Gregory8ba02ac2008-04-30 20:24:54 +020081 S3C2410_IISMOD_32FS);
Graeme Gregory74930bb2007-05-14 11:03:52 +020082 if (ret < 0)
83 return ret;
84
85 /* set codec BCLK division for sample rate */
Liam Girdwood64105cf2008-07-08 13:19:18 +010086 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
Graeme Gregory74930bb2007-05-14 11:03:52 +020087 if (ret < 0)
88 return ret;
89
90 /* set prescaler division for sample rate */
Liam Girdwood64105cf2008-07-08 13:19:18 +010091 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
Graeme Gregory8ba02ac2008-04-30 20:24:54 +020092 S3C24XX_PRESCALE(4, 4));
Graeme Gregory74930bb2007-05-14 11:03:52 +020093 if (ret < 0)
94 return ret;
95
96 /* codec PLL input is PCLK/4 */
Mark Brown85488032009-09-05 18:52:16 +010097 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
Graeme Gregory74930bb2007-05-14 11:03:52 +020098 iis_clkrate / 4, pll_out);
99 if (ret < 0)
100 return ret;
101
102 return 0;
103}
104
105static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
106{
107 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000108 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Graeme Gregory74930bb2007-05-14 11:03:52 +0200109
110 /* disable the PLL */
Takashi Iwai140318a2009-10-01 08:40:32 +0200111 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0, 0);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200112}
113
114/*
115 * Neo1973 WM8753 HiFi DAI opserations.
116 */
117static struct snd_soc_ops neo1973_hifi_ops = {
118 .hw_params = neo1973_hifi_hw_params,
119 .hw_free = neo1973_hifi_hw_free,
120};
121
122static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
123 struct snd_pcm_hw_params *params)
124{
125 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000126 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Graeme Gregory74930bb2007-05-14 11:03:52 +0200127 unsigned int pcmdiv = 0;
128 int ret = 0;
129 unsigned long iis_clkrate;
130
131 iis_clkrate = s3c24xx_i2s_get_clockrate();
132
133 if (params_rate(params) != 8000)
134 return -EINVAL;
135 if (params_channels(params) != 1)
136 return -EINVAL;
137
138 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
139
Graeme Gregory74930bb2007-05-14 11:03:52 +0200140 /* set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100141 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200142 SND_SOC_CLOCK_IN);
143 if (ret < 0)
144 return ret;
145
146 /* set codec PCM division for sample rate */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100147 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200148 if (ret < 0)
149 return ret;
150
Andrea Gelminifa2eb002010-10-16 15:19:20 +0200151 /* configure and enable PLL for 12.288MHz output */
Takashi Iwai140318a2009-10-01 08:40:32 +0200152 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200153 iis_clkrate / 4, 12288000);
154 if (ret < 0)
155 return ret;
156
157 return 0;
158}
159
160static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
161{
162 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000163 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Graeme Gregory74930bb2007-05-14 11:03:52 +0200164
165 /* disable the PLL */
Takashi Iwai140318a2009-10-01 08:40:32 +0200166 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0, 0);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200167}
168
169static struct snd_soc_ops neo1973_voice_ops = {
170 .hw_params = neo1973_voice_hw_params,
171 .hw_free = neo1973_voice_hw_free,
172};
173
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100174static int gta02_speaker_enabled;
175
176static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
177 struct snd_ctl_elem_value *ucontrol)
178{
179 gta02_speaker_enabled = ucontrol->value.integer.value[0];
180
Kukjin Kimb2ca7872013-01-02 09:57:59 -0800181 gpio_set_value(S3C2410_GPJ(2), !gta02_speaker_enabled);
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100182
183 return 0;
184}
185
186static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
187 struct snd_ctl_elem_value *ucontrol)
188{
189 ucontrol->value.integer.value[0] = gta02_speaker_enabled;
190 return 0;
191}
192
193static int lm4853_event(struct snd_soc_dapm_widget *w,
194 struct snd_kcontrol *k, int event)
195{
Kukjin Kimb2ca7872013-01-02 09:57:59 -0800196 gpio_set_value(S3C2410_GPJ(1), SND_SOC_DAPM_EVENT_OFF(event));
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100197
198 return 0;
199}
200
Lars-Peter Clausen61e7fe22014-03-01 16:17:02 +0100201static const struct snd_soc_dapm_widget neo1973_wm8753_dapm_widgets[] = {
202 SND_SOC_DAPM_LINE("GSM Line Out", NULL),
203 SND_SOC_DAPM_LINE("GSM Line In", NULL),
204 SND_SOC_DAPM_MIC("Headset Mic", NULL),
205 SND_SOC_DAPM_MIC("Handset Mic", NULL),
206 SND_SOC_DAPM_SPK("Handset Spk", NULL),
207 SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
208};
209
210static const struct snd_soc_dapm_route neo1973_wm8753_routes[] = {
211 /* Connections to the GSM Module */
212 {"GSM Line Out", NULL, "MONO1"},
213 {"GSM Line Out", NULL, "MONO2"},
214 {"RXP", NULL, "GSM Line In"},
215 {"RXN", NULL, "GSM Line In"},
216
217 /* Connections to Headset */
218 {"MIC1", NULL, "Mic Bias"},
219 {"Mic Bias", NULL, "Headset Mic"},
220
221 /* Call Mic */
222 {"MIC2", NULL, "Mic Bias"},
223 {"MIC2N", NULL, "Mic Bias"},
224 {"Mic Bias", NULL, "Handset Mic"},
225
226 /* Connect the ALC pins */
227 {"ACIN", NULL, "ACOP"},
228
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100229 /* Connections to the amp */
230 {"Stereo Out", NULL, "LOUT1"},
231 {"Stereo Out", NULL, "ROUT1"},
232
233 /* Call Speaker */
234 {"Handset Spk", NULL, "LOUT2"},
235 {"Handset Spk", NULL, "ROUT2"},
236};
237
Lars-Peter Clausen61e7fe22014-03-01 16:17:02 +0100238static const struct snd_kcontrol_new neo1973_wm8753_controls[] = {
239 SOC_DAPM_PIN_SWITCH("GSM Line Out"),
240 SOC_DAPM_PIN_SWITCH("GSM Line In"),
241 SOC_DAPM_PIN_SWITCH("Headset Mic"),
242 SOC_DAPM_PIN_SWITCH("Handset Mic"),
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100243 SOC_DAPM_PIN_SWITCH("Handset Spk"),
244 SOC_DAPM_PIN_SWITCH("Stereo Out"),
245
246 SOC_SINGLE_BOOL_EXT("Amp Spk Switch", 0,
247 lm4853_get_spk,
248 lm4853_set_spk),
249};
250
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000251static int neo1973_wm8753_init(struct snd_soc_pcm_runtime *rtd)
Graeme Gregory74930bb2007-05-14 11:03:52 +0200252{
Lars-Peter Clausen4f07c9c2014-03-01 16:17:03 +0100253 struct snd_soc_card *card = rtd->card;
Tim Niemeyer1894c592008-05-05 14:16:12 +0200254
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100255 /* set endpoints to default off mode */
Lars-Peter Clausen4f07c9c2014-03-01 16:17:03 +0100256 snd_soc_dapm_disable_pin(&card->dapm, "GSM Line Out");
257 snd_soc_dapm_disable_pin(&card->dapm, "GSM Line In");
258 snd_soc_dapm_disable_pin(&card->dapm, "Headset Mic");
259 snd_soc_dapm_disable_pin(&card->dapm, "Handset Mic");
260 snd_soc_dapm_disable_pin(&card->dapm, "Stereo Out");
261 snd_soc_dapm_disable_pin(&card->dapm, "Handset Spk");
Jonas Bonne8089942008-10-01 18:17:12 +0100262
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100263 /* allow audio paths from the GSM modem to run during suspend */
Lars-Peter Clausen4f07c9c2014-03-01 16:17:03 +0100264 snd_soc_dapm_ignore_suspend(&card->dapm, "GSM Line Out");
265 snd_soc_dapm_ignore_suspend(&card->dapm, "GSM Line In");
266 snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic");
267 snd_soc_dapm_ignore_suspend(&card->dapm, "Handset Mic");
268 snd_soc_dapm_ignore_suspend(&card->dapm, "Stereo Out");
269 snd_soc_dapm_ignore_suspend(&card->dapm, "Handset Spk");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200270
Graeme Gregory74930bb2007-05-14 11:03:52 +0200271 return 0;
272}
273
Graeme Gregory74930bb2007-05-14 11:03:52 +0200274static struct snd_soc_dai_link neo1973_dai[] = {
275{ /* Hifi Playback - for similatious use with voice below */
276 .name = "WM8753",
277 .stream_name = "WM8753 HiFi",
Padmavathi Vennaa08485d2012-12-07 13:59:21 +0530278 .platform_name = "s3c24xx-iis",
Lars-Peter Clausen518aa592011-01-24 22:12:42 +0100279 .cpu_dai_name = "s3c24xx-iis",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000280 .codec_dai_name = "wm8753-hifi",
Denis 'GNUtoo' Cariklib2ccf062012-02-26 19:21:54 +0100281 .codec_name = "wm8753.0-001a",
Lars-Peter Clausene16514d92015-01-01 17:16:22 +0100282 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
283 SND_SOC_DAIFMT_CBM_CFM,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200284 .init = neo1973_wm8753_init,
285 .ops = &neo1973_hifi_ops,
286},
287{ /* Voice via BT */
288 .name = "Bluetooth",
289 .stream_name = "Voice",
Barry Song200ceb92013-05-18 20:25:00 +0800290 .cpu_dai_name = "bt-sco-pcm",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000291 .codec_dai_name = "wm8753-voice",
Denis 'GNUtoo' Cariklib2ccf062012-02-26 19:21:54 +0100292 .codec_name = "wm8753.0-001a",
Lars-Peter Clausene16514d92015-01-01 17:16:22 +0100293 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF |
294 SND_SOC_DAIFMT_CBS_CFS,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200295 .ops = &neo1973_voice_ops,
296},
297};
298
Lars-Peter Clausen9b0a25f2011-03-07 08:04:55 +0100299static struct snd_soc_aux_dev neo1973_aux_devs[] = {
300 {
Lars-Peter Clausena077ff92011-03-07 08:04:59 +0100301 .name = "dfbmcs320",
302 .codec_name = "dfbmcs320.0",
303 },
Lars-Peter Clausen9b0a25f2011-03-07 08:04:55 +0100304};
305
306static struct snd_soc_codec_conf neo1973_codec_conf[] = {
307 {
308 .dev_name = "lm4857.0-007c",
309 .name_prefix = "Amp",
310 },
311};
312
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100313static const struct gpio neo1973_gta02_gpios[] = {
Kukjin Kimb2ca7872013-01-02 09:57:59 -0800314 { S3C2410_GPJ(2), GPIOF_OUT_INIT_HIGH, "GTA02_HP_IN" },
315 { S3C2410_GPJ(1), GPIOF_OUT_INIT_HIGH, "GTA02_AMP_SHUT" },
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100316};
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100317
Mark Brown87506542008-11-18 20:50:34 +0000318static struct snd_soc_card neo1973 = {
Graeme Gregory74930bb2007-05-14 11:03:52 +0200319 .name = "neo1973",
Axel Lin095d79d2011-12-22 10:53:15 +0800320 .owner = THIS_MODULE,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200321 .dai_link = neo1973_dai,
322 .num_links = ARRAY_SIZE(neo1973_dai),
Lars-Peter Clausen9b0a25f2011-03-07 08:04:55 +0100323 .aux_dev = neo1973_aux_devs,
324 .num_aux_devs = ARRAY_SIZE(neo1973_aux_devs),
325 .codec_conf = neo1973_codec_conf,
326 .num_configs = ARRAY_SIZE(neo1973_codec_conf),
Lars-Peter Clausen4f07c9c2014-03-01 16:17:03 +0100327
328 .controls = neo1973_wm8753_controls,
329 .num_controls = ARRAY_SIZE(neo1973_wm8753_controls),
330 .dapm_widgets = neo1973_wm8753_dapm_widgets,
331 .num_dapm_widgets = ARRAY_SIZE(neo1973_wm8753_dapm_widgets),
332 .dapm_routes = neo1973_wm8753_routes,
333 .num_dapm_routes = ARRAY_SIZE(neo1973_wm8753_routes),
Lars-Peter Clausenfbfad492014-05-20 11:13:28 +0200334 .fully_routed = true,
Graeme Gregory74930bb2007-05-14 11:03:52 +0200335};
336
337static struct platform_device *neo1973_snd_device;
338
339static int __init neo1973_init(void)
340{
341 int ret;
342
Denis 'GNUtoo' Carikli1ae5cbc2012-01-30 00:31:47 +0100343 if (!machine_is_neo1973_gta02())
Mark Brownfb2aa072008-10-08 13:02:20 +0100344 return -ENODEV;
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100345
346 if (machine_is_neo1973_gta02()) {
347 neo1973.name = "neo1973gta02";
Lars-Peter Clausena077ff92011-03-07 08:04:59 +0100348 neo1973.num_aux_devs = 1;
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100349
350 ret = gpio_request_array(neo1973_gta02_gpios,
351 ARRAY_SIZE(neo1973_gta02_gpios));
352 if (ret)
353 return ret;
Mark Brownfb2aa072008-10-08 13:02:20 +0100354 }
355
Graeme Gregory74930bb2007-05-14 11:03:52 +0200356 neo1973_snd_device = platform_device_alloc("soc-audio", -1);
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100357 if (!neo1973_snd_device) {
358 ret = -ENOMEM;
359 goto err_gpio_free;
360 }
361
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000362 platform_set_drvdata(neo1973_snd_device, &neo1973);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200363 ret = platform_device_add(neo1973_snd_device);
364
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100365 if (ret)
Lars-Peter Clausena077ff92011-03-07 08:04:59 +0100366 goto err_put_device;
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100367
368 return 0;
369
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100370err_put_device:
371 platform_device_put(neo1973_snd_device);
372err_gpio_free:
373 if (machine_is_neo1973_gta02()) {
374 gpio_free_array(neo1973_gta02_gpios,
375 ARRAY_SIZE(neo1973_gta02_gpios));
Jean Delvared2802892008-09-01 17:44:05 +0200376 }
Graeme Gregory74930bb2007-05-14 11:03:52 +0200377 return ret;
378}
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100379module_init(neo1973_init);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200380
381static void __exit neo1973_exit(void)
382{
Graeme Gregory74930bb2007-05-14 11:03:52 +0200383 platform_device_unregister(neo1973_snd_device);
Graeme Gregory74930bb2007-05-14 11:03:52 +0200384
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100385 if (machine_is_neo1973_gta02()) {
386 gpio_free_array(neo1973_gta02_gpios,
387 ARRAY_SIZE(neo1973_gta02_gpios));
388 }
389}
Graeme Gregory74930bb2007-05-14 11:03:52 +0200390module_exit(neo1973_exit);
391
392/* Module information */
Graeme Gregory443590e2008-04-30 20:25:23 +0200393MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
Lars-Peter Clausenf5c4ffb2011-03-07 08:04:58 +0100394MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 and Frerunner");
Graeme Gregory74930bb2007-05-14 11:03:52 +0200395MODULE_LICENSE("GPL");