ASoC: Move headset jack registration to device initialization for SDP3430
[linux-2.6] / sound / soc / omap / sdp3430.c
1 /*
2  * sdp3430.c  --  SoC audio for TI OMAP3430 SDP
3  *
4  * Author: Misael Lopez Cruz <x0052729@ti.com>
5  *
6  * Based on:
7  * Author: Steve Sakoman <steve@sakoman.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <linux/clk.h>
26 #include <linux/platform_device.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/soc.h>
30 #include <sound/soc-dapm.h>
31 #include <sound/jack.h>
32
33 #include <asm/mach-types.h>
34 #include <mach/hardware.h>
35 #include <mach/gpio.h>
36 #include <mach/mcbsp.h>
37
38 #include "omap-mcbsp.h"
39 #include "omap-pcm.h"
40 #include "../codecs/twl4030.h"
41
42 static struct snd_soc_card snd_soc_sdp3430;
43
44 static int sdp3430_hw_params(struct snd_pcm_substream *substream,
45         struct snd_pcm_hw_params *params)
46 {
47         struct snd_soc_pcm_runtime *rtd = substream->private_data;
48         struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
49         struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
50         int ret;
51
52         /* Set codec DAI configuration */
53         ret = snd_soc_dai_set_fmt(codec_dai,
54                                   SND_SOC_DAIFMT_I2S |
55                                   SND_SOC_DAIFMT_NB_NF |
56                                   SND_SOC_DAIFMT_CBM_CFM);
57         if (ret < 0) {
58                 printk(KERN_ERR "can't set codec DAI configuration\n");
59                 return ret;
60         }
61
62         /* Set cpu DAI configuration */
63         ret = snd_soc_dai_set_fmt(cpu_dai,
64                                   SND_SOC_DAIFMT_I2S |
65                                   SND_SOC_DAIFMT_NB_NF |
66                                   SND_SOC_DAIFMT_CBM_CFM);
67         if (ret < 0) {
68                 printk(KERN_ERR "can't set cpu DAI configuration\n");
69                 return ret;
70         }
71
72         /* Set the codec system clock for DAC and ADC */
73         ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
74                                             SND_SOC_CLOCK_IN);
75         if (ret < 0) {
76                 printk(KERN_ERR "can't set codec system clock\n");
77                 return ret;
78         }
79
80         return 0;
81 }
82
83 static struct snd_soc_ops sdp3430_ops = {
84         .hw_params = sdp3430_hw_params,
85 };
86
87 /* Headset jack */
88 static struct snd_soc_jack hs_jack;
89
90 /* Headset jack detection DAPM pins */
91 static struct snd_soc_jack_pin hs_jack_pins[] = {
92         {
93                 .pin = "Headset Jack",
94                 .mask = SND_JACK_HEADSET,
95         },
96 };
97
98 /* Headset jack detection gpios */
99 static struct snd_soc_jack_gpio hs_jack_gpios[] = {
100         {
101                 .gpio = (OMAP_MAX_GPIO_LINES + 2),
102                 .name = "hsdet-gpio",
103                 .report = SND_JACK_HEADSET,
104                 .debounce_time = 200,
105         },
106 };
107
108 /* SDP3430 machine DAPM */
109 static const struct snd_soc_dapm_widget sdp3430_twl4030_dapm_widgets[] = {
110         SND_SOC_DAPM_MIC("Ext Mic", NULL),
111         SND_SOC_DAPM_SPK("Ext Spk", NULL),
112         SND_SOC_DAPM_HP("Headset Jack", NULL),
113 };
114
115 static const struct snd_soc_dapm_route audio_map[] = {
116         /* External Mics: MAINMIC, SUBMIC with bias*/
117         {"MAINMIC", NULL, "Mic Bias 1"},
118         {"SUBMIC", NULL, "Mic Bias 2"},
119         {"Mic Bias 1", NULL, "Ext Mic"},
120         {"Mic Bias 2", NULL, "Ext Mic"},
121
122         /* External Speakers: HFL, HFR */
123         {"Ext Spk", NULL, "HFL"},
124         {"Ext Spk", NULL, "HFR"},
125
126         /* Headset: HSMIC (with bias), HSOL, HSOR */
127         {"Headset Jack", NULL, "HSOL"},
128         {"Headset Jack", NULL, "HSOR"},
129         {"HSMIC", NULL, "Headset Mic Bias"},
130         {"Headset Mic Bias", NULL, "Headset Jack"},
131 };
132
133 static int sdp3430_twl4030_init(struct snd_soc_codec *codec)
134 {
135         int ret;
136
137         /* Add SDP3430 specific widgets */
138         ret = snd_soc_dapm_new_controls(codec, sdp3430_twl4030_dapm_widgets,
139                                 ARRAY_SIZE(sdp3430_twl4030_dapm_widgets));
140         if (ret)
141                 return ret;
142
143         /* Set up SDP3430 specific audio path audio_map */
144         snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
145
146         /* SDP3430 connected pins */
147         snd_soc_dapm_enable_pin(codec, "Ext Mic");
148         snd_soc_dapm_enable_pin(codec, "Ext Spk");
149         snd_soc_dapm_disable_pin(codec, "Headset Jack");
150
151         /* TWL4030 not connected pins */
152         snd_soc_dapm_nc_pin(codec, "AUXL");
153         snd_soc_dapm_nc_pin(codec, "AUXR");
154         snd_soc_dapm_nc_pin(codec, "CARKITMIC");
155         snd_soc_dapm_nc_pin(codec, "DIGIMIC0");
156         snd_soc_dapm_nc_pin(codec, "DIGIMIC1");
157
158         snd_soc_dapm_nc_pin(codec, "OUTL");
159         snd_soc_dapm_nc_pin(codec, "OUTR");
160         snd_soc_dapm_nc_pin(codec, "EARPIECE");
161         snd_soc_dapm_nc_pin(codec, "PREDRIVEL");
162         snd_soc_dapm_nc_pin(codec, "PREDRIVER");
163         snd_soc_dapm_nc_pin(codec, "CARKITL");
164         snd_soc_dapm_nc_pin(codec, "CARKITR");
165
166         ret = snd_soc_dapm_sync(codec);
167         if (ret)
168                 return ret;
169
170         /* Headset jack detection */
171         ret = snd_soc_jack_new(&snd_soc_sdp3430, "Headset Jack",
172                                 SND_JACK_HEADSET, &hs_jack);
173         if (ret)
174                 return ret;
175
176         ret = snd_soc_jack_add_pins(&hs_jack, ARRAY_SIZE(hs_jack_pins),
177                                 hs_jack_pins);
178         if (ret)
179                 return ret;
180
181         ret = snd_soc_jack_add_gpios(&hs_jack, ARRAY_SIZE(hs_jack_gpios),
182                                 hs_jack_gpios);
183
184         return ret;
185 }
186
187 /* Digital audio interface glue - connects codec <--> CPU */
188 static struct snd_soc_dai_link sdp3430_dai = {
189         .name = "TWL4030",
190         .stream_name = "TWL4030",
191         .cpu_dai = &omap_mcbsp_dai[0],
192         .codec_dai = &twl4030_dai,
193         .init = sdp3430_twl4030_init,
194         .ops = &sdp3430_ops,
195 };
196
197 /* Audio machine driver */
198 static struct snd_soc_card snd_soc_sdp3430 = {
199         .name = "SDP3430",
200         .platform = &omap_soc_platform,
201         .dai_link = &sdp3430_dai,
202         .num_links = 1,
203 };
204
205 /* Audio subsystem */
206 static struct snd_soc_device sdp3430_snd_devdata = {
207         .card = &snd_soc_sdp3430,
208         .codec_dev = &soc_codec_dev_twl4030,
209 };
210
211 static struct platform_device *sdp3430_snd_device;
212
213 static int __init sdp3430_soc_init(void)
214 {
215         int ret;
216
217         if (!machine_is_omap_3430sdp()) {
218                 pr_debug("Not SDP3430!\n");
219                 return -ENODEV;
220         }
221         printk(KERN_INFO "SDP3430 SoC init\n");
222
223         sdp3430_snd_device = platform_device_alloc("soc-audio", -1);
224         if (!sdp3430_snd_device) {
225                 printk(KERN_ERR "Platform device allocation failed\n");
226                 return -ENOMEM;
227         }
228
229         platform_set_drvdata(sdp3430_snd_device, &sdp3430_snd_devdata);
230         sdp3430_snd_devdata.dev = &sdp3430_snd_device->dev;
231         *(unsigned int *)sdp3430_dai.cpu_dai->private_data = 1; /* McBSP2 */
232
233         ret = platform_device_add(sdp3430_snd_device);
234         if (ret)
235                 goto err1;
236
237         return 0;
238
239 err1:
240         printk(KERN_ERR "Unable to add platform device\n");
241         platform_device_put(sdp3430_snd_device);
242
243         return ret;
244 }
245 module_init(sdp3430_soc_init);
246
247 static void __exit sdp3430_soc_exit(void)
248 {
249         snd_soc_jack_free_gpios(&hs_jack, ARRAY_SIZE(hs_jack_gpios),
250                                 hs_jack_gpios);
251
252         platform_device_unregister(sdp3430_snd_device);
253 }
254 module_exit(sdp3430_soc_exit);
255
256 MODULE_AUTHOR("Misael Lopez Cruz <x0052729@ti.com>");
257 MODULE_DESCRIPTION("ALSA SoC SDP3430");
258 MODULE_LICENSE("GPL");
259