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 static struct regulator *omap3pandora_amp_reg;
47
48 static int omap3pandora_hw_params(struct snd_pcm_substream *substream,
49         struct snd_pcm_hw_params *params)
50 {
51         struct snd_soc_pcm_runtime *rtd = substream->private_data;
52         struct snd_soc_dai *codec_dai = rtd->codec_dai;
53         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
54         int divider;
55         int ret;
56
57         /* Set the codec system clock for DAC and ADC */
58         ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
59                                             SND_SOC_CLOCK_IN);
60         if (ret < 0) {
61                 pr_err(PREFIX "can't set codec system clock\n");
62                 return ret;
63         }
64
65         /* Set McBSP clock to external */
66         ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
67                                      256 * params_rate(params),
68                                      SND_SOC_CLOCK_IN);
69         if (ret < 0) {
70                 pr_err(PREFIX "can't set cpu system clock\n");
71                 return ret;
72         }
73
74         divider = 8;
75         if (snd_pcm_format_physical_width(params_format(params)) > 16)
76                 divider = 4;
77
78         ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, divider);
79         if (ret < 0) {
80                 pr_err(PREFIX "can't set SRG clock divider to %d\n", divider);
81                 return ret;
82         }
83
84         return 0;
85 }
86
87 static int omap3pandora_hw_free(struct snd_pcm_substream *substream)
88 {
89         struct snd_soc_pcm_runtime *rtd = substream->private_data;
90         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
91         int ret;
92
93         /* Set McBSP clock back to internal for power saving to work */
94         ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_FCLK,
95                                      0, SND_SOC_CLOCK_IN);
96         if (ret < 0) {
97                 pr_err(PREFIX "can't set cpu system clock\n");
98                 return ret;
99         }
100
101         return 0;
102 }
103
104 #include <plat/mcbsp.h>
105
106 static struct omap_mcbsp_reg_cfg cfg = {
107         .spcr2 = XINTM(3) | FREE,
108         .spcr1 = RINTM(3),
109         .rcr2  = RPHASE | RWDLEN2(OMAP_MCBSP_WORD_16) | RDATDLY(1),
110         .rcr1  = RWDLEN1(OMAP_MCBSP_WORD_16),
111         .xcr2  = XPHASE | XWDLEN2(OMAP_MCBSP_WORD_16) | XDATDLY(1),
112         .xcr1  = XWDLEN1(OMAP_MCBSP_WORD_16),
113         .srgr2 = FSGM | FPER(32 - 1),
114         .srgr1 = FWID(32 / 2 - 1) | CLKGDV(8 - 1),
115         .pcr0  = FSXM | FSRM | CLKXM | CLKRM | FSXP | FSRP | CLKXP | CLKRP,
116         .xccr  = DXENDLY(1) | XDISABLE,
117         .rccr  = RFULL_CYCLE | RDISABLE,
118 };
119
120 /* to power down the DAC properly, it's not enough to just set the GPIO,
121  * the clocks must be running. What's worse, SCKI (256fs) is not enough,
122  * the other clocks must be running too, and for that McBSP must be started,
123  * so here are some horrible functions */
124 static int mcbsp2_clocking_start(void)
125 {
126         int ret;
127
128         ret = omap_mcbsp_request(1);
129         if (ret != 0) {
130                 pr_err("%s: request mcbsp2: %d\n", __func__, ret);
131                 return ret;
132         }
133         omap2_mcbsp_set_clks_src(1, MCBSP_CLKS_PAD_SRC);
134
135         cfg.xccr &= ~XDMAEN,
136         cfg.rccr &= ~RDMAEN,
137         omap_mcbsp_config(1, &cfg);
138
139         omap_mcbsp_start(1, 1, 0);
140
141         /* according to TRM "21.4.4.5 Underflow in the Transmitter",
142          * now it should output zeros on mcbspi_dx, so it's enough to sleep */
143         return 0;
144 }
145
146 static void mcbsp2_clocking_stop(void)
147 {
148         omap_mcbsp_stop(1, 1, 0);
149
150         cfg.xccr |= XDMAEN,
151         cfg.rccr |= RDMAEN,
152         omap_mcbsp_config(1, &cfg);
153
154         omap2_mcbsp_set_clks_src(1, MCBSP_CLKS_PRCM_SRC);
155         omap_mcbsp_free(1);
156 }
157
158 static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
159         struct snd_kcontrol *k, int event)
160 {
161         if (SND_SOC_DAPM_EVENT_ON(event)) {
162                 regulator_enable(omap3pandora_dac_reg);
163                 /* can only set /PD from low to high 1ms after
164                  * power and clock stabilize */
165                 msleep(1);
166                 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
167         } else {
168                 int ret = mcbsp2_clocking_start();
169
170                 /* let it sync to the clocks (just in case) */
171                 msleep(1);
172
173                 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
174
175                 /* 9334 (!) samples before power and clocks can be cut */
176                 msleep(9334 * 1000 / 8000 + 1);
177
178                 if (ret == 0)
179                         mcbsp2_clocking_stop();
180                 regulator_disable(omap3pandora_dac_reg);
181         }
182
183         return 0;
184 }
185
186 static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
187         struct snd_kcontrol *k, int event)
188 {
189         if (SND_SOC_DAPM_EVENT_ON(event)) {
190                 regulator_enable(omap3pandora_amp_reg);
191                 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
192         }
193         else {
194                 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
195                 /* wait for the large caps to discharge, otherwise the
196                  * "turn off noise" heard through the speakers takes longer */
197                 regulator_disable_deferred(omap3pandora_amp_reg, 1000);
198         }
199
200         return 0;
201 }
202
203 /*
204  * Audio paths on Pandora board:
205  *
206  *  |O| ---> PCM DAC +-> AMP -> Headphone Jack
207  *  |M|         A    +--------> Line Out
208  *  |A| <~~clk~~+
209  *  |P| <--- TWL4030 <--------- Line In and MICs
210  */
211 static const struct snd_soc_dapm_widget omap3pandora_out_dapm_widgets[] = {
212         SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
213                            0, 0, omap3pandora_dac_event,
214                            SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
215         SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
216                            0, 0, NULL, 0, omap3pandora_hp_event,
217                            SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
218         SND_SOC_DAPM_HP("Headphone Jack", NULL),
219         SND_SOC_DAPM_LINE("Line Out", NULL),
220 };
221
222 static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = {
223         SND_SOC_DAPM_MIC("Mic (internal)", NULL),
224         SND_SOC_DAPM_MIC("Mic (external)", NULL),
225         SND_SOC_DAPM_LINE("Line In", NULL),
226 };
227
228 static const struct snd_soc_dapm_route omap3pandora_out_map[] = {
229         {"PCM DAC", NULL, "APLL Enable"},
230         {"Headphone Amplifier", NULL, "PCM DAC"},
231         {"Line Out", NULL, "PCM DAC"},
232         {"Headphone Jack", NULL, "Headphone Amplifier"},
233 };
234
235 static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
236         {"AUXL", NULL, "Line In"},
237         {"AUXR", NULL, "Line In"},
238
239         {"MAINMIC", NULL, "Mic Bias 1"},
240         {"Mic Bias 1", NULL, "Mic (internal)"},
241
242         {"SUBMIC", NULL, "Mic Bias 2"},
243         {"Mic Bias 2", NULL, "Mic (external)"},
244 };
245
246 static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
247 {
248         struct snd_soc_codec *codec = rtd->codec;
249         struct snd_soc_dapm_context *dapm = &codec->dapm;
250         int ret;
251
252         /* All TWL4030 output pins are floating */
253         snd_soc_dapm_nc_pin(dapm, "EARPIECE");
254         snd_soc_dapm_nc_pin(dapm, "PREDRIVEL");
255         snd_soc_dapm_nc_pin(dapm, "PREDRIVER");
256         snd_soc_dapm_nc_pin(dapm, "HSOL");
257         snd_soc_dapm_nc_pin(dapm, "HSOR");
258         snd_soc_dapm_nc_pin(dapm, "CARKITL");
259         snd_soc_dapm_nc_pin(dapm, "CARKITR");
260         snd_soc_dapm_nc_pin(dapm, "HFL");
261         snd_soc_dapm_nc_pin(dapm, "HFR");
262         snd_soc_dapm_nc_pin(dapm, "VIBRA");
263
264         ret = snd_soc_dapm_new_controls(dapm, omap3pandora_out_dapm_widgets,
265                                 ARRAY_SIZE(omap3pandora_out_dapm_widgets));
266         if (ret < 0)
267                 return ret;
268
269         return snd_soc_dapm_add_routes(dapm, omap3pandora_out_map,
270                 ARRAY_SIZE(omap3pandora_out_map));
271 }
272
273 static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
274 {
275         struct snd_soc_codec *codec = rtd->codec;
276         struct snd_soc_dapm_context *dapm = &codec->dapm;
277         int ret;
278
279         /* Not comnnected */
280         snd_soc_dapm_nc_pin(dapm, "HSMIC");
281         snd_soc_dapm_nc_pin(dapm, "CARKITMIC");
282         snd_soc_dapm_nc_pin(dapm, "DIGIMIC0");
283         snd_soc_dapm_nc_pin(dapm, "DIGIMIC1");
284
285         ret = snd_soc_dapm_new_controls(dapm, omap3pandora_in_dapm_widgets,
286                                 ARRAY_SIZE(omap3pandora_in_dapm_widgets));
287         if (ret < 0)
288                 return ret;
289
290         return snd_soc_dapm_add_routes(dapm, omap3pandora_in_map,
291                 ARRAY_SIZE(omap3pandora_in_map));
292 }
293
294 static struct snd_soc_ops omap3pandora_ops = {
295         .hw_params = omap3pandora_hw_params,
296         .hw_free = omap3pandora_hw_free,
297 };
298
299 /* Digital audio interface glue - connects codec <--> CPU */
300 static struct snd_soc_dai_link omap3pandora_dai[] = {
301         {
302                 .name = "PCM1773",
303                 .stream_name = "HiFi Out",
304                 .cpu_dai_name = "omap-mcbsp-dai.1",
305                 .codec_dai_name = "twl4030-hifi",
306                 .platform_name = "omap-pcm-audio",
307                 .codec_name = "twl4030-codec",
308                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
309                            SND_SOC_DAIFMT_CBS_CFS,
310                 .ops = &omap3pandora_ops,
311                 .init = omap3pandora_out_init,
312         }, {
313                 .name = "TWL4030",
314                 .stream_name = "Line/Mic In",
315                 .cpu_dai_name = "omap-mcbsp-dai.3",
316                 .codec_dai_name = "twl4030-hifi",
317                 .platform_name = "omap-pcm-audio",
318                 .codec_name = "twl4030-codec",
319                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
320                            SND_SOC_DAIFMT_CBS_CFS,
321                 .ops = &omap3pandora_ops,
322                 .init = omap3pandora_in_init,
323         }
324 };
325
326 /* SoC card */
327 static struct snd_soc_card snd_soc_card_omap3pandora = {
328         .name = "omap3pandora",
329         .dai_link = omap3pandora_dai,
330         .num_links = ARRAY_SIZE(omap3pandora_dai),
331 };
332
333 static struct platform_device *omap3pandora_snd_device;
334
335 static int __init omap3pandora_soc_init(void)
336 {
337         int ret;
338
339         if (!machine_is_omap3_pandora())
340                 return -ENODEV;
341
342         pr_info("OMAP3 Pandora SoC init\n");
343
344         ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
345         if (ret) {
346                 pr_err(PREFIX "Failed to get DAC power GPIO\n");
347                 return ret;
348         }
349
350         ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
351         if (ret) {
352                 pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
353                 goto fail0;
354         }
355
356         ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
357         if (ret) {
358                 pr_err(PREFIX "Failed to get amp power GPIO\n");
359                 goto fail0;
360         }
361
362         ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
363         if (ret) {
364                 pr_err(PREFIX "Failed to set amp power GPIO direction\n");
365                 goto fail1;
366         }
367
368         omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
369         if (omap3pandora_snd_device == NULL) {
370                 pr_err(PREFIX "Platform device allocation failed\n");
371                 ret = -ENOMEM;
372                 goto fail1;
373         }
374
375         platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
376
377         ret = platform_device_add(omap3pandora_snd_device);
378         if (ret) {
379                 pr_err(PREFIX "Unable to add platform device\n");
380                 goto fail2;
381         }
382
383         omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
384         if (IS_ERR(omap3pandora_dac_reg)) {
385                 pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
386                         dev_name(&omap3pandora_snd_device->dev),
387                         PTR_ERR(omap3pandora_dac_reg));
388                 ret = PTR_ERR(omap3pandora_dac_reg);
389                 goto fail3;
390         }
391
392         omap3pandora_amp_reg = regulator_get(&omap3pandora_snd_device->dev, "vdd_amp");
393         if (IS_ERR(omap3pandora_amp_reg)) {
394                 pr_err(PREFIX "Failed to get amp regulator from %s: %ld\n",
395                         dev_name(&omap3pandora_snd_device->dev),
396                         PTR_ERR(omap3pandora_amp_reg));
397                 ret = PTR_ERR(omap3pandora_amp_reg);
398                 goto fail4;
399         }
400
401         return 0;
402
403 fail4:
404         regulator_put(omap3pandora_dac_reg);
405 fail3:
406         platform_device_del(omap3pandora_snd_device);
407 fail2:
408         platform_device_put(omap3pandora_snd_device);
409 fail1:
410         gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
411 fail0:
412         gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
413         return ret;
414 }
415 module_init(omap3pandora_soc_init);
416
417 static void __exit omap3pandora_soc_exit(void)
418 {
419         regulator_put(omap3pandora_dac_reg);
420         regulator_put(omap3pandora_amp_reg);
421         platform_device_unregister(omap3pandora_snd_device);
422         gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
423         gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
424 }
425 module_exit(omap3pandora_soc_exit);
426
427 MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
428 MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
429 MODULE_LICENSE("GPL");