ASoC: neo1973_gta02_wm8753: Use gpio_request_array to request gpios
[firefly-linux-kernel-4.4.55.git] / sound / soc / samsung / neo1973_gta02_wm8753.c
1 /*
2  * neo1973_gta02_wm8753.c  --  SoC audio for Openmoko Freerunner(GTA02)
3  *
4  * Copyright 2007 Openmoko Inc
5  * Author: Graeme Gregory <graeme@openmoko.org>
6  * Copyright 2007 Wolfson Microelectronics PLC.
7  * Author: Graeme Gregory <linux@wolfsonmicro.com>
8  * Copyright 2009 Wolfson Microelectronics
9  *
10  *  This program is free software; you can redistribute  it and/or modify it
11  *  under  the terms of  the GNU General  Public License as published by the
12  *  Free Software Foundation;  either version 2 of the  License, or (at your
13  *  option) any later version.
14  */
15
16 #include <linux/gpio.h>
17
18 #include <sound/soc.h>
19
20 #include <asm/mach-types.h>
21 #include <plat/regs-iis.h>
22 #include <mach/gta02.h>
23
24 #include "../codecs/wm8753.h"
25 #include "s3c24xx-i2s.h"
26
27 static struct snd_soc_card neo1973_gta02;
28
29 static int neo1973_gta02_hifi_hw_params(struct snd_pcm_substream *substream,
30         struct snd_pcm_hw_params *params)
31 {
32         struct snd_soc_pcm_runtime *rtd = substream->private_data;
33         struct snd_soc_dai *codec_dai = rtd->codec_dai;
34         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
35         unsigned int pll_out = 0, bclk = 0;
36         int ret = 0;
37         unsigned long iis_clkrate;
38
39         iis_clkrate = s3c24xx_i2s_get_clockrate();
40
41         switch (params_rate(params)) {
42         case 8000:
43         case 16000:
44                 pll_out = 12288000;
45                 break;
46         case 48000:
47                 bclk = WM8753_BCLK_DIV_4;
48                 pll_out = 12288000;
49                 break;
50         case 96000:
51                 bclk = WM8753_BCLK_DIV_2;
52                 pll_out = 12288000;
53                 break;
54         case 11025:
55                 bclk = WM8753_BCLK_DIV_16;
56                 pll_out = 11289600;
57                 break;
58         case 22050:
59                 bclk = WM8753_BCLK_DIV_8;
60                 pll_out = 11289600;
61                 break;
62         case 44100:
63                 bclk = WM8753_BCLK_DIV_4;
64                 pll_out = 11289600;
65                 break;
66         case 88200:
67                 bclk = WM8753_BCLK_DIV_2;
68                 pll_out = 11289600;
69                 break;
70         }
71
72         /* set codec DAI configuration */
73         ret = snd_soc_dai_set_fmt(codec_dai,
74                 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
75                 SND_SOC_DAIFMT_CBM_CFM);
76         if (ret < 0)
77                 return ret;
78
79         /* set cpu DAI configuration */
80         ret = snd_soc_dai_set_fmt(cpu_dai,
81                 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
82                 SND_SOC_DAIFMT_CBM_CFM);
83         if (ret < 0)
84                 return ret;
85
86         /* set the codec system clock for DAC and ADC */
87         ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_MCLK, pll_out,
88                 SND_SOC_CLOCK_IN);
89         if (ret < 0)
90                 return ret;
91
92         /* set MCLK division for sample rate */
93         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_MCLK,
94                 S3C2410_IISMOD_32FS);
95         if (ret < 0)
96                 return ret;
97
98         /* set codec BCLK division for sample rate */
99         ret = snd_soc_dai_set_clkdiv(codec_dai,
100                                         WM8753_BCLKDIV, bclk);
101         if (ret < 0)
102                 return ret;
103
104         /* set prescaler division for sample rate */
105         ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
106                 S3C24XX_PRESCALE(4, 4));
107         if (ret < 0)
108                 return ret;
109
110         /* codec PLL input is PCLK/4 */
111         ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0,
112                 iis_clkrate / 4, pll_out);
113         if (ret < 0)
114                 return ret;
115
116         return 0;
117 }
118
119 static int neo1973_gta02_hifi_hw_free(struct snd_pcm_substream *substream)
120 {
121         struct snd_soc_pcm_runtime *rtd = substream->private_data;
122         struct snd_soc_dai *codec_dai = rtd->codec_dai;
123
124         /* disable the PLL */
125         return snd_soc_dai_set_pll(codec_dai, WM8753_PLL1, 0, 0, 0);
126 }
127
128 /*
129  * Neo1973 WM8753 HiFi DAI opserations.
130  */
131 static struct snd_soc_ops neo1973_gta02_hifi_ops = {
132         .hw_params = neo1973_gta02_hifi_hw_params,
133         .hw_free = neo1973_gta02_hifi_hw_free,
134 };
135
136 static int neo1973_gta02_voice_hw_params(
137         struct snd_pcm_substream *substream,
138         struct snd_pcm_hw_params *params)
139 {
140         struct snd_soc_pcm_runtime *rtd = substream->private_data;
141         struct snd_soc_dai *codec_dai = rtd->codec_dai;
142         unsigned int pcmdiv = 0;
143         int ret = 0;
144         unsigned long iis_clkrate;
145
146         iis_clkrate = s3c24xx_i2s_get_clockrate();
147
148         if (params_rate(params) != 8000)
149                 return -EINVAL;
150         if (params_channels(params) != 1)
151                 return -EINVAL;
152
153         pcmdiv = WM8753_PCM_DIV_6; /* 2.048 MHz */
154
155         /* todo: gg check mode (DSP_B) against CSR datasheet */
156         /* set codec DAI configuration */
157         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_B |
158                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
159         if (ret < 0)
160                 return ret;
161
162         /* set the codec system clock for DAC and ADC */
163         ret = snd_soc_dai_set_sysclk(codec_dai, WM8753_PCMCLK,
164                 12288000, SND_SOC_CLOCK_IN);
165         if (ret < 0)
166                 return ret;
167
168         /* set codec PCM division for sample rate */
169         ret = snd_soc_dai_set_clkdiv(codec_dai, WM8753_PCMDIV,
170                                         pcmdiv);
171         if (ret < 0)
172                 return ret;
173
174         /* configure and enable PLL for 12.288MHz output */
175         ret = snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0,
176                 iis_clkrate / 4, 12288000);
177         if (ret < 0)
178                 return ret;
179
180         return 0;
181 }
182
183 static int neo1973_gta02_voice_hw_free(struct snd_pcm_substream *substream)
184 {
185         struct snd_soc_pcm_runtime *rtd = substream->private_data;
186         struct snd_soc_dai *codec_dai = rtd->codec_dai;
187
188         /* disable the PLL */
189         return snd_soc_dai_set_pll(codec_dai, WM8753_PLL2, 0, 0, 0);
190 }
191
192 static struct snd_soc_ops neo1973_gta02_voice_ops = {
193         .hw_params = neo1973_gta02_voice_hw_params,
194         .hw_free = neo1973_gta02_voice_hw_free,
195 };
196
197 static int gta02_speaker_enabled;
198
199 static int lm4853_set_spk(struct snd_kcontrol *kcontrol,
200         struct snd_ctl_elem_value *ucontrol)
201 {
202         gta02_speaker_enabled = ucontrol->value.integer.value[0];
203
204         gpio_set_value(GTA02_GPIO_HP_IN, !gta02_speaker_enabled);
205
206         return 0;
207 }
208
209 static int lm4853_get_spk(struct snd_kcontrol *kcontrol,
210         struct snd_ctl_elem_value *ucontrol)
211 {
212         ucontrol->value.integer.value[0] = gta02_speaker_enabled;
213         return 0;
214 }
215
216 static int lm4853_event(struct snd_soc_dapm_widget *w,
217                         struct snd_kcontrol *k, int event)
218 {
219         gpio_set_value(GTA02_GPIO_AMP_SHUT, SND_SOC_DAPM_EVENT_OFF(event));
220
221         return 0;
222 }
223
224 static const struct snd_soc_dapm_widget wm8753_dapm_widgets[] = {
225         SND_SOC_DAPM_SPK("Stereo Out", lm4853_event),
226         SND_SOC_DAPM_LINE("GSM Line Out", NULL),
227         SND_SOC_DAPM_LINE("GSM Line In", NULL),
228         SND_SOC_DAPM_MIC("Headset Mic", NULL),
229         SND_SOC_DAPM_MIC("Handset Mic", NULL),
230         SND_SOC_DAPM_SPK("Handset Spk", NULL),
231 };
232
233
234 /* example machine audio_mapnections */
235 static const struct snd_soc_dapm_route audio_map[] = {
236
237         /* Connections to the lm4853 amp */
238         {"Stereo Out", NULL, "LOUT1"},
239         {"Stereo Out", NULL, "ROUT1"},
240
241         /* Connections to the GSM Module */
242         {"GSM Line Out", NULL, "MONO1"},
243         {"GSM Line Out", NULL, "MONO2"},
244         {"RXP", NULL, "GSM Line In"},
245         {"RXN", NULL, "GSM Line In"},
246
247         /* Connections to Headset */
248         {"MIC1", NULL, "Mic Bias"},
249         {"Mic Bias", NULL, "Headset Mic"},
250
251         /* Call Mic */
252         {"MIC2", NULL, "Mic Bias"},
253         {"MIC2N", NULL, "Mic Bias"},
254         {"Mic Bias", NULL, "Handset Mic"},
255
256         /* Call Speaker */
257         {"Handset Spk", NULL, "LOUT2"},
258         {"Handset Spk", NULL, "ROUT2"},
259
260         /* Connect the ALC pins */
261         {"ACIN", NULL, "ACOP"},
262 };
263
264 static const struct snd_kcontrol_new wm8753_neo1973_gta02_controls[] = {
265         SOC_DAPM_PIN_SWITCH("Stereo Out"),
266         SOC_DAPM_PIN_SWITCH("GSM Line Out"),
267         SOC_DAPM_PIN_SWITCH("GSM Line In"),
268         SOC_DAPM_PIN_SWITCH("Headset Mic"),
269         SOC_DAPM_PIN_SWITCH("Handset Mic"),
270         SOC_DAPM_PIN_SWITCH("Handset Spk"),
271
272         SOC_SINGLE_BOOL_EXT("Amp Spk Switch", 0,
273                 lm4853_get_spk,
274                 lm4853_set_spk),
275 };
276
277 /*
278  * This is an example machine initialisation for a wm8753 connected to a
279  * neo1973 GTA02.
280  */
281 static int neo1973_gta02_wm8753_init(struct snd_soc_pcm_runtime *rtd)
282 {
283         struct snd_soc_codec *codec = rtd->codec;
284         struct snd_soc_dapm_context *dapm = &codec->dapm;
285         int err;
286
287         /* set up NC codec pins */
288         snd_soc_dapm_nc_pin(dapm, "OUT3");
289         snd_soc_dapm_nc_pin(dapm, "OUT4");
290         snd_soc_dapm_nc_pin(dapm, "LINE1");
291         snd_soc_dapm_nc_pin(dapm, "LINE2");
292
293         /* Add neo1973 gta02 specific widgets */
294         snd_soc_dapm_new_controls(dapm, wm8753_dapm_widgets,
295                                   ARRAY_SIZE(wm8753_dapm_widgets));
296
297         /* add neo1973 gta02 specific controls */
298         err = snd_soc_add_controls(codec, wm8753_neo1973_gta02_controls,
299                 ARRAY_SIZE(wm8753_neo1973_gta02_controls));
300
301         if (err < 0)
302                 return err;
303
304         /* set up neo1973 gta02 specific audio path audio_map */
305         snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
306
307         /* set endpoints to default off mode */
308         snd_soc_dapm_disable_pin(dapm, "Stereo Out");
309         snd_soc_dapm_disable_pin(dapm, "GSM Line Out");
310         snd_soc_dapm_disable_pin(dapm, "GSM Line In");
311         snd_soc_dapm_disable_pin(dapm, "Headset Mic");
312         snd_soc_dapm_disable_pin(dapm, "Handset Mic");
313         snd_soc_dapm_disable_pin(dapm, "Handset Spk");
314
315         /* allow audio paths from the GSM modem to run during suspend */
316         snd_soc_dapm_ignore_suspend(dapm, "Stereo Out");
317         snd_soc_dapm_ignore_suspend(dapm, "GSM Line Out");
318         snd_soc_dapm_ignore_suspend(dapm, "GSM Line In");
319         snd_soc_dapm_ignore_suspend(dapm, "Headset Mic");
320         snd_soc_dapm_ignore_suspend(dapm, "Handset Mic");
321         snd_soc_dapm_ignore_suspend(dapm, "Handset Spk");
322
323         snd_soc_dapm_sync(dapm);
324
325         return 0;
326 }
327
328 /*
329  * BT Codec DAI
330  */
331 static struct snd_soc_dai_driver bt_dai = {
332         .name = "bluetooth-dai",
333         .playback = {
334                 .channels_min = 1,
335                 .channels_max = 1,
336                 .rates = SNDRV_PCM_RATE_8000,
337                 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
338         .capture = {
339                 .channels_min = 1,
340                 .channels_max = 1,
341                 .rates = SNDRV_PCM_RATE_8000,
342                 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
343 };
344
345 static struct snd_soc_dai_link neo1973_gta02_dai[] = {
346 { /* Hifi Playback - for similatious use with voice below */
347         .name = "WM8753",
348         .stream_name = "WM8753 HiFi",
349         .cpu_dai_name = "s3c24xx-iis",
350         .codec_dai_name = "wm8753-hifi",
351         .init = neo1973_gta02_wm8753_init,
352         .platform_name = "samsung-audio",
353         .codec_name = "wm8753-codec.0-001a",
354         .ops = &neo1973_gta02_hifi_ops,
355 },
356 { /* Voice via BT */
357         .name = "Bluetooth",
358         .stream_name = "Voice",
359         .cpu_dai_name = "bluetooth-dai",
360         .codec_dai_name = "wm8753-voice",
361         .ops = &neo1973_gta02_voice_ops,
362         .codec_name = "wm8753-codec.0-001a",
363         .platform_name = "samsung-audio",
364 },
365 };
366
367 static struct snd_soc_card neo1973_gta02 = {
368         .name = "neo1973-gta02",
369         .dai_link = neo1973_gta02_dai,
370         .num_links = ARRAY_SIZE(neo1973_gta02_dai),
371 };
372
373 static const struct gpio neo1973_gta02_gpios[] = {
374         { GTA02_GPIO_HP_IN, GPIOF_OUT_INIT_HIGH, "GTA02_HP_IN" },
375         { GTA02_GPIO_AMP_SHUT, GPIOF_OUT_INIT_HIGH, "GTA02_AMP_SHUT" },
376 };
377
378 static struct platform_device *neo1973_gta02_snd_device;
379
380 static int __init neo1973_gta02_init(void)
381 {
382         int ret;
383
384         if (!machine_is_neo1973_gta02()) {
385                 printk(KERN_INFO
386                        "Only GTA02 is supported by this ASoC driver\n");
387                 return -ENODEV;
388         }
389
390         ret = gpio_request_array(neo1973_gta02_gpios,
391                                 ARRAY_SIZE(neo1973_gta02_gpios));
392         if (ret)
393                 return ret;
394
395         neo1973_gta02_snd_device = platform_device_alloc("soc-audio", -1);
396         if (!neo1973_gta02_snd_device) {
397                 ret = -ENOMEM;
398                 goto err_gpio_free;
399         }
400
401         /* register bluetooth DAI here */
402         ret = snd_soc_register_dai(&neo1973_gta02_snd_device->dev, &bt_dai);
403         if (ret)
404                 goto err_put_device;
405
406         platform_set_drvdata(neo1973_gta02_snd_device, &neo1973_gta02);
407         ret = platform_device_add(neo1973_gta02_snd_device);
408
409         if (ret)
410                 goto err_unregister_dai;
411
412         return 0;
413
414 err_unregister_dai:
415         snd_soc_unregister_dai(&neo1973_gta02_snd_device->dev);
416 err_put_device:
417         platform_device_put(neo1973_gta02_snd_device);
418 err_gpio_free:
419         gpio_free_array(neo1973_gta02_gpios, ARRAY_SIZE(neo1973_gta02_gpios));
420         return ret;
421 }
422 module_init(neo1973_gta02_init);
423
424 static void __exit neo1973_gta02_exit(void)
425 {
426         snd_soc_unregister_dai(&neo1973_gta02_snd_device->dev);
427         platform_device_unregister(neo1973_gta02_snd_device);
428         gpio_free_array(neo1973_gta02_gpios, ARRAY_SIZE(neo1973_gta02_gpios));
429 }
430 module_exit(neo1973_gta02_exit);
431
432 /* Module information */
433 MODULE_AUTHOR("Graeme Gregory, graeme@openmoko.org");
434 MODULE_DESCRIPTION("ALSA SoC WM8753 Neo1973 GTA02");
435 MODULE_LICENSE("GPL");