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