2 * omap3pandora.c -- SoC audio for Pandora Handheld Console
4 * Author: GraÅžvydas Ignotas <notasas@gmail.com>
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.
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.
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
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>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/soc.h>
32 #include <sound/pcm_params.h>
34 #include <asm/mach-types.h>
35 #include <plat/mcbsp.h>
37 #include "omap-mcbsp.h"
40 #define OMAP3_PANDORA_DAC_POWER_GPIO 118
41 #define OMAP3_PANDORA_AMP_POWER_GPIO 14
43 #define PREFIX "ASoC omap3pandora: "
45 static struct regulator *omap3pandora_dac_reg;
46 static struct regulator *omap3pandora_amp_reg;
48 static int omap3pandora_hw_params(struct snd_pcm_substream *substream,
49 struct snd_pcm_hw_params *params)
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;
57 /* Set the codec system clock for DAC and ADC */
58 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
61 pr_err(PREFIX "can't set codec system clock\n");
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),
70 pr_err(PREFIX "can't set cpu system clock\n");
75 if (snd_pcm_format_physical_width(params_format(params)) > 16)
78 ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, divider);
80 pr_err(PREFIX "can't set SRG clock divider to %d\n", divider);
87 static int omap3pandora_hw_free(struct snd_pcm_substream *substream)
89 struct snd_soc_pcm_runtime *rtd = substream->private_data;
90 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
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,
97 pr_err(PREFIX "can't set cpu system clock\n");
104 #include <plat/mcbsp.h>
106 static struct omap_mcbsp_reg_cfg cfg = {
107 .spcr2 = XINTM(3) | FREE,
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,
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)
128 ret = omap_mcbsp_request(1);
130 pr_err("%s: request mcbsp2: %d\n", __func__, ret);
133 omap2_mcbsp_set_clks_src(1, MCBSP_CLKS_PAD_SRC);
137 omap_mcbsp_config(1, &cfg);
139 omap_mcbsp_start(1, 1, 0);
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 */
146 static void mcbsp2_clocking_stop(void)
148 omap_mcbsp_stop(1, 1, 0);
152 omap_mcbsp_config(1, &cfg);
154 omap2_mcbsp_set_clks_src(1, MCBSP_CLKS_PRCM_SRC);
158 static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
159 struct snd_kcontrol *k, int event)
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 */
166 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
168 int ret = mcbsp2_clocking_start();
170 /* let it sync to the clocks (just in case) */
173 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
175 /* 9334 (!) samples before power and clocks can be cut */
176 msleep(9334 * 1000 / 8000 + 1);
179 mcbsp2_clocking_stop();
180 regulator_disable(omap3pandora_dac_reg);
186 static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
187 struct snd_kcontrol *k, int event)
189 if (SND_SOC_DAPM_EVENT_ON(event)) {
190 regulator_enable(omap3pandora_amp_reg);
191 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
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);
204 * Audio paths on Pandora board:
206 * |O| ---> PCM DAC +-> AMP -> Headphone Jack
207 * |M| A +--------> Line Out
209 * |P| <--- TWL4030 <--------- Line In and MICs
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),
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),
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"},
235 static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
236 {"AUXL", NULL, "Line In"},
237 {"AUXR", NULL, "Line In"},
239 {"MAINMIC", NULL, "Mic Bias 1"},
240 {"Mic Bias 1", NULL, "Mic (internal)"},
242 {"SUBMIC", NULL, "Mic Bias 2"},
243 {"Mic Bias 2", NULL, "Mic (external)"},
246 static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
248 struct snd_soc_codec *codec = rtd->codec;
249 struct snd_soc_dapm_context *dapm = &codec->dapm;
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");
264 ret = snd_soc_dapm_new_controls(dapm, omap3pandora_out_dapm_widgets,
265 ARRAY_SIZE(omap3pandora_out_dapm_widgets));
269 return snd_soc_dapm_add_routes(dapm, omap3pandora_out_map,
270 ARRAY_SIZE(omap3pandora_out_map));
273 static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
275 struct snd_soc_codec *codec = rtd->codec;
276 struct snd_soc_dapm_context *dapm = &codec->dapm;
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");
285 ret = snd_soc_dapm_new_controls(dapm, omap3pandora_in_dapm_widgets,
286 ARRAY_SIZE(omap3pandora_in_dapm_widgets));
290 return snd_soc_dapm_add_routes(dapm, omap3pandora_in_map,
291 ARRAY_SIZE(omap3pandora_in_map));
294 static struct snd_soc_ops omap3pandora_ops = {
295 .hw_params = omap3pandora_hw_params,
296 .hw_free = omap3pandora_hw_free,
299 /* Digital audio interface glue - connects codec <--> CPU */
300 static struct snd_soc_dai_link omap3pandora_dai[] = {
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,
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,
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),
333 static struct platform_device *omap3pandora_snd_device;
335 static int __init omap3pandora_soc_init(void)
339 if (!machine_is_omap3_pandora())
342 pr_info("OMAP3 Pandora SoC init\n");
344 ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
346 pr_err(PREFIX "Failed to get DAC power GPIO\n");
350 ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
352 pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
356 ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
358 pr_err(PREFIX "Failed to get amp power GPIO\n");
362 ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
364 pr_err(PREFIX "Failed to set amp power GPIO direction\n");
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");
375 platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
377 ret = platform_device_add(omap3pandora_snd_device);
379 pr_err(PREFIX "Unable to add platform device\n");
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);
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);
404 regulator_put(omap3pandora_dac_reg);
406 platform_device_del(omap3pandora_snd_device);
408 platform_device_put(omap3pandora_snd_device);
410 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
412 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
415 module_init(omap3pandora_soc_init);
417 static void __exit omap3pandora_soc_exit(void)
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);
425 module_exit(omap3pandora_soc_exit);
427 MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
428 MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
429 MODULE_LICENSE("GPL");