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>
33 #include <asm/mach-types.h>
34 #include <linux/platform_data/asoc-ti-mcbsp.h>
36 #include "omap-mcbsp.h"
39 #define OMAP3_PANDORA_DAC_POWER_GPIO 118
40 #define OMAP3_PANDORA_AMP_POWER_GPIO 14
42 #define PREFIX "ASoC omap3pandora: "
44 static struct regulator *omap3pandora_dac_reg;
46 static int omap3pandora_hw_params(struct snd_pcm_substream *substream,
47 struct snd_pcm_hw_params *params)
49 struct snd_soc_pcm_runtime *rtd = substream->private_data;
50 struct snd_soc_dai *codec_dai = rtd->codec_dai;
51 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
54 /* Set the codec system clock for DAC and ADC */
55 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
58 pr_err(PREFIX "can't set codec system clock\n");
62 /* Set McBSP clock to external */
63 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
64 256 * params_rate(params),
67 pr_err(PREFIX "can't set cpu system clock\n");
71 ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8);
73 pr_err(PREFIX "can't set SRG clock divider\n");
80 static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
81 struct snd_kcontrol *k, int event)
86 * The PCM1773 DAC datasheet requires 1ms delay between switching
87 * VCC power on/off and /PD pin high/low
89 if (SND_SOC_DAPM_EVENT_ON(event)) {
90 ret = regulator_enable(omap3pandora_dac_reg);
92 dev_err(w->dapm.dev, "Failed to power DAC: %d\n", ret);
96 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
98 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
100 regulator_disable(omap3pandora_dac_reg);
106 static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
107 struct snd_kcontrol *k, int event)
109 if (SND_SOC_DAPM_EVENT_ON(event))
110 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
112 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
118 * Audio paths on Pandora board:
120 * |O| ---> PCM DAC +-> AMP -> Headphone Jack
121 * |M| A +--------> Line Out
123 * |P| <--- TWL4030 <--------- Line In and MICs
125 static const struct snd_soc_dapm_widget omap3pandora_out_dapm_widgets[] = {
126 SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
127 0, 0, omap3pandora_dac_event,
128 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
129 SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
130 0, 0, NULL, 0, omap3pandora_hp_event,
131 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
132 SND_SOC_DAPM_HP("Headphone Jack", NULL),
133 SND_SOC_DAPM_LINE("Line Out", NULL),
136 static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = {
137 SND_SOC_DAPM_MIC("Mic (internal)", NULL),
138 SND_SOC_DAPM_MIC("Mic (external)", NULL),
139 SND_SOC_DAPM_LINE("Line In", NULL),
142 static const struct snd_soc_dapm_route omap3pandora_out_map[] = {
143 {"PCM DAC", NULL, "APLL Enable"},
144 {"Headphone Amplifier", NULL, "PCM DAC"},
145 {"Line Out", NULL, "PCM DAC"},
146 {"Headphone Jack", NULL, "Headphone Amplifier"},
149 static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
150 {"AUXL", NULL, "Line In"},
151 {"AUXR", NULL, "Line In"},
153 {"MAINMIC", NULL, "Mic (internal)"},
154 {"Mic (internal)", NULL, "Mic Bias 1"},
156 {"SUBMIC", NULL, "Mic (external)"},
157 {"Mic (external)", NULL, "Mic Bias 2"},
160 static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
162 struct snd_soc_codec *codec = rtd->codec;
163 struct snd_soc_dapm_context *dapm = &codec->dapm;
166 /* All TWL4030 output pins are floating */
167 snd_soc_dapm_nc_pin(dapm, "EARPIECE");
168 snd_soc_dapm_nc_pin(dapm, "PREDRIVEL");
169 snd_soc_dapm_nc_pin(dapm, "PREDRIVER");
170 snd_soc_dapm_nc_pin(dapm, "HSOL");
171 snd_soc_dapm_nc_pin(dapm, "HSOR");
172 snd_soc_dapm_nc_pin(dapm, "CARKITL");
173 snd_soc_dapm_nc_pin(dapm, "CARKITR");
174 snd_soc_dapm_nc_pin(dapm, "HFL");
175 snd_soc_dapm_nc_pin(dapm, "HFR");
176 snd_soc_dapm_nc_pin(dapm, "VIBRA");
178 ret = snd_soc_dapm_new_controls(dapm, omap3pandora_out_dapm_widgets,
179 ARRAY_SIZE(omap3pandora_out_dapm_widgets));
183 return snd_soc_dapm_add_routes(dapm, omap3pandora_out_map,
184 ARRAY_SIZE(omap3pandora_out_map));
187 static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
189 struct snd_soc_codec *codec = rtd->codec;
190 struct snd_soc_dapm_context *dapm = &codec->dapm;
194 snd_soc_dapm_nc_pin(dapm, "HSMIC");
195 snd_soc_dapm_nc_pin(dapm, "CARKITMIC");
196 snd_soc_dapm_nc_pin(dapm, "DIGIMIC0");
197 snd_soc_dapm_nc_pin(dapm, "DIGIMIC1");
199 ret = snd_soc_dapm_new_controls(dapm, omap3pandora_in_dapm_widgets,
200 ARRAY_SIZE(omap3pandora_in_dapm_widgets));
204 return snd_soc_dapm_add_routes(dapm, omap3pandora_in_map,
205 ARRAY_SIZE(omap3pandora_in_map));
208 static struct snd_soc_ops omap3pandora_ops = {
209 .hw_params = omap3pandora_hw_params,
212 /* Digital audio interface glue - connects codec <--> CPU */
213 static struct snd_soc_dai_link omap3pandora_dai[] = {
216 .stream_name = "HiFi Out",
217 .cpu_dai_name = "omap-mcbsp.2",
218 .codec_dai_name = "twl4030-hifi",
219 .platform_name = "omap-pcm-audio",
220 .codec_name = "twl4030-codec",
221 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
222 SND_SOC_DAIFMT_CBS_CFS,
223 .ops = &omap3pandora_ops,
224 .init = omap3pandora_out_init,
227 .stream_name = "Line/Mic In",
228 .cpu_dai_name = "omap-mcbsp.4",
229 .codec_dai_name = "twl4030-hifi",
230 .platform_name = "omap-pcm-audio",
231 .codec_name = "twl4030-codec",
232 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
233 SND_SOC_DAIFMT_CBS_CFS,
234 .ops = &omap3pandora_ops,
235 .init = omap3pandora_in_init,
240 static struct snd_soc_card snd_soc_card_omap3pandora = {
241 .name = "omap3pandora",
242 .owner = THIS_MODULE,
243 .dai_link = omap3pandora_dai,
244 .num_links = ARRAY_SIZE(omap3pandora_dai),
247 static struct platform_device *omap3pandora_snd_device;
249 static int __init omap3pandora_soc_init(void)
253 if (!machine_is_omap3_pandora())
256 pr_info("OMAP3 Pandora SoC init\n");
258 ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
260 pr_err(PREFIX "Failed to get DAC power GPIO\n");
264 ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
266 pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
270 ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
272 pr_err(PREFIX "Failed to get amp power GPIO\n");
276 ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
278 pr_err(PREFIX "Failed to set amp power GPIO direction\n");
282 omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
283 if (omap3pandora_snd_device == NULL) {
284 pr_err(PREFIX "Platform device allocation failed\n");
289 platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
291 ret = platform_device_add(omap3pandora_snd_device);
293 pr_err(PREFIX "Unable to add platform device\n");
297 omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
298 if (IS_ERR(omap3pandora_dac_reg)) {
299 pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
300 dev_name(&omap3pandora_snd_device->dev),
301 PTR_ERR(omap3pandora_dac_reg));
302 ret = PTR_ERR(omap3pandora_dac_reg);
309 platform_device_del(omap3pandora_snd_device);
311 platform_device_put(omap3pandora_snd_device);
313 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
315 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
318 module_init(omap3pandora_soc_init);
320 static void __exit omap3pandora_soc_exit(void)
322 regulator_put(omap3pandora_dac_reg);
323 platform_device_unregister(omap3pandora_snd_device);
324 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
325 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
327 module_exit(omap3pandora_soc_exit);
329 MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
330 MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
331 MODULE_LICENSE("GPL");