Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / sound / soc / omap / omap3pandora.c
1 /*
2  * omap3pandora.c  --  SoC audio for Pandora Handheld Console
3  *
4  * Author: GraÅžvydas Ignotas <notasas@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301 USA
19  *
20  */
21
22 #include <linux/clk.h>
23 #include <linux/platform_device.h>
24 #include <linux/gpio.h>
25 #include <linux/delay.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/module.h>
28
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/soc.h>
32 #include <sound/pcm_params.h>
33
34 #include <asm/mach-types.h>
35 #include <plat/mcbsp.h>
36
37 #include "omap-mcbsp.h"
38 #include "omap-pcm.h"
39
40 #define OMAP3_PANDORA_DAC_POWER_GPIO    118
41 #define OMAP3_PANDORA_AMP_POWER_GPIO    14
42
43 #define PREFIX "ASoC omap3pandora: "
44
45 static struct regulator *omap3pandora_dac_reg;
46
47 static int omap3pandora_hw_params(struct snd_pcm_substream *substream,
48         struct snd_pcm_hw_params *params)
49 {
50         struct snd_soc_pcm_runtime *rtd = substream->private_data;
51         struct snd_soc_dai *codec_dai = rtd->codec_dai;
52         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
53         int divider;
54         int ret;
55
56         /* Set the codec system clock for DAC and ADC */
57         ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
58                                             SND_SOC_CLOCK_IN);
59         if (ret < 0) {
60                 pr_err(PREFIX "can't set codec system clock\n");
61                 return ret;
62         }
63
64         /* Set McBSP clock to external */
65         ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
66                                      256 * params_rate(params),
67                                      SND_SOC_CLOCK_IN);
68         if (ret < 0) {
69                 pr_err(PREFIX "can't set cpu system clock\n");
70                 return ret;
71         }
72
73         divider = 8;
74         if (snd_pcm_format_physical_width(params_format(params)) > 16)
75                 divider = 4;
76
77         ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, divider);
78         if (ret < 0) {
79                 pr_err(PREFIX "can't set SRG clock divider to %d\n", divider);
80                 return ret;
81         }
82
83         return 0;
84 }
85
86 static int omap3pandora_hw_free(struct snd_pcm_substream *substream)
87 {
88         struct snd_soc_pcm_runtime *rtd = substream->private_data;
89         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
90         int ret;
91
92         /* Set McBSP clock back to internal for power saving to work */
93         ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_FCLK,
94                                      0, SND_SOC_CLOCK_IN);
95         if (ret < 0) {
96                 pr_err(PREFIX "can't set cpu system clock\n");
97                 return ret;
98         }
99
100         return 0;
101 }
102
103 static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
104         struct snd_kcontrol *k, int event)
105 {
106         /*
107          * The PCM1773 DAC datasheet requires 1ms delay between switching
108          * VCC power on/off and /PD pin high/low
109          */
110         if (SND_SOC_DAPM_EVENT_ON(event)) {
111                 regulator_enable(omap3pandora_dac_reg);
112                 mdelay(1);
113                 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
114         } else {
115                 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
116                 mdelay(1);
117                 regulator_disable(omap3pandora_dac_reg);
118         }
119
120         return 0;
121 }
122
123 static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
124         struct snd_kcontrol *k, int event)
125 {
126         if (SND_SOC_DAPM_EVENT_ON(event))
127                 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
128         else
129                 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
130
131         return 0;
132 }
133
134 /*
135  * Audio paths on Pandora board:
136  *
137  *  |O| ---> PCM DAC +-> AMP -> Headphone Jack
138  *  |M|         A    +--------> Line Out
139  *  |A| <~~clk~~+
140  *  |P| <--- TWL4030 <--------- Line In and MICs
141  */
142 static const struct snd_soc_dapm_widget omap3pandora_out_dapm_widgets[] = {
143         SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
144                            0, 0, omap3pandora_dac_event,
145                            SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
146         SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
147                            0, 0, NULL, 0, omap3pandora_hp_event,
148                            SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
149         SND_SOC_DAPM_HP("Headphone Jack", NULL),
150         SND_SOC_DAPM_LINE("Line Out", NULL),
151 };
152
153 static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = {
154         SND_SOC_DAPM_MIC("Mic (internal)", NULL),
155         SND_SOC_DAPM_MIC("Mic (external)", NULL),
156         SND_SOC_DAPM_LINE("Line In", NULL),
157 };
158
159 static const struct snd_soc_dapm_route omap3pandora_out_map[] = {
160         {"PCM DAC", NULL, "APLL Enable"},
161         {"Headphone Amplifier", NULL, "PCM DAC"},
162         {"Line Out", NULL, "PCM DAC"},
163         {"Headphone Jack", NULL, "Headphone Amplifier"},
164 };
165
166 static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
167         {"AUXL", NULL, "Line In"},
168         {"AUXR", NULL, "Line In"},
169
170         {"MAINMIC", NULL, "Mic Bias 1"},
171         {"Mic Bias 1", NULL, "Mic (internal)"},
172
173         {"SUBMIC", NULL, "Mic Bias 2"},
174         {"Mic Bias 2", NULL, "Mic (external)"},
175 };
176
177 static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
178 {
179         struct snd_soc_codec *codec = rtd->codec;
180         struct snd_soc_dapm_context *dapm = &codec->dapm;
181         int ret;
182
183         /* All TWL4030 output pins are floating */
184         snd_soc_dapm_nc_pin(dapm, "EARPIECE");
185         snd_soc_dapm_nc_pin(dapm, "PREDRIVEL");
186         snd_soc_dapm_nc_pin(dapm, "PREDRIVER");
187         snd_soc_dapm_nc_pin(dapm, "HSOL");
188         snd_soc_dapm_nc_pin(dapm, "HSOR");
189         snd_soc_dapm_nc_pin(dapm, "CARKITL");
190         snd_soc_dapm_nc_pin(dapm, "CARKITR");
191         snd_soc_dapm_nc_pin(dapm, "HFL");
192         snd_soc_dapm_nc_pin(dapm, "HFR");
193         snd_soc_dapm_nc_pin(dapm, "VIBRA");
194
195         ret = snd_soc_dapm_new_controls(dapm, omap3pandora_out_dapm_widgets,
196                                 ARRAY_SIZE(omap3pandora_out_dapm_widgets));
197         if (ret < 0)
198                 return ret;
199
200         return snd_soc_dapm_add_routes(dapm, omap3pandora_out_map,
201                 ARRAY_SIZE(omap3pandora_out_map));
202 }
203
204 static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
205 {
206         struct snd_soc_codec *codec = rtd->codec;
207         struct snd_soc_dapm_context *dapm = &codec->dapm;
208         int ret;
209
210         /* Not comnnected */
211         snd_soc_dapm_nc_pin(dapm, "HSMIC");
212         snd_soc_dapm_nc_pin(dapm, "CARKITMIC");
213         snd_soc_dapm_nc_pin(dapm, "DIGIMIC0");
214         snd_soc_dapm_nc_pin(dapm, "DIGIMIC1");
215
216         ret = snd_soc_dapm_new_controls(dapm, omap3pandora_in_dapm_widgets,
217                                 ARRAY_SIZE(omap3pandora_in_dapm_widgets));
218         if (ret < 0)
219                 return ret;
220
221         return snd_soc_dapm_add_routes(dapm, omap3pandora_in_map,
222                 ARRAY_SIZE(omap3pandora_in_map));
223 }
224
225 static struct snd_soc_ops omap3pandora_ops = {
226         .hw_params = omap3pandora_hw_params,
227         .hw_free = omap3pandora_hw_free,
228 };
229
230 /* Digital audio interface glue - connects codec <--> CPU */
231 static struct snd_soc_dai_link omap3pandora_dai[] = {
232         {
233                 .name = "PCM1773",
234                 .stream_name = "HiFi Out",
235                 .cpu_dai_name = "omap-mcbsp-dai.1",
236                 .codec_dai_name = "twl4030-hifi",
237                 .platform_name = "omap-pcm-audio",
238                 .codec_name = "twl4030-codec",
239                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
240                            SND_SOC_DAIFMT_CBS_CFS,
241                 .ops = &omap3pandora_ops,
242                 .init = omap3pandora_out_init,
243         }, {
244                 .name = "TWL4030",
245                 .stream_name = "Line/Mic In",
246                 .cpu_dai_name = "omap-mcbsp-dai.3",
247                 .codec_dai_name = "twl4030-hifi",
248                 .platform_name = "omap-pcm-audio",
249                 .codec_name = "twl4030-codec",
250                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
251                            SND_SOC_DAIFMT_CBS_CFS,
252                 .ops = &omap3pandora_ops,
253                 .init = omap3pandora_in_init,
254         }
255 };
256
257 /* SoC card */
258 static struct snd_soc_card snd_soc_card_omap3pandora = {
259         .name = "omap3pandora",
260         .dai_link = omap3pandora_dai,
261         .num_links = ARRAY_SIZE(omap3pandora_dai),
262 };
263
264 static struct platform_device *omap3pandora_snd_device;
265
266 static int __init omap3pandora_soc_init(void)
267 {
268         int ret;
269
270         if (!machine_is_omap3_pandora())
271                 return -ENODEV;
272
273         pr_info("OMAP3 Pandora SoC init\n");
274
275         ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
276         if (ret) {
277                 pr_err(PREFIX "Failed to get DAC power GPIO\n");
278                 return ret;
279         }
280
281         ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
282         if (ret) {
283                 pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
284                 goto fail0;
285         }
286
287         ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
288         if (ret) {
289                 pr_err(PREFIX "Failed to get amp power GPIO\n");
290                 goto fail0;
291         }
292
293         ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
294         if (ret) {
295                 pr_err(PREFIX "Failed to set amp power GPIO direction\n");
296                 goto fail1;
297         }
298
299         omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
300         if (omap3pandora_snd_device == NULL) {
301                 pr_err(PREFIX "Platform device allocation failed\n");
302                 ret = -ENOMEM;
303                 goto fail1;
304         }
305
306         platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
307
308         ret = platform_device_add(omap3pandora_snd_device);
309         if (ret) {
310                 pr_err(PREFIX "Unable to add platform device\n");
311                 goto fail2;
312         }
313
314         omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
315         if (IS_ERR(omap3pandora_dac_reg)) {
316                 pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
317                         dev_name(&omap3pandora_snd_device->dev),
318                         PTR_ERR(omap3pandora_dac_reg));
319                 ret = PTR_ERR(omap3pandora_dac_reg);
320                 goto fail3;
321         }
322
323         return 0;
324
325 fail3:
326         platform_device_del(omap3pandora_snd_device);
327 fail2:
328         platform_device_put(omap3pandora_snd_device);
329 fail1:
330         gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
331 fail0:
332         gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
333         return ret;
334 }
335 module_init(omap3pandora_soc_init);
336
337 static void __exit omap3pandora_soc_exit(void)
338 {
339         regulator_put(omap3pandora_dac_reg);
340         platform_device_unregister(omap3pandora_snd_device);
341         gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
342         gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
343 }
344 module_exit(omap3pandora_soc_exit);
345
346 MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
347 MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
348 MODULE_LICENSE("GPL");