blob: cace5f13792df8956e57fd79ff6049ad705ba6a4 [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>
26
27#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/soc.h>
30#include <sound/soc-dapm.h>
31
32#include <asm/mach-types.h>
33
34#include "omap-mcbsp.h"
35#include "omap-pcm.h"
36#include "../codecs/twl4030.h"
37
38#define OMAP3_PANDORA_DAC_POWER_GPIO 118
39#define OMAP3_PANDORA_AMP_POWER_GPIO 14
40
41#define PREFIX "ASoC omap3pandora: "
42
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020043static int omap3pandora_cmn_hw_params(struct snd_pcm_substream *substream,
44 struct snd_pcm_hw_params *params, unsigned int fmt)
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020045{
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020046 struct snd_soc_pcm_runtime *rtd = substream->private_data;
47 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
48 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020049 int ret;
50
51 /* Set codec DAI configuration */
52 ret = snd_soc_dai_set_fmt(codec_dai, fmt);
53 if (ret < 0) {
54 pr_err(PREFIX "can't set codec DAI configuration\n");
55 return ret;
56 }
57
58 /* Set cpu DAI configuration */
59 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
60 if (ret < 0) {
61 pr_err(PREFIX "can't set cpu DAI configuration\n");
62 return ret;
63 }
64
65 /* Set the codec system clock for DAC and ADC */
66 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
67 SND_SOC_CLOCK_IN);
68 if (ret < 0) {
69 pr_err(PREFIX "can't set codec system clock\n");
70 return ret;
71 }
72
73 /* Set McBSP clock to external */
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020074 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
75 256 * params_rate(params),
76 SND_SOC_CLOCK_IN);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020077 if (ret < 0) {
78 pr_err(PREFIX "can't set cpu system clock\n");
79 return ret;
80 }
81
82 ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8);
83 if (ret < 0) {
84 pr_err(PREFIX "can't set SRG clock divider\n");
85 return ret;
86 }
87
88 return 0;
89}
90
91static int omap3pandora_out_hw_params(struct snd_pcm_substream *substream,
92 struct snd_pcm_hw_params *params)
93{
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020094 return omap3pandora_cmn_hw_params(substream, params,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020095 SND_SOC_DAIFMT_I2S |
96 SND_SOC_DAIFMT_IB_NF |
97 SND_SOC_DAIFMT_CBS_CFS);
98}
99
100static int omap3pandora_in_hw_params(struct snd_pcm_substream *substream,
101 struct snd_pcm_hw_params *params)
102{
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +0200103 return omap3pandora_cmn_hw_params(substream, params,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200104 SND_SOC_DAIFMT_I2S |
105 SND_SOC_DAIFMT_NB_NF |
106 SND_SOC_DAIFMT_CBS_CFS);
107}
108
109static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
110 struct snd_kcontrol *k, int event)
111{
112 if (SND_SOC_DAPM_EVENT_ON(event)) {
113 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
114 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
115 } else {
116 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
117 mdelay(1);
118 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
119 }
120
121 return 0;
122}
123
124/*
125 * Audio paths on Pandora board:
126 *
127 * |O| ---> PCM DAC +-> AMP -> Headphone Jack
128 * |M| A +--------> Line Out
129 * |A| <~~clk~~+
130 * |P| <--- TWL4030 <--------- Line In and MICs
131 */
132static const struct snd_soc_dapm_widget omap3pandora_out_dapm_widgets[] = {
133 SND_SOC_DAPM_DAC("PCM DAC", "Playback", SND_SOC_NOPM, 0, 0),
134 SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
135 0, 0, NULL, 0, omap3pandora_hp_event,
136 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
137 SND_SOC_DAPM_HP("Headphone Jack", NULL),
138 SND_SOC_DAPM_LINE("Line Out", NULL),
139};
140
141static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = {
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200142 SND_SOC_DAPM_MIC("Mic (internal)", NULL),
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200143 SND_SOC_DAPM_MIC("Mic (external)", NULL),
144 SND_SOC_DAPM_LINE("Line In", NULL),
145};
146
147static const struct snd_soc_dapm_route omap3pandora_out_map[] = {
148 {"Headphone Amplifier", NULL, "PCM DAC"},
149 {"Line Out", NULL, "PCM DAC"},
150 {"Headphone Jack", NULL, "Headphone Amplifier"},
151};
152
153static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200154 {"AUXL", NULL, "Line In"},
155 {"AUXR", NULL, "Line In"},
156
157 {"MAINMIC", NULL, "Mic Bias 1"},
158 {"Mic Bias 1", NULL, "Mic (internal)"},
159
160 {"SUBMIC", NULL, "Mic Bias 2"},
161 {"Mic Bias 2", NULL, "Mic (external)"},
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200162};
163
164static int omap3pandora_out_init(struct snd_soc_codec *codec)
165{
166 int ret;
167
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200168 /* All TWL4030 output pins are floating */
169 snd_soc_dapm_nc_pin(codec, "OUTL");
170 snd_soc_dapm_nc_pin(codec, "OUTR");
171 snd_soc_dapm_nc_pin(codec, "EARPIECE");
172 snd_soc_dapm_nc_pin(codec, "PREDRIVEL");
173 snd_soc_dapm_nc_pin(codec, "PREDRIVER");
174 snd_soc_dapm_nc_pin(codec, "HSOL");
175 snd_soc_dapm_nc_pin(codec, "HSOR");
176 snd_soc_dapm_nc_pin(codec, "CARKITL");
177 snd_soc_dapm_nc_pin(codec, "CARKITR");
178 snd_soc_dapm_nc_pin(codec, "HFL");
179 snd_soc_dapm_nc_pin(codec, "HFR");
180
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200181 ret = snd_soc_dapm_new_controls(codec, omap3pandora_out_dapm_widgets,
182 ARRAY_SIZE(omap3pandora_out_dapm_widgets));
183 if (ret < 0)
184 return ret;
185
186 snd_soc_dapm_add_routes(codec, omap3pandora_out_map,
187 ARRAY_SIZE(omap3pandora_out_map));
188
189 return snd_soc_dapm_sync(codec);
190}
191
192static int omap3pandora_in_init(struct snd_soc_codec *codec)
193{
194 int ret;
195
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200196 /* Not comnnected */
197 snd_soc_dapm_nc_pin(codec, "HSMIC");
198 snd_soc_dapm_nc_pin(codec, "CARKITMIC");
199 snd_soc_dapm_nc_pin(codec, "DIGIMIC0");
200 snd_soc_dapm_nc_pin(codec, "DIGIMIC1");
Grazvydas Ignotas7f185342008-12-23 12:04:48 +0200201
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200202 ret = snd_soc_dapm_new_controls(codec, omap3pandora_in_dapm_widgets,
203 ARRAY_SIZE(omap3pandora_in_dapm_widgets));
204 if (ret < 0)
205 return ret;
206
207 snd_soc_dapm_add_routes(codec, omap3pandora_in_map,
208 ARRAY_SIZE(omap3pandora_in_map));
209
210 return snd_soc_dapm_sync(codec);
211}
212
213static struct snd_soc_ops omap3pandora_out_ops = {
214 .hw_params = omap3pandora_out_hw_params,
215};
216
217static struct snd_soc_ops omap3pandora_in_ops = {
218 .hw_params = omap3pandora_in_hw_params,
219};
220
221/* Digital audio interface glue - connects codec <--> CPU */
222static struct snd_soc_dai_link omap3pandora_dai[] = {
223 {
224 .name = "PCM1773",
225 .stream_name = "HiFi Out",
226 .cpu_dai = &omap_mcbsp_dai[0],
Joonyoung Shim7154b3e2009-04-20 19:21:35 +0900227 .codec_dai = &twl4030_dai[TWL4030_DAI_HIFI],
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200228 .ops = &omap3pandora_out_ops,
229 .init = omap3pandora_out_init,
230 }, {
231 .name = "TWL4030",
232 .stream_name = "Line/Mic In",
233 .cpu_dai = &omap_mcbsp_dai[1],
Joonyoung Shim7154b3e2009-04-20 19:21:35 +0900234 .codec_dai = &twl4030_dai[TWL4030_DAI_HIFI],
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200235 .ops = &omap3pandora_in_ops,
236 .init = omap3pandora_in_init,
237 }
238};
239
240/* SoC card */
241static struct snd_soc_card snd_soc_card_omap3pandora = {
242 .name = "omap3pandora",
243 .platform = &omap_soc_platform,
244 .dai_link = omap3pandora_dai,
245 .num_links = ARRAY_SIZE(omap3pandora_dai),
246};
247
248/* Audio subsystem */
249static struct snd_soc_device omap3pandora_snd_data = {
250 .card = &snd_soc_card_omap3pandora,
251 .codec_dev = &soc_codec_dev_twl4030,
252};
253
254static struct platform_device *omap3pandora_snd_device;
255
256static int __init omap3pandora_soc_init(void)
257{
258 int ret;
259
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200260 if (!machine_is_omap3_pandora())
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200261 return -ENODEV;
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200262
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200263 pr_info("OMAP3 Pandora SoC init\n");
264
265 ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
266 if (ret) {
267 pr_err(PREFIX "Failed to get DAC power GPIO\n");
268 return ret;
269 }
270
271 ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
272 if (ret) {
273 pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
274 goto fail0;
275 }
276
277 ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
278 if (ret) {
279 pr_err(PREFIX "Failed to get amp power GPIO\n");
280 goto fail0;
281 }
282
283 ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
284 if (ret) {
285 pr_err(PREFIX "Failed to set amp power GPIO direction\n");
286 goto fail1;
287 }
288
289 omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
290 if (omap3pandora_snd_device == NULL) {
291 pr_err(PREFIX "Platform device allocation failed\n");
292 ret = -ENOMEM;
293 goto fail1;
294 }
295
296 platform_set_drvdata(omap3pandora_snd_device, &omap3pandora_snd_data);
297 omap3pandora_snd_data.dev = &omap3pandora_snd_device->dev;
298 *(unsigned int *)omap_mcbsp_dai[0].private_data = 1; /* McBSP2 */
299 *(unsigned int *)omap_mcbsp_dai[1].private_data = 3; /* McBSP4 */
300
301 ret = platform_device_add(omap3pandora_snd_device);
302 if (ret) {
303 pr_err(PREFIX "Unable to add platform device\n");
304 goto fail2;
305 }
306
307 return 0;
308
309fail2:
310 platform_device_put(omap3pandora_snd_device);
311fail1:
312 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
313fail0:
314 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
315 return ret;
316}
317module_init(omap3pandora_soc_init);
318
319static void __exit omap3pandora_soc_exit(void)
320{
321 platform_device_unregister(omap3pandora_snd_device);
322 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
323 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
324}
325module_exit(omap3pandora_soc_exit);
326
327MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
328MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
329MODULE_LICENSE("GPL");