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;
47 static int omap3pandora_hw_params(struct snd_pcm_substream *substream,
48 struct snd_pcm_hw_params *params)
50 struct snd_soc_pcm_runtime *rtd = substream->private_data;
51 struct snd_soc_dai *codec_dai = rtd->codec_dai;
52 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
56 /* Set the codec system clock for DAC and ADC */
57 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
60 pr_err(PREFIX "can't set codec system clock\n");
64 /* Set McBSP clock to external */
65 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
66 256 * params_rate(params),
69 pr_err(PREFIX "can't set cpu system clock\n");
74 if (snd_pcm_format_physical_width(params_format(params)) > 16)
77 ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, divider);
79 pr_err(PREFIX "can't set SRG clock divider to %d\n", divider);
86 static int omap3pandora_hw_free(struct snd_pcm_substream *substream)
88 struct snd_soc_pcm_runtime *rtd = substream->private_data;
89 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
92 /* Set McBSP clock back to internal for power saving to work */
93 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_FCLK,
96 pr_err(PREFIX "can't set cpu system clock\n");
103 static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
104 struct snd_kcontrol *k, int event)
107 * The PCM1773 DAC datasheet requires 1ms delay between switching
108 * VCC power on/off and /PD pin high/low
110 if (SND_SOC_DAPM_EVENT_ON(event)) {
111 regulator_enable(omap3pandora_dac_reg);
113 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
115 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
117 regulator_disable(omap3pandora_dac_reg);
123 static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
124 struct snd_kcontrol *k, int event)
126 if (SND_SOC_DAPM_EVENT_ON(event))
127 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
129 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
135 * Audio paths on Pandora board:
137 * |O| ---> PCM DAC +-> AMP -> Headphone Jack
138 * |M| A +--------> Line Out
140 * |P| <--- TWL4030 <--------- Line In and MICs
142 static const struct snd_soc_dapm_widget omap3pandora_out_dapm_widgets[] = {
143 SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
144 0, 0, omap3pandora_dac_event,
145 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
146 SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
147 0, 0, NULL, 0, omap3pandora_hp_event,
148 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
149 SND_SOC_DAPM_HP("Headphone Jack", NULL),
150 SND_SOC_DAPM_LINE("Line Out", NULL),
153 static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = {
154 SND_SOC_DAPM_MIC("Mic (internal)", NULL),
155 SND_SOC_DAPM_MIC("Mic (external)", NULL),
156 SND_SOC_DAPM_LINE("Line In", NULL),
159 static const struct snd_soc_dapm_route omap3pandora_out_map[] = {
160 {"PCM DAC", NULL, "APLL Enable"},
161 {"Headphone Amplifier", NULL, "PCM DAC"},
162 {"Line Out", NULL, "PCM DAC"},
163 {"Headphone Jack", NULL, "Headphone Amplifier"},
166 static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
167 {"AUXL", NULL, "Line In"},
168 {"AUXR", NULL, "Line In"},
170 {"MAINMIC", NULL, "Mic Bias 1"},
171 {"Mic Bias 1", NULL, "Mic (internal)"},
173 {"SUBMIC", NULL, "Mic Bias 2"},
174 {"Mic Bias 2", NULL, "Mic (external)"},
177 static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
179 struct snd_soc_codec *codec = rtd->codec;
180 struct snd_soc_dapm_context *dapm = &codec->dapm;
183 /* All TWL4030 output pins are floating */
184 snd_soc_dapm_nc_pin(dapm, "EARPIECE");
185 snd_soc_dapm_nc_pin(dapm, "PREDRIVEL");
186 snd_soc_dapm_nc_pin(dapm, "PREDRIVER");
187 snd_soc_dapm_nc_pin(dapm, "HSOL");
188 snd_soc_dapm_nc_pin(dapm, "HSOR");
189 snd_soc_dapm_nc_pin(dapm, "CARKITL");
190 snd_soc_dapm_nc_pin(dapm, "CARKITR");
191 snd_soc_dapm_nc_pin(dapm, "HFL");
192 snd_soc_dapm_nc_pin(dapm, "HFR");
193 snd_soc_dapm_nc_pin(dapm, "VIBRA");
195 ret = snd_soc_dapm_new_controls(dapm, omap3pandora_out_dapm_widgets,
196 ARRAY_SIZE(omap3pandora_out_dapm_widgets));
200 return snd_soc_dapm_add_routes(dapm, omap3pandora_out_map,
201 ARRAY_SIZE(omap3pandora_out_map));
204 static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
206 struct snd_soc_codec *codec = rtd->codec;
207 struct snd_soc_dapm_context *dapm = &codec->dapm;
211 snd_soc_dapm_nc_pin(dapm, "HSMIC");
212 snd_soc_dapm_nc_pin(dapm, "CARKITMIC");
213 snd_soc_dapm_nc_pin(dapm, "DIGIMIC0");
214 snd_soc_dapm_nc_pin(dapm, "DIGIMIC1");
216 ret = snd_soc_dapm_new_controls(dapm, omap3pandora_in_dapm_widgets,
217 ARRAY_SIZE(omap3pandora_in_dapm_widgets));
221 return snd_soc_dapm_add_routes(dapm, omap3pandora_in_map,
222 ARRAY_SIZE(omap3pandora_in_map));
225 static struct snd_soc_ops omap3pandora_ops = {
226 .hw_params = omap3pandora_hw_params,
227 .hw_free = omap3pandora_hw_free,
230 /* Digital audio interface glue - connects codec <--> CPU */
231 static struct snd_soc_dai_link omap3pandora_dai[] = {
234 .stream_name = "HiFi Out",
235 .cpu_dai_name = "omap-mcbsp-dai.1",
236 .codec_dai_name = "twl4030-hifi",
237 .platform_name = "omap-pcm-audio",
238 .codec_name = "twl4030-codec",
239 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
240 SND_SOC_DAIFMT_CBS_CFS,
241 .ops = &omap3pandora_ops,
242 .init = omap3pandora_out_init,
245 .stream_name = "Line/Mic In",
246 .cpu_dai_name = "omap-mcbsp-dai.3",
247 .codec_dai_name = "twl4030-hifi",
248 .platform_name = "omap-pcm-audio",
249 .codec_name = "twl4030-codec",
250 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
251 SND_SOC_DAIFMT_CBS_CFS,
252 .ops = &omap3pandora_ops,
253 .init = omap3pandora_in_init,
258 static struct snd_soc_card snd_soc_card_omap3pandora = {
259 .name = "omap3pandora",
260 .dai_link = omap3pandora_dai,
261 .num_links = ARRAY_SIZE(omap3pandora_dai),
264 static struct platform_device *omap3pandora_snd_device;
266 static int __init omap3pandora_soc_init(void)
270 if (!machine_is_omap3_pandora())
273 pr_info("OMAP3 Pandora SoC init\n");
275 ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
277 pr_err(PREFIX "Failed to get DAC power GPIO\n");
281 ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
283 pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
287 ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
289 pr_err(PREFIX "Failed to get amp power GPIO\n");
293 ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
295 pr_err(PREFIX "Failed to set amp power GPIO direction\n");
299 omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
300 if (omap3pandora_snd_device == NULL) {
301 pr_err(PREFIX "Platform device allocation failed\n");
306 platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
308 ret = platform_device_add(omap3pandora_snd_device);
310 pr_err(PREFIX "Unable to add platform device\n");
314 omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
315 if (IS_ERR(omap3pandora_dac_reg)) {
316 pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
317 dev_name(&omap3pandora_snd_device->dev),
318 PTR_ERR(omap3pandora_dac_reg));
319 ret = PTR_ERR(omap3pandora_dac_reg);
326 platform_device_del(omap3pandora_snd_device);
328 platform_device_put(omap3pandora_snd_device);
330 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
332 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
335 module_init(omap3pandora_soc_init);
337 static void __exit omap3pandora_soc_exit(void)
339 regulator_put(omap3pandora_dac_reg);
340 platform_device_unregister(omap3pandora_snd_device);
341 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
342 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
344 module_exit(omap3pandora_soc_exit);
346 MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
347 MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
348 MODULE_LICENSE("GPL");