ASoC: Decouple DAPM from CODECs
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / pxa / e740_wm9705.c
CommitLineData
28796eaf
IM
1/*
2 * e740-wm9705.c -- SoC audio for e740
3 *
4 * Copyright 2007 (c) Ian Molton <spyro@f2s.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 ONLY.
9 *
10 */
11
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/gpio.h>
15
16#include <sound/core.h>
17#include <sound/pcm.h>
18#include <sound/soc.h>
19#include <sound/soc-dapm.h>
20
28796eaf
IM
21#include <mach/audio.h>
22#include <mach/eseries-gpio.h>
23
24#include <asm/mach-types.h>
25
26#include "../codecs/wm9705.h"
28796eaf
IM
27#include "pxa2xx-ac97.h"
28
29
30#define E740_AUDIO_OUT 1
31#define E740_AUDIO_IN 2
32
33static int e740_audio_power;
34
35static void e740_sync_audio_power(int status)
36{
37 gpio_set_value(GPIO_E740_WM9705_nAVDD2, !status);
38 gpio_set_value(GPIO_E740_AMP_ON, (status & E740_AUDIO_OUT) ? 1 : 0);
39 gpio_set_value(GPIO_E740_MIC_ON, (status & E740_AUDIO_IN) ? 1 : 0);
40}
41
42static int e740_mic_amp_event(struct snd_soc_dapm_widget *w,
43 struct snd_kcontrol *kcontrol, int event)
44{
45 if (event & SND_SOC_DAPM_PRE_PMU)
46 e740_audio_power |= E740_AUDIO_IN;
47 else if (event & SND_SOC_DAPM_POST_PMD)
48 e740_audio_power &= ~E740_AUDIO_IN;
49
50 e740_sync_audio_power(e740_audio_power);
51
52 return 0;
53}
54
55static int e740_output_amp_event(struct snd_soc_dapm_widget *w,
56 struct snd_kcontrol *kcontrol, int event)
57{
58 if (event & SND_SOC_DAPM_PRE_PMU)
59 e740_audio_power |= E740_AUDIO_OUT;
60 else if (event & SND_SOC_DAPM_POST_PMD)
61 e740_audio_power &= ~E740_AUDIO_OUT;
62
63 e740_sync_audio_power(e740_audio_power);
64
65 return 0;
66}
67
68static const struct snd_soc_dapm_widget e740_dapm_widgets[] = {
69 SND_SOC_DAPM_HP("Headphone Jack", NULL),
70 SND_SOC_DAPM_SPK("Speaker", NULL),
71 SND_SOC_DAPM_MIC("Mic (Internal)", NULL),
72 SND_SOC_DAPM_PGA_E("Output Amp", SND_SOC_NOPM, 0, 0, NULL, 0,
73 e740_output_amp_event, SND_SOC_DAPM_PRE_PMU |
74 SND_SOC_DAPM_POST_PMD),
75 SND_SOC_DAPM_PGA_E("Mic Amp", SND_SOC_NOPM, 0, 0, NULL, 0,
76 e740_mic_amp_event, SND_SOC_DAPM_PRE_PMU |
77 SND_SOC_DAPM_POST_PMD),
78};
79
80static const struct snd_soc_dapm_route audio_map[] = {
81 {"Output Amp", NULL, "LOUT"},
82 {"Output Amp", NULL, "ROUT"},
83 {"Output Amp", NULL, "MONOOUT"},
84
85 {"Speaker", NULL, "Output Amp"},
86 {"Headphone Jack", NULL, "Output Amp"},
87
88 {"MIC1", NULL, "Mic Amp"},
89 {"Mic Amp", NULL, "Mic (Internal)"},
90};
91
f0fba2ad 92static int e740_ac97_init(struct snd_soc_pcm_runtime *rtd)
28796eaf 93{
f0fba2ad 94 struct snd_soc_codec *codec = rtd->codec;
ce6120cc
LG
95 struct snd_soc_dapm_context *dapm = &codec->dapm;
96
97 snd_soc_dapm_nc_pin(dapm, "HPOUTL");
98 snd_soc_dapm_nc_pin(dapm, "HPOUTR");
99 snd_soc_dapm_nc_pin(dapm, "PHONE");
100 snd_soc_dapm_nc_pin(dapm, "LINEINL");
101 snd_soc_dapm_nc_pin(dapm, "LINEINR");
102 snd_soc_dapm_nc_pin(dapm, "CDINL");
103 snd_soc_dapm_nc_pin(dapm, "CDINR");
104 snd_soc_dapm_nc_pin(dapm, "PCBEEP");
105 snd_soc_dapm_nc_pin(dapm, "MIC2");
106
107 snd_soc_dapm_new_controls(dapm, e740_dapm_widgets,
28796eaf
IM
108 ARRAY_SIZE(e740_dapm_widgets));
109
ce6120cc 110 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
28796eaf 111
ce6120cc 112 snd_soc_dapm_sync(dapm);
28796eaf
IM
113
114 return 0;
115}
116
117static struct snd_soc_dai_link e740_dai[] = {
118 {
119 .name = "AC97",
120 .stream_name = "AC97 HiFi",
f0fba2ad
LG
121 .cpu_dai_name = "pxa-ac97.0",
122 .codec_dai_name = "wm9705-hifi",
123 .platform_name = "pxa-pcm-audio",
124 .codec_name = "wm9705-codec",
28796eaf
IM
125 .init = e740_ac97_init,
126 },
127 {
128 .name = "AC97 Aux",
129 .stream_name = "AC97 Aux",
f0fba2ad
LG
130 .cpu_dai_name = "pxa-ac97.1",
131 .codec_dai_name = "wm9705-aux",
132 .platform_name = "pxa-pcm-audio",
133 .codec_name = "wm9705-codec",
28796eaf
IM
134 },
135};
136
137static struct snd_soc_card e740 = {
138 .name = "Toshiba e740",
28796eaf
IM
139 .dai_link = e740_dai,
140 .num_links = ARRAY_SIZE(e740_dai),
141};
142
28796eaf
IM
143static struct platform_device *e740_snd_device;
144
145static int __init e740_init(void)
146{
147 int ret;
148
149 if (!machine_is_e740())
150 return -ENODEV;
151
152 ret = gpio_request(GPIO_E740_MIC_ON, "Mic amp");
153 if (ret)
154 return ret;
155
156 ret = gpio_request(GPIO_E740_AMP_ON, "Output amp");
157 if (ret)
158 goto free_mic_amp_gpio;
159
160 ret = gpio_request(GPIO_E740_WM9705_nAVDD2, "Audio power");
161 if (ret)
162 goto free_op_amp_gpio;
163
164 /* Disable audio */
165 ret = gpio_direction_output(GPIO_E740_MIC_ON, 0);
166 if (ret)
167 goto free_apwr_gpio;
168 ret = gpio_direction_output(GPIO_E740_AMP_ON, 0);
169 if (ret)
170 goto free_apwr_gpio;
171 ret = gpio_direction_output(GPIO_E740_WM9705_nAVDD2, 1);
172 if (ret)
173 goto free_apwr_gpio;
174
175 e740_snd_device = platform_device_alloc("soc-audio", -1);
176 if (!e740_snd_device) {
177 ret = -ENOMEM;
178 goto free_apwr_gpio;
179 }
180
f0fba2ad 181 platform_set_drvdata(e740_snd_device, &e740);
28796eaf
IM
182 ret = platform_device_add(e740_snd_device);
183
184 if (!ret)
185 return 0;
186
187/* Fail gracefully */
188 platform_device_put(e740_snd_device);
189free_apwr_gpio:
190 gpio_free(GPIO_E740_WM9705_nAVDD2);
191free_op_amp_gpio:
192 gpio_free(GPIO_E740_AMP_ON);
193free_mic_amp_gpio:
194 gpio_free(GPIO_E740_MIC_ON);
195
196 return ret;
197}
198
199static void __exit e740_exit(void)
200{
201 platform_device_unregister(e740_snd_device);
b67696b4
AL
202 gpio_free(GPIO_E740_WM9705_nAVDD2);
203 gpio_free(GPIO_E740_AMP_ON);
204 gpio_free(GPIO_E740_MIC_ON);
28796eaf
IM
205}
206
207module_init(e740_init);
208module_exit(e740_exit);
209
210/* Module information */
211MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
212MODULE_DESCRIPTION("ALSA SoC driver for e740");
213MODULE_LICENSE("GPL v2");