blob: 3ae87fd36612fdc2aefc0363538c830f0505a545 [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>
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020031
32#include <asm/mach-types.h>
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000033#include <plat/mcbsp.h>
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020034
35#include "omap-mcbsp.h"
36#include "omap-pcm.h"
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020037
38#define OMAP3_PANDORA_DAC_POWER_GPIO 118
39#define OMAP3_PANDORA_AMP_POWER_GPIO 14
40
41#define PREFIX "ASoC omap3pandora: "
42
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +020043static struct regulator *omap3pandora_dac_reg;
44
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +030045static int omap3pandora_hw_params(struct snd_pcm_substream *substream,
46 struct snd_pcm_hw_params *params)
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020047{
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020048 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000049 struct snd_soc_dai *codec_dai = rtd->codec_dai;
50 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020051 int ret;
52
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020053 /* Set the codec system clock for DAC and ADC */
54 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
55 SND_SOC_CLOCK_IN);
56 if (ret < 0) {
57 pr_err(PREFIX "can't set codec system clock\n");
58 return ret;
59 }
60
61 /* Set McBSP clock to external */
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020062 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
63 256 * params_rate(params),
64 SND_SOC_CLOCK_IN);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020065 if (ret < 0) {
66 pr_err(PREFIX "can't set cpu system clock\n");
67 return ret;
68 }
69
70 ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8);
71 if (ret < 0) {
72 pr_err(PREFIX "can't set SRG clock divider\n");
73 return ret;
74 }
75
76 return 0;
77}
78
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +020079static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
80 struct snd_kcontrol *k, int event)
81{
82 /*
83 * The PCM1773 DAC datasheet requires 1ms delay between switching
84 * VCC power on/off and /PD pin high/low
85 */
86 if (SND_SOC_DAPM_EVENT_ON(event)) {
87 regulator_enable(omap3pandora_dac_reg);
88 mdelay(1);
89 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
90 } else {
91 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
92 mdelay(1);
93 regulator_disable(omap3pandora_dac_reg);
94 }
95
96 return 0;
97}
98
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020099static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
100 struct snd_kcontrol *k, int event)
101{
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200102 if (SND_SOC_DAPM_EVENT_ON(event))
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200103 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200104 else
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200105 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200106
107 return 0;
108}
109
110/*
111 * Audio paths on Pandora board:
112 *
113 * |O| ---> PCM DAC +-> AMP -> Headphone Jack
114 * |M| A +--------> Line Out
115 * |A| <~~clk~~+
116 * |P| <--- TWL4030 <--------- Line In and MICs
117 */
118static const struct snd_soc_dapm_widget omap3pandora_out_dapm_widgets[] = {
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200119 SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
120 0, 0, omap3pandora_dac_event,
121 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200122 SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
123 0, 0, NULL, 0, omap3pandora_hp_event,
124 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
125 SND_SOC_DAPM_HP("Headphone Jack", NULL),
126 SND_SOC_DAPM_LINE("Line Out", NULL),
127};
128
129static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = {
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200130 SND_SOC_DAPM_MIC("Mic (internal)", NULL),
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200131 SND_SOC_DAPM_MIC("Mic (external)", NULL),
132 SND_SOC_DAPM_LINE("Line In", NULL),
133};
134
135static const struct snd_soc_dapm_route omap3pandora_out_map[] = {
Grazvydas Ignotas3b9447f2010-02-05 00:55:33 +0200136 {"PCM DAC", NULL, "APLL Enable"},
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200137 {"Headphone Amplifier", NULL, "PCM DAC"},
138 {"Line Out", NULL, "PCM DAC"},
139 {"Headphone Jack", NULL, "Headphone Amplifier"},
140};
141
142static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200143 {"AUXL", NULL, "Line In"},
144 {"AUXR", NULL, "Line In"},
145
146 {"MAINMIC", NULL, "Mic Bias 1"},
147 {"Mic Bias 1", NULL, "Mic (internal)"},
148
149 {"SUBMIC", NULL, "Mic Bias 2"},
150 {"Mic Bias 2", NULL, "Mic (external)"},
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200151};
152
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000153static int omap3pandora_out_init(struct snd_soc_pcm_runtime *rtd)
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200154{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000155 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200156 struct snd_soc_dapm_context *dapm = &codec->dapm;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200157 int ret;
158
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200159 /* All TWL4030 output pins are floating */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200160 snd_soc_dapm_nc_pin(dapm, "EARPIECE");
161 snd_soc_dapm_nc_pin(dapm, "PREDRIVEL");
162 snd_soc_dapm_nc_pin(dapm, "PREDRIVER");
163 snd_soc_dapm_nc_pin(dapm, "HSOL");
164 snd_soc_dapm_nc_pin(dapm, "HSOR");
165 snd_soc_dapm_nc_pin(dapm, "CARKITL");
166 snd_soc_dapm_nc_pin(dapm, "CARKITR");
167 snd_soc_dapm_nc_pin(dapm, "HFL");
168 snd_soc_dapm_nc_pin(dapm, "HFR");
169 snd_soc_dapm_nc_pin(dapm, "VIBRA");
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200170
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200171 ret = snd_soc_dapm_new_controls(dapm, omap3pandora_out_dapm_widgets,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200172 ARRAY_SIZE(omap3pandora_out_dapm_widgets));
173 if (ret < 0)
174 return ret;
175
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200176 snd_soc_dapm_add_routes(dapm, omap3pandora_out_map,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200177 ARRAY_SIZE(omap3pandora_out_map));
178
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200179 return snd_soc_dapm_sync(dapm);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200180}
181
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000182static int omap3pandora_in_init(struct snd_soc_pcm_runtime *rtd)
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200183{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200185 struct snd_soc_dapm_context *dapm = &codec->dapm;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200186 int ret;
187
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200188 /* Not comnnected */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200189 snd_soc_dapm_nc_pin(dapm, "HSMIC");
190 snd_soc_dapm_nc_pin(dapm, "CARKITMIC");
191 snd_soc_dapm_nc_pin(dapm, "DIGIMIC0");
192 snd_soc_dapm_nc_pin(dapm, "DIGIMIC1");
Grazvydas Ignotas7f185342008-12-23 12:04:48 +0200193
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200194 ret = snd_soc_dapm_new_controls(dapm, omap3pandora_in_dapm_widgets,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200195 ARRAY_SIZE(omap3pandora_in_dapm_widgets));
196 if (ret < 0)
197 return ret;
198
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200199 snd_soc_dapm_add_routes(dapm, omap3pandora_in_map,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200200 ARRAY_SIZE(omap3pandora_in_map));
201
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200202 return snd_soc_dapm_sync(dapm);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200203}
204
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +0300205static struct snd_soc_ops omap3pandora_ops = {
206 .hw_params = omap3pandora_hw_params,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200207};
208
209/* Digital audio interface glue - connects codec <--> CPU */
210static struct snd_soc_dai_link omap3pandora_dai[] = {
211 {
212 .name = "PCM1773",
213 .stream_name = "HiFi Out",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000214 .cpu_dai_name = "omap-mcbsp-dai.1",
215 .codec_dai_name = "twl4030-hifi",
216 .platform_name = "omap-pcm-audio",
217 .codec_name = "twl4030-codec",
Jarkko Nikulacf9feff2011-09-30 16:07:45 +0300218 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
219 SND_SOC_DAIFMT_CBS_CFS,
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +0300220 .ops = &omap3pandora_ops,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200221 .init = omap3pandora_out_init,
222 }, {
223 .name = "TWL4030",
224 .stream_name = "Line/Mic In",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000225 .cpu_dai_name = "omap-mcbsp-dai.3",
226 .codec_dai_name = "twl4030-hifi",
227 .platform_name = "omap-pcm-audio",
228 .codec_name = "twl4030-codec",
Jarkko Nikulacf9feff2011-09-30 16:07:45 +0300229 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
230 SND_SOC_DAIFMT_CBS_CFS,
Grazvydas Ignotas4b94dba2010-06-17 16:45:28 +0300231 .ops = &omap3pandora_ops,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200232 .init = omap3pandora_in_init,
233 }
234};
235
236/* SoC card */
237static struct snd_soc_card snd_soc_card_omap3pandora = {
238 .name = "omap3pandora",
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200239 .dai_link = omap3pandora_dai,
240 .num_links = ARRAY_SIZE(omap3pandora_dai),
241};
242
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200243static struct platform_device *omap3pandora_snd_device;
244
245static int __init omap3pandora_soc_init(void)
246{
247 int ret;
248
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200249 if (!machine_is_omap3_pandora())
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200250 return -ENODEV;
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200251
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200252 pr_info("OMAP3 Pandora SoC init\n");
253
254 ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
255 if (ret) {
256 pr_err(PREFIX "Failed to get DAC power GPIO\n");
257 return ret;
258 }
259
260 ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
261 if (ret) {
262 pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
263 goto fail0;
264 }
265
266 ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
267 if (ret) {
268 pr_err(PREFIX "Failed to get amp power GPIO\n");
269 goto fail0;
270 }
271
272 ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
273 if (ret) {
274 pr_err(PREFIX "Failed to set amp power GPIO direction\n");
275 goto fail1;
276 }
277
278 omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
279 if (omap3pandora_snd_device == NULL) {
280 pr_err(PREFIX "Platform device allocation failed\n");
281 ret = -ENOMEM;
282 goto fail1;
283 }
284
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000285 platform_set_drvdata(omap3pandora_snd_device, &snd_soc_card_omap3pandora);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200286
287 ret = platform_device_add(omap3pandora_snd_device);
288 if (ret) {
289 pr_err(PREFIX "Unable to add platform device\n");
290 goto fail2;
291 }
292
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200293 omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
294 if (IS_ERR(omap3pandora_dac_reg)) {
295 pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
296 dev_name(&omap3pandora_snd_device->dev),
297 PTR_ERR(omap3pandora_dac_reg));
Axel Lin5c12d202010-11-24 15:20:48 +0800298 ret = PTR_ERR(omap3pandora_dac_reg);
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200299 goto fail3;
300 }
301
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200302 return 0;
303
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200304fail3:
305 platform_device_del(omap3pandora_snd_device);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200306fail2:
307 platform_device_put(omap3pandora_snd_device);
308fail1:
309 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
310fail0:
311 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
312 return ret;
313}
314module_init(omap3pandora_soc_init);
315
316static void __exit omap3pandora_soc_exit(void)
317{
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200318 regulator_put(omap3pandora_dac_reg);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200319 platform_device_unregister(omap3pandora_snd_device);
320 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
321 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
322}
323module_exit(omap3pandora_soc_exit);
324
325MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
326MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
327MODULE_LICENSE("GPL");