Merge branch 'topic/asoc' into for-linus
[linux-2.6.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, "EARPIECE");
192         snd_soc_dapm_nc_pin(codec, "PREDRIVEL");
193         snd_soc_dapm_nc_pin(codec, "PREDRIVER");
194         snd_soc_dapm_nc_pin(codec, "HSOL");
195         snd_soc_dapm_nc_pin(codec, "HSOR");
196         snd_soc_dapm_nc_pin(codec, "CARKITL");
197         snd_soc_dapm_nc_pin(codec, "CARKITR");
198         snd_soc_dapm_nc_pin(codec, "HFL");
199         snd_soc_dapm_nc_pin(codec, "HFR");
200         snd_soc_dapm_nc_pin(codec, "VIBRA");
201
202         ret = snd_soc_dapm_new_controls(codec, omap3pandora_out_dapm_widgets,
203                                 ARRAY_SIZE(omap3pandora_out_dapm_widgets));
204         if (ret < 0)
205                 return ret;
206
207         snd_soc_dapm_add_routes(codec, omap3pandora_out_map,
208                 ARRAY_SIZE(omap3pandora_out_map));
209
210         return snd_soc_dapm_sync(codec);
211 }
212
213 static int omap3pandora_in_init(struct snd_soc_codec *codec)
214 {
215         int ret;
216
217         /* Not comnnected */
218         snd_soc_dapm_nc_pin(codec, "HSMIC");
219         snd_soc_dapm_nc_pin(codec, "CARKITMIC");
220         snd_soc_dapm_nc_pin(codec, "DIGIMIC0");
221         snd_soc_dapm_nc_pin(codec, "DIGIMIC1");
222
223         ret = snd_soc_dapm_new_controls(codec, omap3pandora_in_dapm_widgets,
224                                 ARRAY_SIZE(omap3pandora_in_dapm_widgets));
225         if (ret < 0)
226                 return ret;
227
228         snd_soc_dapm_add_routes(codec, omap3pandora_in_map,
229                 ARRAY_SIZE(omap3pandora_in_map));
230
231         return snd_soc_dapm_sync(codec);
232 }
233
234 static struct snd_soc_ops omap3pandora_out_ops = {
235         .hw_params = omap3pandora_out_hw_params,
236 };
237
238 static struct snd_soc_ops omap3pandora_in_ops = {
239         .hw_params = omap3pandora_in_hw_params,
240 };
241
242 /* Digital audio interface glue - connects codec <--> CPU */
243 static struct snd_soc_dai_link omap3pandora_dai[] = {
244         {
245                 .name = "PCM1773",
246                 .stream_name = "HiFi Out",
247                 .cpu_dai = &omap_mcbsp_dai[0],
248                 .codec_dai = &twl4030_dai[TWL4030_DAI_HIFI],
249                 .ops = &omap3pandora_out_ops,
250                 .init = omap3pandora_out_init,
251         }, {
252                 .name = "TWL4030",
253                 .stream_name = "Line/Mic In",
254                 .cpu_dai = &omap_mcbsp_dai[1],
255                 .codec_dai = &twl4030_dai[TWL4030_DAI_HIFI],
256                 .ops = &omap3pandora_in_ops,
257                 .init = omap3pandora_in_init,
258         }
259 };
260
261 /* SoC card */
262 static struct snd_soc_card snd_soc_card_omap3pandora = {
263         .name = "omap3pandora",
264         .platform = &omap_soc_platform,
265         .dai_link = omap3pandora_dai,
266         .num_links = ARRAY_SIZE(omap3pandora_dai),
267 };
268
269 /* Audio subsystem */
270 static struct snd_soc_device omap3pandora_snd_data = {
271         .card = &snd_soc_card_omap3pandora,
272         .codec_dev = &soc_codec_dev_twl4030,
273 };
274
275 static struct platform_device *omap3pandora_snd_device;
276
277 static int __init omap3pandora_soc_init(void)
278 {
279         int ret;
280
281         if (!machine_is_omap3_pandora())
282                 return -ENODEV;
283
284         pr_info("OMAP3 Pandora SoC init\n");
285
286         ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
287         if (ret) {
288                 pr_err(PREFIX "Failed to get DAC power GPIO\n");
289                 return ret;
290         }
291
292         ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
293         if (ret) {
294                 pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
295                 goto fail0;
296         }
297
298         ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
299         if (ret) {
300                 pr_err(PREFIX "Failed to get amp power GPIO\n");
301                 goto fail0;
302         }
303
304         ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
305         if (ret) {
306                 pr_err(PREFIX "Failed to set amp power GPIO direction\n");
307                 goto fail1;
308         }
309
310         omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
311         if (omap3pandora_snd_device == NULL) {
312                 pr_err(PREFIX "Platform device allocation failed\n");
313                 ret = -ENOMEM;
314                 goto fail1;
315         }
316
317         platform_set_drvdata(omap3pandora_snd_device, &omap3pandora_snd_data);
318         omap3pandora_snd_data.dev = &omap3pandora_snd_device->dev;
319         *(unsigned int *)omap_mcbsp_dai[0].private_data = 1; /* McBSP2 */
320         *(unsigned int *)omap_mcbsp_dai[1].private_data = 3; /* McBSP4 */
321
322         ret = platform_device_add(omap3pandora_snd_device);
323         if (ret) {
324                 pr_err(PREFIX "Unable to add platform device\n");
325                 goto fail2;
326         }
327
328         omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
329         if (IS_ERR(omap3pandora_dac_reg)) {
330                 pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
331                         dev_name(&omap3pandora_snd_device->dev),
332                         PTR_ERR(omap3pandora_dac_reg));
333                 goto fail3;
334         }
335
336         return 0;
337
338 fail3:
339         platform_device_del(omap3pandora_snd_device);
340 fail2:
341         platform_device_put(omap3pandora_snd_device);
342 fail1:
343         gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
344 fail0:
345         gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
346         return ret;
347 }
348 module_init(omap3pandora_soc_init);
349
350 static void __exit omap3pandora_soc_exit(void)
351 {
352         regulator_put(omap3pandora_dac_reg);
353         platform_device_unregister(omap3pandora_snd_device);
354         gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
355         gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
356 }
357 module_exit(omap3pandora_soc_exit);
358
359 MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
360 MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
361 MODULE_LICENSE("GPL");