1 // SPDX-License-Identifier: GPL-2.0+
3 // neo1973_wm8753.c - SoC audio for Openmoko Neo1973 and Freerunner devices
5 // Copyright 2007 Openmoko Inc
6 // Author: Graeme Gregory <graeme@openmoko.org>
7 // Copyright 2007 Wolfson Microelectronics PLC.
8 // Author: Graeme Gregory
9 // graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
10 // Copyright 2009 Wolfson Microelectronics
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/gpio/consumer.h>
16 #include <sound/soc.h>
19 #include "../codecs/wm8753.h"
20 #include "s3c24xx-i2s.h"
22 static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
23 struct snd_pcm_hw_params *params)
25 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
26 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
27 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
28 unsigned int pll_out = 0, bclk = 0;
30 unsigned long iis_clkrate;
32 iis_clkrate = s3c24xx_i2s_get_clockrate();
34 switch (params_rate(params)) {
40 bclk = WM8753_BCLK_DIV_4;
44 bclk = WM8753_BCLK_DIV_2;
48 bclk = WM8753_BCLK_DIV_16;
52 bclk = WM8753_BCLK_DIV_8;
56 bclk = WM8753_BCLK_DIV_4;
60 bclk = WM8753_BCLK_DIV_2;
65 /* set the codec system clock for DAC and ADC */
66 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
71 /* set MCLK division for sample rate */
72 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
77 /* set codec BCLK division for sample rate */
78 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
82 /* set prescaler division for sample rate */
83 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
84 S3C24XX_PRESCALE(4, 4));
88 /* codec PLL input is PCLK/4 */
89 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
90 iis_clkrate / 4, pll_out);
97 static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
99 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
100 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
102 /* disable the PLL */
103 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0, 0);
107 * Neo1973 WM8753 HiFi DAI opserations.
109 static struct snd_soc_ops neo1973_hifi_ops = {
110 .hw_params = neo1973_hifi_hw_params,
111 .hw_free = neo1973_hifi_hw_free,
114 static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
115 struct snd_pcm_hw_params *params)
117 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
118 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
119 unsigned int pcmdiv = 0;
121 unsigned long iis_clkrate;
123 iis_clkrate = s3c24xx_i2s_get_clockrate();
125 if (params_rate(params) != 8000)
127 if (params_channels(params) != 1)
130 pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
132 /* set the codec system clock for DAC and ADC */
133 ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
138 /* set codec PCM division for sample rate */
139 ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
143 /* configure and enable PLL for 12.288MHz output */
144 ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
145 iis_clkrate / 4, 12288000);
152 static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
154 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
155 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
157 /* disable the PLL */
158 return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0, 0);
161 static struct snd_soc_ops neo1973_voice_ops = {
162 .hw_params = neo1973_voice_hw_params,
163 .hw_free = neo1973_voice_hw_free,
166 static struct gpio_desc *gpiod_hp_in, *gpiod_amp_shut;
167 static int gta02_speaker_enabled;
169 static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
170 struct snd_ctl_elem_value *ucontrol)
172 gta02_speaker_enabled = ucontrol->value.integer.value[0];
174 gpiod_set_value(gpiod_hp_in, !gta02_speaker_enabled);
179 static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
180 struct snd_ctl_elem_value *ucontrol)
182 ucontrol->value.integer.value[0] = gta02_speaker_enabled;
186 static int lm4853_event(struct snd_soc_dapm_widget *w,
187 struct snd_kcontrol *k, int event)
189 gpiod_set_value(gpiod_amp_shut, SND_SOC_DAPM_EVENT_OFF(event));
194 static const struct snd_soc_dapm_widget neo1973_wm8753_dapm_widgets[] = {
195 SND_SOC_DAPM_LINE("GSM Line Out", NULL),
196 SND_SOC_DAPM_LINE("GSM Line In", NULL),
197 SND_SOC_DAPM_MIC("Headset Mic", NULL),
198 SND_SOC_DAPM_MIC("Handset Mic", NULL),
199 SND_SOC_DAPM_SPK("Handset Spk", NULL),
200 SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
203 static const struct snd_soc_dapm_route neo1973_wm8753_routes[] = {
204 /* Connections to the GSM Module */
205 {"GSM Line Out", NULL, "MONO1"},
206 {"GSM Line Out", NULL, "MONO2"},
207 {"RXP", NULL, "GSM Line In"},
208 {"RXN", NULL, "GSM Line In"},
210 /* Connections to Headset */
211 {"MIC1", NULL, "Mic Bias"},
212 {"Mic Bias", NULL, "Headset Mic"},
215 {"MIC2", NULL, "Mic Bias"},
216 {"MIC2N", NULL, "Mic Bias"},
217 {"Mic Bias", NULL, "Handset Mic"},
219 /* Connect the ALC pins */
220 {"ACIN", NULL, "ACOP"},
222 /* Connections to the amp */
223 {"Stereo Out", NULL, "LOUT1"},
224 {"Stereo Out", NULL, "ROUT1"},
227 {"Handset Spk", NULL, "LOUT2"},
228 {"Handset Spk", NULL, "ROUT2"},
231 static const struct snd_kcontrol_new neo1973_wm8753_controls[] = {
232 SOC_DAPM_PIN_SWITCH("GSM Line Out"),
233 SOC_DAPM_PIN_SWITCH("GSM Line In"),
234 SOC_DAPM_PIN_SWITCH("Headset Mic"),
235 SOC_DAPM_PIN_SWITCH("Handset Mic"),
236 SOC_DAPM_PIN_SWITCH("Handset Spk"),
237 SOC_DAPM_PIN_SWITCH("Stereo Out"),
239 SOC_SINGLE_BOOL_EXT("Amp Spk Switch", 0,
244 static int neo1973_wm8753_init(struct snd_soc_pcm_runtime *rtd)
246 struct snd_soc_card *card = rtd->card;
248 /* set endpoints to default off mode */
249 snd_soc_dapm_disable_pin(&card->dapm, "GSM Line Out");
250 snd_soc_dapm_disable_pin(&card->dapm, "GSM Line In");
251 snd_soc_dapm_disable_pin(&card->dapm, "Headset Mic");
252 snd_soc_dapm_disable_pin(&card->dapm, "Handset Mic");
253 snd_soc_dapm_disable_pin(&card->dapm, "Stereo Out");
254 snd_soc_dapm_disable_pin(&card->dapm, "Handset Spk");
256 /* allow audio paths from the GSM modem to run during suspend */
257 snd_soc_dapm_ignore_suspend(&card->dapm, "GSM Line Out");
258 snd_soc_dapm_ignore_suspend(&card->dapm, "GSM Line In");
259 snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic");
260 snd_soc_dapm_ignore_suspend(&card->dapm, "Handset Mic");
261 snd_soc_dapm_ignore_suspend(&card->dapm, "Stereo Out");
262 snd_soc_dapm_ignore_suspend(&card->dapm, "Handset Spk");
267 SND_SOC_DAILINK_DEFS(wm8753,
268 DAILINK_COMP_ARRAY(COMP_CPU("s3c24xx-iis")),
269 DAILINK_COMP_ARRAY(COMP_CODEC("wm8753.0-001a", "wm8753-hifi")),
270 DAILINK_COMP_ARRAY(COMP_PLATFORM("s3c24xx-iis")));
272 SND_SOC_DAILINK_DEFS(bluetooth,
273 DAILINK_COMP_ARRAY(COMP_CPU("bt-sco-pcm")),
274 DAILINK_COMP_ARRAY(COMP_CODEC("wm8753.0-001a", "wm8753-voice")));
276 static struct snd_soc_dai_link neo1973_dai[] = {
277 { /* Hifi Playback - for similatious use with voice below */
279 .stream_name = "WM8753 HiFi",
280 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
281 SND_SOC_DAIFMT_CBM_CFM,
282 .init = neo1973_wm8753_init,
283 .ops = &neo1973_hifi_ops,
284 SND_SOC_DAILINK_REG(wm8753),
288 .stream_name = "Voice",
289 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF |
290 SND_SOC_DAIFMT_CBS_CFS,
291 .ops = &neo1973_voice_ops,
292 SND_SOC_DAILINK_REG(bluetooth),
296 static struct snd_soc_aux_dev neo1973_aux_devs[] = {
298 .dlc = COMP_AUX("dfbmcs320.0"),
302 static struct snd_soc_codec_conf neo1973_codec_conf[] = {
304 .dlc = COMP_CODEC_CONF("lm4857.0-007c"),
305 .name_prefix = "Amp",
309 static struct snd_soc_card neo1973 = {
310 .name = "neo1973gta02",
311 .owner = THIS_MODULE,
312 .dai_link = neo1973_dai,
313 .num_links = ARRAY_SIZE(neo1973_dai),
314 .aux_dev = neo1973_aux_devs,
315 .num_aux_devs = ARRAY_SIZE(neo1973_aux_devs),
316 .codec_conf = neo1973_codec_conf,
317 .num_configs = ARRAY_SIZE(neo1973_codec_conf),
319 .controls = neo1973_wm8753_controls,
320 .num_controls = ARRAY_SIZE(neo1973_wm8753_controls),
321 .dapm_widgets = neo1973_wm8753_dapm_widgets,
322 .num_dapm_widgets = ARRAY_SIZE(neo1973_wm8753_dapm_widgets),
323 .dapm_routes = neo1973_wm8753_routes,
324 .num_dapm_routes = ARRAY_SIZE(neo1973_wm8753_routes),
325 .fully_routed = true,
328 static int neo1973_probe(struct platform_device *pdev)
330 struct device *dev = &pdev->dev;
332 gpiod_hp_in = devm_gpiod_get(dev, "hp", GPIOD_OUT_HIGH);
333 if (IS_ERR(gpiod_hp_in)) {
334 dev_err(dev, "missing gpio %s\n", "hp");
335 return PTR_ERR(gpiod_hp_in);
337 gpiod_amp_shut = devm_gpiod_get(dev, "amp-shut", GPIOD_OUT_HIGH);
338 if (IS_ERR(gpiod_amp_shut)) {
339 dev_err(dev, "missing gpio %s\n", "amp-shut");
340 return PTR_ERR(gpiod_amp_shut);
344 return devm_snd_soc_register_card(dev, &neo1973);
347 struct platform_driver neo1973_audio = {
349 .name = "neo1973-audio",
350 .pm = &snd_soc_pm_ops,
352 .probe = neo1973_probe,
354 module_platform_driver(neo1973_audio);
356 /* Module information */
357 MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
358 MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 and Frerunner");
359 MODULE_LICENSE("GPL");
360 MODULE_ALIAS("platform:neo1973-audio");