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