blob: de10f76badedf7a31d709851789aa7d68b8ee4f5 [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>
34
35#include "omap-mcbsp.h"
36#include "omap-pcm.h"
37#include "../codecs/twl4030.h"
38
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
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020046static int omap3pandora_cmn_hw_params(struct snd_pcm_substream *substream,
47 struct snd_pcm_hw_params *params, unsigned int fmt)
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;
50 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
51 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020052 int ret;
53
54 /* Set codec DAI configuration */
55 ret = snd_soc_dai_set_fmt(codec_dai, fmt);
56 if (ret < 0) {
57 pr_err(PREFIX "can't set codec DAI configuration\n");
58 return ret;
59 }
60
61 /* Set cpu DAI configuration */
62 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
63 if (ret < 0) {
64 pr_err(PREFIX "can't set cpu DAI configuration\n");
65 return ret;
66 }
67
68 /* Set the codec system clock for DAC and ADC */
69 ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
70 SND_SOC_CLOCK_IN);
71 if (ret < 0) {
72 pr_err(PREFIX "can't set codec system clock\n");
73 return ret;
74 }
75
76 /* Set McBSP clock to external */
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020077 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
78 256 * params_rate(params),
79 SND_SOC_CLOCK_IN);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020080 if (ret < 0) {
81 pr_err(PREFIX "can't set cpu system clock\n");
82 return ret;
83 }
84
85 ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8);
86 if (ret < 0) {
87 pr_err(PREFIX "can't set SRG clock divider\n");
88 return ret;
89 }
90
91 return 0;
92}
93
94static int omap3pandora_out_hw_params(struct snd_pcm_substream *substream,
95 struct snd_pcm_hw_params *params)
96{
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +020097 return omap3pandora_cmn_hw_params(substream, params,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +020098 SND_SOC_DAIFMT_I2S |
99 SND_SOC_DAIFMT_IB_NF |
100 SND_SOC_DAIFMT_CBS_CFS);
101}
102
103static int omap3pandora_in_hw_params(struct snd_pcm_substream *substream,
104 struct snd_pcm_hw_params *params)
105{
Jarkko Nikula9e5d86f2009-11-09 08:44:32 +0200106 return omap3pandora_cmn_hw_params(substream, params,
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200107 SND_SOC_DAIFMT_I2S |
108 SND_SOC_DAIFMT_NB_NF |
109 SND_SOC_DAIFMT_CBS_CFS);
110}
111
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200112static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
113 struct snd_kcontrol *k, int event)
114{
115 /*
116 * The PCM1773 DAC datasheet requires 1ms delay between switching
117 * VCC power on/off and /PD pin high/low
118 */
119 if (SND_SOC_DAPM_EVENT_ON(event)) {
120 regulator_enable(omap3pandora_dac_reg);
121 mdelay(1);
122 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
123 } else {
124 gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
125 mdelay(1);
126 regulator_disable(omap3pandora_dac_reg);
127 }
128
129 return 0;
130}
131
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200132static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
133 struct snd_kcontrol *k, int event)
134{
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200135 if (SND_SOC_DAPM_EVENT_ON(event))
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200136 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200137 else
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200138 gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200139
140 return 0;
141}
142
143/*
144 * Audio paths on Pandora board:
145 *
146 * |O| ---> PCM DAC +-> AMP -> Headphone Jack
147 * |M| A +--------> Line Out
148 * |A| <~~clk~~+
149 * |P| <--- TWL4030 <--------- Line In and MICs
150 */
151static const struct snd_soc_dapm_widget omap3pandora_out_dapm_widgets[] = {
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200152 SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
153 0, 0, omap3pandora_dac_event,
154 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200155 SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
156 0, 0, NULL, 0, omap3pandora_hp_event,
157 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
158 SND_SOC_DAPM_HP("Headphone Jack", NULL),
159 SND_SOC_DAPM_LINE("Line Out", NULL),
160};
161
162static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = {
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200163 SND_SOC_DAPM_MIC("Mic (internal)", NULL),
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200164 SND_SOC_DAPM_MIC("Mic (external)", NULL),
165 SND_SOC_DAPM_LINE("Line In", NULL),
166};
167
168static const struct snd_soc_dapm_route omap3pandora_out_map[] = {
Grazvydas Ignotas3b9447f2010-02-05 00:55:33 +0200169 {"PCM DAC", NULL, "APLL Enable"},
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200170 {"Headphone Amplifier", NULL, "PCM DAC"},
171 {"Line Out", NULL, "PCM DAC"},
172 {"Headphone Jack", NULL, "Headphone Amplifier"},
173};
174
175static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200176 {"AUXL", NULL, "Line In"},
177 {"AUXR", NULL, "Line In"},
178
179 {"MAINMIC", NULL, "Mic Bias 1"},
180 {"Mic Bias 1", NULL, "Mic (internal)"},
181
182 {"SUBMIC", NULL, "Mic Bias 2"},
183 {"Mic Bias 2", NULL, "Mic (external)"},
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200184};
185
186static int omap3pandora_out_init(struct snd_soc_codec *codec)
187{
188 int ret;
189
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200190 /* All TWL4030 output pins are floating */
191 snd_soc_dapm_nc_pin(codec, "OUTL");
192 snd_soc_dapm_nc_pin(codec, "OUTR");
193 snd_soc_dapm_nc_pin(codec, "EARPIECE");
194 snd_soc_dapm_nc_pin(codec, "PREDRIVEL");
195 snd_soc_dapm_nc_pin(codec, "PREDRIVER");
196 snd_soc_dapm_nc_pin(codec, "HSOL");
197 snd_soc_dapm_nc_pin(codec, "HSOR");
198 snd_soc_dapm_nc_pin(codec, "CARKITL");
199 snd_soc_dapm_nc_pin(codec, "CARKITR");
200 snd_soc_dapm_nc_pin(codec, "HFL");
201 snd_soc_dapm_nc_pin(codec, "HFR");
Grazvydas Ignotasf3dd7042009-11-07 23:16:12 +0200202 snd_soc_dapm_nc_pin(codec, "VIBRA");
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200203
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200204 ret = snd_soc_dapm_new_controls(codec, omap3pandora_out_dapm_widgets,
205 ARRAY_SIZE(omap3pandora_out_dapm_widgets));
206 if (ret < 0)
207 return ret;
208
209 snd_soc_dapm_add_routes(codec, omap3pandora_out_map,
210 ARRAY_SIZE(omap3pandora_out_map));
211
212 return snd_soc_dapm_sync(codec);
213}
214
215static int omap3pandora_in_init(struct snd_soc_codec *codec)
216{
217 int ret;
218
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200219 /* Not comnnected */
220 snd_soc_dapm_nc_pin(codec, "HSMIC");
221 snd_soc_dapm_nc_pin(codec, "CARKITMIC");
222 snd_soc_dapm_nc_pin(codec, "DIGIMIC0");
223 snd_soc_dapm_nc_pin(codec, "DIGIMIC1");
Grazvydas Ignotas7f185342008-12-23 12:04:48 +0200224
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200225 ret = snd_soc_dapm_new_controls(codec, omap3pandora_in_dapm_widgets,
226 ARRAY_SIZE(omap3pandora_in_dapm_widgets));
227 if (ret < 0)
228 return ret;
229
230 snd_soc_dapm_add_routes(codec, omap3pandora_in_map,
231 ARRAY_SIZE(omap3pandora_in_map));
232
233 return snd_soc_dapm_sync(codec);
234}
235
236static struct snd_soc_ops omap3pandora_out_ops = {
237 .hw_params = omap3pandora_out_hw_params,
238};
239
240static struct snd_soc_ops omap3pandora_in_ops = {
241 .hw_params = omap3pandora_in_hw_params,
242};
243
244/* Digital audio interface glue - connects codec <--> CPU */
245static struct snd_soc_dai_link omap3pandora_dai[] = {
246 {
247 .name = "PCM1773",
248 .stream_name = "HiFi Out",
249 .cpu_dai = &omap_mcbsp_dai[0],
Joonyoung Shim7154b3e2009-04-20 19:21:35 +0900250 .codec_dai = &twl4030_dai[TWL4030_DAI_HIFI],
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200251 .ops = &omap3pandora_out_ops,
252 .init = omap3pandora_out_init,
253 }, {
254 .name = "TWL4030",
255 .stream_name = "Line/Mic In",
256 .cpu_dai = &omap_mcbsp_dai[1],
Joonyoung Shim7154b3e2009-04-20 19:21:35 +0900257 .codec_dai = &twl4030_dai[TWL4030_DAI_HIFI],
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200258 .ops = &omap3pandora_in_ops,
259 .init = omap3pandora_in_init,
260 }
261};
262
263/* SoC card */
264static struct snd_soc_card snd_soc_card_omap3pandora = {
265 .name = "omap3pandora",
266 .platform = &omap_soc_platform,
267 .dai_link = omap3pandora_dai,
268 .num_links = ARRAY_SIZE(omap3pandora_dai),
269};
270
271/* Audio subsystem */
272static struct snd_soc_device omap3pandora_snd_data = {
273 .card = &snd_soc_card_omap3pandora,
274 .codec_dev = &soc_codec_dev_twl4030,
275};
276
277static struct platform_device *omap3pandora_snd_device;
278
279static int __init omap3pandora_soc_init(void)
280{
281 int ret;
282
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200283 if (!machine_is_omap3_pandora())
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200284 return -ENODEV;
Grazvydas Ignotas8f008062009-01-31 16:29:24 +0200285
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200286 pr_info("OMAP3 Pandora SoC init\n");
287
288 ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
289 if (ret) {
290 pr_err(PREFIX "Failed to get DAC power GPIO\n");
291 return ret;
292 }
293
294 ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
295 if (ret) {
296 pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
297 goto fail0;
298 }
299
300 ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
301 if (ret) {
302 pr_err(PREFIX "Failed to get amp power GPIO\n");
303 goto fail0;
304 }
305
306 ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
307 if (ret) {
308 pr_err(PREFIX "Failed to set amp power GPIO direction\n");
309 goto fail1;
310 }
311
312 omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
313 if (omap3pandora_snd_device == NULL) {
314 pr_err(PREFIX "Platform device allocation failed\n");
315 ret = -ENOMEM;
316 goto fail1;
317 }
318
319 platform_set_drvdata(omap3pandora_snd_device, &omap3pandora_snd_data);
320 omap3pandora_snd_data.dev = &omap3pandora_snd_device->dev;
321 *(unsigned int *)omap_mcbsp_dai[0].private_data = 1; /* McBSP2 */
322 *(unsigned int *)omap_mcbsp_dai[1].private_data = 3; /* McBSP4 */
323
324 ret = platform_device_add(omap3pandora_snd_device);
325 if (ret) {
326 pr_err(PREFIX "Unable to add platform device\n");
327 goto fail2;
328 }
329
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200330 omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
331 if (IS_ERR(omap3pandora_dac_reg)) {
332 pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
333 dev_name(&omap3pandora_snd_device->dev),
334 PTR_ERR(omap3pandora_dac_reg));
335 goto fail3;
336 }
337
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200338 return 0;
339
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200340fail3:
341 platform_device_del(omap3pandora_snd_device);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200342fail2:
343 platform_device_put(omap3pandora_snd_device);
344fail1:
345 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
346fail0:
347 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
348 return ret;
349}
350module_init(omap3pandora_soc_init);
351
352static void __exit omap3pandora_soc_exit(void)
353{
Grazvydas Ignotasc50749d2010-02-05 16:29:53 +0200354 regulator_put(omap3pandora_dac_reg);
Grazvydas Ignotas68fb7402008-12-04 22:39:54 +0200355 platform_device_unregister(omap3pandora_snd_device);
356 gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
357 gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
358}
359module_exit(omap3pandora_soc_exit);
360
361MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
362MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
363MODULE_LICENSE("GPL");