blob: 93e83c0f66604a37efce3c0685fc19a520e8fa03 [file] [log] [blame]
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +02001/*
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>
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +020026#include <linux/regulator/consumer.h>
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020027
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>
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000034#include <plat/mcbsp.h>
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020035
36#include "omap-mcbsp.h"
37#include "omap-pcm.h"
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020038
39#define OMAP3_PANDORA_DAC_POWER_GPIO 118
40#define OMAP3_PANDORA_AMP_POWER_GPIO 14
41
42#define PREFIX "ASoC omap3pandora: "
43
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +020044static struct regulator *omap3pandora_dac_reg;
45
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +030046static int omap3pandora_hw_params(struct snd_pcm_substream *substream,
47 struct snd_pcm_hw_params *params)
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020048{
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020049 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000050 struct snd_soc_dai *codec_dai = rtd->codec_dai;
51 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +030052 int fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
53 SND_SOC_DAIFMT_CBS_CFS;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020054 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 */
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020079 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
80 256 * params_rate(params),
81 SND_SOC_CLOCK_IN);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020082 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
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +020096static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
97 struct snd_kcontrol *k, int event)
98{
99 /*
100 * The PCM1773 DAC datasheet requires 1ms delay between switching
101 * VCC power on/off and /PD pin high/low
102 */
103 if (SND_SOC_DAPM_EVENT_ON(event)) {
104 regulator_enable(omap3pandora_dac_reg);
105 mdelay(1);
106 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
107 } else {
108 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
109 mdelay(1);
110 regulator_disable(omap3pandora_dac_reg);
111 }
112
113 return 0;
114}
115
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200116static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
117 struct snd_kcontrol *k, int event)
118{
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200119 if (SND_SOC_DAPM_EVENT_ON(event))
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200120 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200121 else
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200122 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200123
124 return 0;
125}
126
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[] = {
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200136 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),
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200139 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[] = {
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200147 SND_SOC_DAPM_MIC("Mic (internal)", NULL),
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200148 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[] = {
Grazvydas Ignotas3b9447f2010-02-05 00:55:33 +0200153 {"PCM DAC", NULL, "APLL Enable"},
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200154 {"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[] = {
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200160 {"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)"},
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200168};
169
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000170static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200171{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000172 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200173 struct snd_soc_dapm_context *dapm = &codec->dapm;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200174 int ret;
175
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200176 /* All TWL4030 output pins are floating */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200177 snd_soc_dapm_nc_pin(dapm, "EARPIECE");
178 snd_soc_dapm_nc_pin(dapm, "PREDRIVEL");
179 snd_soc_dapm_nc_pin(dapm, "PREDRIVER");
180 snd_soc_dapm_nc_pin(dapm, "HSOL");
181 snd_soc_dapm_nc_pin(dapm, "HSOR");
182 snd_soc_dapm_nc_pin(dapm, "CARKITL");
183 snd_soc_dapm_nc_pin(dapm, "CARKITR");
184 snd_soc_dapm_nc_pin(dapm, "HFL");
185 snd_soc_dapm_nc_pin(dapm, "HFR");
186 snd_soc_dapm_nc_pin(dapm, "VIBRA");
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200187
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200188 ret = snd_soc_dapm_new_controls(dapm, omap3pandora_out_dapm_widgets,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200189 ARRAY_SIZE(omap3pandora_out_dapm_widgets));
190 if (ret < 0)
191 return ret;
192
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200193 snd_soc_dapm_add_routes(dapm, omap3pandora_out_map,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200194 ARRAY_SIZE(omap3pandora_out_map));
195
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200196 return snd_soc_dapm_sync(dapm);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200197}
198
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000199static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200200{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000201 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200202 struct snd_soc_dapm_context *dapm = &codec->dapm;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200203 int ret;
204
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200205 /* Not comnnected */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200206 snd_soc_dapm_nc_pin(dapm, "HSMIC");
207 snd_soc_dapm_nc_pin(dapm, "CARKITMIC");
208 snd_soc_dapm_nc_pin(dapm, "DIGIMIC0");
209 snd_soc_dapm_nc_pin(dapm, "DIGIMIC1");
Grazvydas Ignotas7f185342008-12-23 12:04:48 +0200210
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200211 ret = snd_soc_dapm_new_controls(dapm, omap3pandora_in_dapm_widgets,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200212 ARRAY_SIZE(omap3pandora_in_dapm_widgets));
213 if (ret < 0)
214 return ret;
215
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200216 snd_soc_dapm_add_routes(dapm, omap3pandora_in_map,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200217 ARRAY_SIZE(omap3pandora_in_map));
218
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200219 return snd_soc_dapm_sync(dapm);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200220}
221
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +0300222static struct snd_soc_ops omap3pandora_ops = {
223 .hw_params = omap3pandora_hw_params,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200224};
225
226/* Digital audio interface glue - connects codec <--> CPU */
227static struct snd_soc_dai_link omap3pandora_dai[] = {
228 {
229 .name = "PCM1773",
230 .stream_name = "HiFi Out",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000231 .cpu_dai_name = "omap-mcbsp-dai.1",
232 .codec_dai_name = "twl4030-hifi",
233 .platform_name = "omap-pcm-audio",
234 .codec_name = "twl4030-codec",
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +0300235 .ops = &omap3pandora_ops,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200236 .init = omap3pandora_out_init,
237 }, {
238 .name = "TWL4030",
239 .stream_name = "Line/Mic In",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000240 .cpu_dai_name = "omap-mcbsp-dai.3",
241 .codec_dai_name = "twl4030-hifi",
242 .platform_name = "omap-pcm-audio",
243 .codec_name = "twl4030-codec",
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +0300244 .ops = &omap3pandora_ops,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200245 .init = omap3pandora_in_init,
246 }
247};
248
249/* SoC card */
250static struct snd_soc_card snd_soc_card_omap3pandora = {
251 .name = "omap3pandora",
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200252 .dai_link = omap3pandora_dai,
253 .num_links = ARRAY_SIZE(omap3pandora_dai),
254};
255
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200256static struct platform_device *omap3pandora_snd_device;
257
258static int __init omap3pandora_soc_init(void)
259{
260 int ret;
261
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200262 if (!machine_is_omap3_pandora())
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200263 return -ENODEV;
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200264
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200265 pr_info("OMAP3 Pandora SoC init\n");
266
267 ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
268 if (ret) {
269 pr_err(PREFIX "Failed to get DAC power GPIO\n");
270 return ret;
271 }
272
273 ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
274 if (ret) {
275 pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
276 goto fail0;
277 }
278
279 ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
280 if (ret) {
281 pr_err(PREFIX "Failed to get amp power GPIO\n");
282 goto fail0;
283 }
284
285 ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
286 if (ret) {
287 pr_err(PREFIX "Failed to set amp power GPIO direction\n");
288 goto fail1;
289 }
290
291 omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
292 if (omap3pandora_snd_device == NULL) {
293 pr_err(PREFIX "Platform device allocation failed\n");
294 ret = -ENOMEM;
295 goto fail1;
296 }
297
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000298 platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200299
300 ret = platform_device_add(omap3pandora_snd_device);
301 if (ret) {
302 pr_err(PREFIX "Unable to add platform device\n");
303 goto fail2;
304 }
305
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200306 omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
307 if (IS_ERR(omap3pandora_dac_reg)) {
308 pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
309 dev_name(&omap3pandora_snd_device->dev),
310 PTR_ERR(omap3pandora_dac_reg));
311 goto fail3;
312 }
313
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200314 return 0;
315
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200316fail3:
317 platform_device_del(omap3pandora_snd_device);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200318fail2:
319 platform_device_put(omap3pandora_snd_device);
320fail1:
321 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
322fail0:
323 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
324 return ret;
325}
326module_init(omap3pandora_soc_init);
327
328static void __exit omap3pandora_soc_exit(void)
329{
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200330 regulator_put(omap3pandora_dac_reg);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200331 platform_device_unregister(omap3pandora_snd_device);
332 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
333 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
334}
335module_exit(omap3pandora_soc_exit);
336
337MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
338MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
339MODULE_LICENSE("GPL");