7761827314b2528d8d8ec3d7c3c33aef7862a1d4
[firefly-linux-kernel-4.4.55.git] / sound / soc / samsung / neo1973_wm8753.c
1 /*
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  *
13  */
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 <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/soc.h>
23
24 #include <asm/mach-types.h>
25 #include <mach/regs-clock.h>
26 #include <mach/regs-gpio.h>
27 #include <mach/hardware.h>
28 #include <linux/io.h>
29 #include <mach/spi-gpio.h>
30
31 #include <plat/regs-iis.h>
32
33 #include "../codecs/wm8753.h"
34 #include "dma.h"
35 #include "s3c24xx-i2s.h"
36
37 static struct snd_soc_card neo1973;
38
39 static int neo1973_hifi_hw_params(struct snd_pcm_substream *substream,
40         struct snd_pcm_hw_params *params)
41 {
42         struct snd_soc_pcm_runtime *rtd = substream->private_data;
43         struct snd_soc_dai *codec_dai = rtd->codec_dai;
44         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
45         unsigned int pll_out = 0, bclk = 0;
46         int ret = 0;
47         unsigned long iis_clkrate;
48
49         pr_debug("Entered %s\n", __func__);
50
51         iis_clkrate = s3c24xx_i2s_get_clockrate();
52
53         switch (params_rate(params)) {
54         case 8000:
55         case 16000:
56                 pll_out = 12288000;
57                 break;
58         case 48000:
59                 bclk = WM8753_BCLK_DIV_4;
60                 pll_out = 12288000;
61                 break;
62         case 96000:
63                 bclk = WM8753_BCLK_DIV_2;
64                 pll_out = 12288000;
65                 break;
66         case 11025:
67                 bclk = WM8753_BCLK_DIV_16;
68                 pll_out = 11289600;
69                 break;
70         case 22050:
71                 bclk = WM8753_BCLK_DIV_8;
72                 pll_out = 11289600;
73                 break;
74         case 44100:
75                 bclk = WM8753_BCLK_DIV_4;
76                 pll_out = 11289600;
77                 break;
78         case 88200:
79                 bclk = WM8753_BCLK_DIV_2;
80                 pll_out = 11289600;
81                 break;
82         }
83
84         /* set codec DAI configuration */
85         ret = snd_soc_dai_set_fmt(codec_dai,
86                 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
87                 SND_SOC_DAIFMT_CBM_CFM);
88         if (ret < 0)
89                 return ret;
90
91         /* set cpu DAI configuration */
92         ret = snd_soc_dai_set_fmt(cpu_dai,
93                 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
94                 SND_SOC_DAIFMT_CBM_CFM);
95         if (ret < 0)
96                 return ret;
97
98         /* set the codec system clock for DAC and ADC */
99         ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
100                 SND_SOC_CLOCK_IN);
101         if (ret < 0)
102                 return ret;
103
104         /* set MCLK division for sample rate */
105         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
106                 S3C2410_IISMOD_32FS);
107         if (ret < 0)
108                 return ret;
109
110         /* set codec BCLK division for sample rate */
111         ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_BCLKDIV, bclk);
112         if (ret < 0)
113                 return ret;
114
115         /* set prescaler division for sample rate */
116         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
117                 S3C24XX_PRESCALE(4, 4));
118         if (ret < 0)
119                 return ret;
120
121         /* codec PLL input is PCLK/4 */
122         ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
123                 iis_clkrate / 4, pll_out);
124         if (ret < 0)
125                 return ret;
126
127         return 0;
128 }
129
130 static int neo1973_hifi_hw_free(struct snd_pcm_substream *substream)
131 {
132         struct snd_soc_pcm_runtime *rtd = substream->private_data;
133         struct snd_soc_dai *codec_dai = rtd->codec_dai;
134
135         pr_debug("Entered %s\n", __func__);
136
137         /* disable the PLL */
138         return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0, 0);
139 }
140
141 /*
142  * Neo1973 WM8753 HiFi DAI opserations.
143  */
144 static struct snd_soc_ops neo1973_hifi_ops = {
145         .hw_params = neo1973_hifi_hw_params,
146         .hw_free = neo1973_hifi_hw_free,
147 };
148
149 static int neo1973_voice_hw_params(struct snd_pcm_substream *substream,
150         struct snd_pcm_hw_params *params)
151 {
152         struct snd_soc_pcm_runtime *rtd = substream->private_data;
153         struct snd_soc_dai *codec_dai = rtd->codec_dai;
154         unsigned int pcmdiv = 0;
155         int ret = 0;
156         unsigned long iis_clkrate;
157
158         pr_debug("Entered %s\n", __func__);
159
160         iis_clkrate = s3c24xx_i2s_get_clockrate();
161
162         if (params_rate(params) != 8000)
163                 return -EINVAL;
164         if (params_channels(params) != 1)
165                 return -EINVAL;
166
167         pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
168
169         /* todo: gg check mode (DSP_B) against CSR datasheet */
170         /* set codec DAI configuration */
171         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
172                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
173         if (ret < 0)
174                 return ret;
175
176         /* set the codec system clock for DAC and ADC */
177         ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK, 12288000,
178                 SND_SOC_CLOCK_IN);
179         if (ret < 0)
180                 return ret;
181
182         /* set codec PCM division for sample rate */
183         ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV, pcmdiv);
184         if (ret < 0)
185                 return ret;
186
187         /* configure and enable PLL for 12.288MHz output */
188         ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
189                 iis_clkrate / 4, 12288000);
190         if (ret < 0)
191                 return ret;
192
193         return 0;
194 }
195
196 static int neo1973_voice_hw_free(struct snd_pcm_substream *substream)
197 {
198         struct snd_soc_pcm_runtime *rtd = substream->private_data;
199         struct snd_soc_dai *codec_dai = rtd->codec_dai;
200
201         pr_debug("Entered %s\n", __func__);
202
203         /* disable the PLL */
204         return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0, 0);
205 }
206
207 static struct snd_soc_ops neo1973_voice_ops = {
208         .hw_params = neo1973_voice_hw_params,
209         .hw_free = neo1973_voice_hw_free,
210 };
211
212 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
213         SND_SOC_DAPM_LINE("GSM Line Out", NULL),
214         SND_SOC_DAPM_LINE("GSM Line In", NULL),
215         SND_SOC_DAPM_MIC("Headset Mic", NULL),
216         SND_SOC_DAPM_MIC("Call Mic", NULL),
217 };
218
219
220 static const struct snd_soc_dapm_route neo1973_wm8753_routes[] = {
221         /* Connections to the GSM Module */
222         {"GSM Line Out", NULL, "MONO1"},
223         {"GSM Line Out", NULL, "MONO2"},
224         {"RXP", NULL, "GSM Line In"},
225         {"RXN", NULL, "GSM Line In"},
226
227         /* Connections to Headset */
228         {"MIC1", NULL, "Mic Bias"},
229         {"Mic Bias", NULL, "Headset Mic"},
230
231         /* Call Mic */
232         {"MIC2", NULL, "Mic Bias"},
233         {"MIC2N", NULL, "Mic Bias"},
234         {"Mic Bias", NULL, "Call Mic"},
235
236         /* Connect the ALC pins */
237         {"ACIN", NULL, "ACOP"},
238 };
239
240 static const struct snd_kcontrol_new neo1973_wm8753_controls[] = {
241         SOC_DAPM_PIN_SWITCH("GSM Line Out"),
242         SOC_DAPM_PIN_SWITCH("GSM Line In"),
243         SOC_DAPM_PIN_SWITCH("Headset Mic"),
244         SOC_DAPM_PIN_SWITCH("Call Mic"),
245 };
246
247
248 /*
249  * This is an example machine initialisation for a wm8753 connected to a
250  * neo1973 II. It is missing logic to detect hp/mic insertions and logic
251  * to re-route the audio in such an event.
252  */
253 static int neo1973_wm8753_init(struct snd_soc_pcm_runtime *rtd)
254 {
255         struct snd_soc_codec *codec = rtd->codec;
256         struct snd_soc_dapm_context *dapm = &codec->dapm;
257         int err;
258
259         pr_debug("Entered %s\n", __func__);
260
261         /* set up NC codec pins */
262         snd_soc_dapm_nc_pin(dapm, "LOUT2");
263         snd_soc_dapm_nc_pin(dapm, "ROUT2");
264         snd_soc_dapm_nc_pin(dapm, "OUT3");
265         snd_soc_dapm_nc_pin(dapm, "OUT4");
266         snd_soc_dapm_nc_pin(dapm, "LINE1");
267         snd_soc_dapm_nc_pin(dapm, "LINE2");
268
269         /* Add neo1973 specific widgets */
270         snd_soc_dapm_new_controls(dapm, wm8753_dapm_widgets,
271                         ARRAY_SIZE(wm8753_dapm_widgets));
272
273         /* set endpoints to default mode */
274         snd_soc_dapm_disable_pin(dapm, "GSM Line Out");
275         snd_soc_dapm_disable_pin(dapm, "GSM Line In");
276         snd_soc_dapm_disable_pin(dapm, "Headset Mic");
277         snd_soc_dapm_disable_pin(dapm, "Call Mic");
278
279         /* add neo1973 specific controls */
280         err = snd_soc_add_controls(codec, neo1973_wm8753_controls,
281                                 ARRAY_SIZE(neo1973_wm8753_controls));
282         if (err < 0)
283                 return err;
284
285         /* set up neo1973 specific audio routes */
286         err = snd_soc_dapm_add_routes(dapm, neo1973_wm8753_routes,
287                         ARRAY_SIZE(neo1973_wm8753_routes));
288
289         snd_soc_dapm_sync(dapm);
290         return 0;
291 }
292
293 static const struct snd_soc_dapm_route neo1973_lm4857_routes[] = {
294         {"Amp IN", NULL, "ROUT1"},
295         {"Amp IN", NULL, "LOUT1"},
296
297         {"Handset Spk", NULL, "Amp EP"},
298         {"Stereo Out", NULL, "Amp LS"},
299         {"Headphone", NULL, "Amp HP"},
300 };
301
302 static const struct snd_soc_dapm_widget neo1973_lm4857_dapm_widgets[] = {
303         SND_SOC_DAPM_SPK("Handset Spk", NULL),
304         SND_SOC_DAPM_SPK("Stereo Out", NULL),
305         SND_SOC_DAPM_HP("Headphone", NULL),
306 };
307
308 static int neo1973_lm4857_init(struct snd_soc_dapm_context *dapm)
309 {
310         int ret;
311
312         ret = snd_soc_dapm_new_controls(dapm, neo1973_lm4857_dapm_widgets,
313                         ARRAY_SIZE(neo1973_lm4857_dapm_widgets));
314         if (ret)
315                 return ret;
316
317         ret = snd_soc_dapm_add_routes(dapm, neo1973_lm4857_routes,
318                         ARRAY_SIZE(neo1973_lm4857_routes));
319         if (ret)
320                 return ret;
321
322         snd_soc_dapm_ignore_suspend(dapm, "Stereo Out");
323         snd_soc_dapm_ignore_suspend(dapm, "Handset Spk");
324         snd_soc_dapm_ignore_suspend(dapm, "Headphone");
325
326         snd_soc_dapm_sync(dapm);
327
328         return 0;
329 }
330
331 /*
332  * BT Codec DAI
333  */
334 static struct snd_soc_dai bt_dai = {
335         .name = "bluetooth-dai",
336         .playback = {
337                 .channels_min = 1,
338                 .channels_max = 1,
339                 .rates = SNDRV_PCM_RATE_8000,
340                 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
341         .capture = {
342                 .channels_min = 1,
343                 .channels_max = 1,
344                 .rates = SNDRV_PCM_RATE_8000,
345                 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
346 };
347
348 static struct snd_soc_dai_link neo1973_dai[] = {
349 { /* Hifi Playback - for similatious use with voice below */
350         .name = "WM8753",
351         .stream_name = "WM8753 HiFi",
352         .platform_name = "samsung-audio",
353         .cpu_dai_name = "s3c24xx-iis",
354         .codec_dai_name = "wm8753-hifi",
355         .codec_name = "wm8753-codec.0-001a",
356         .init = neo1973_wm8753_init,
357         .ops = &neo1973_hifi_ops,
358 },
359 { /* Voice via BT */
360         .name = "Bluetooth",
361         .stream_name = "Voice",
362         .platform_name = "samsung-audio",
363         .cpu_dai_name = "bluetooth-dai",
364         .codec_dai_name = "wm8753-voice",
365         .codec_name = "wm8753-codec.0-001a",
366         .ops = &neo1973_voice_ops,
367 },
368 };
369
370 static struct snd_soc_aux_dev neo1973_aux_devs[] = {
371         {
372                 .name = "lm4857",
373                 .codec_name = "lm4857.0-007c",
374                 .init = neo1973_lm4857_init,
375         },
376 };
377
378 static struct snd_soc_codec_conf neo1973_codec_conf[] = {
379         {
380                 .dev_name = "lm4857.0-007c",
381                 .name_prefix = "Amp",
382         },
383 };
384
385 static struct snd_soc_card neo1973 = {
386         .name = "neo1973",
387         .dai_link = neo1973_dai,
388         .num_links = ARRAY_SIZE(neo1973_dai),
389         .aux_dev = neo1973_aux_devs,
390         .num_aux_devs = ARRAY_SIZE(neo1973_aux_devs),
391         .codec_conf = neo1973_codec_conf,
392         .num_configs = ARRAY_SIZE(neo1973_codec_conf),
393 };
394
395 static struct platform_device *neo1973_snd_device;
396
397 static int __init neo1973_init(void)
398 {
399         int ret;
400
401         pr_debug("Entered %s\n", __func__);
402
403         if (!machine_is_neo1973_gta01()) {
404                 printk(KERN_INFO
405                         "Only GTA01 hardware supported by ASoC driver\n");
406                 return -ENODEV;
407         }
408
409         neo1973_snd_device = platform_device_alloc("soc-audio", -1);
410         if (!neo1973_snd_device)
411                 return -ENOMEM;
412
413         platform_set_drvdata(neo1973_snd_device, &neo1973);
414         ret = platform_device_add(neo1973_snd_device);
415
416         if (ret) {
417                 platform_device_put(neo1973_snd_device);
418                 return ret;
419         }
420
421         if (ret != 0)
422                 platform_device_unregister(neo1973_snd_device);
423
424         return ret;
425 }
426
427 static void __exit neo1973_exit(void)
428 {
429         pr_debug("Entered %s\n", __func__);
430
431         platform_device_unregister(neo1973_snd_device);
432 }
433
434 module_init(neo1973_init);
435 module_exit(neo1973_exit);
436
437 /* Module information */
438 MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org, www.openmoko.org");
439 MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973");
440 MODULE_LICENSE("GPL");