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