ASoC: Decouple DAPM from CODECs
[deliverable/linux.git] / sound / soc / pxa / z2.c
1 /*
2 * linux/sound/soc/pxa/z2.c
3 *
4 * SoC Audio driver for Aeronix Zipit Z2
5 *
6 * Copyright (C) 2009 Ken McGuire <kenm@desertweyr.com>
7 * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/timer.h>
17 #include <linux/interrupt.h>
18 #include <linux/platform_device.h>
19 #include <linux/gpio.h>
20
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/soc.h>
24 #include <sound/soc-dapm.h>
25 #include <sound/jack.h>
26
27 #include <asm/mach-types.h>
28 #include <mach/hardware.h>
29 #include <mach/audio.h>
30 #include <mach/z2.h>
31
32 #include "../codecs/wm8750.h"
33 #include "pxa2xx-i2s.h"
34
35 static struct snd_soc_card snd_soc_z2;
36
37 static int z2_hw_params(struct snd_pcm_substream *substream,
38 struct snd_pcm_hw_params *params)
39 {
40 struct snd_soc_pcm_runtime *rtd = substream->private_data;
41 struct snd_soc_dai *codec_dai = rtd->codec_dai;
42 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
43 unsigned int clk = 0;
44 int ret = 0;
45
46 switch (params_rate(params)) {
47 case 8000:
48 case 16000:
49 case 48000:
50 case 96000:
51 clk = 12288000;
52 break;
53 case 11025:
54 case 22050:
55 case 44100:
56 clk = 11289600;
57 break;
58 }
59
60 /* set codec DAI configuration */
61 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
62 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
63 if (ret < 0)
64 return ret;
65
66 /* set cpu DAI configuration */
67 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
68 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
69 if (ret < 0)
70 return ret;
71
72 /* set the codec system clock for DAC and ADC */
73 ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk,
74 SND_SOC_CLOCK_IN);
75 if (ret < 0)
76 return ret;
77
78 /* set the I2S system clock as input (unused) */
79 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
80 SND_SOC_CLOCK_IN);
81 if (ret < 0)
82 return ret;
83
84 return 0;
85 }
86
87 static struct snd_soc_jack hs_jack;
88
89 /* Headset jack detection DAPM pins */
90 static struct snd_soc_jack_pin hs_jack_pins[] = {
91 {
92 .pin = "Mic Jack",
93 .mask = SND_JACK_MICROPHONE,
94 },
95 {
96 .pin = "Headphone Jack",
97 .mask = SND_JACK_HEADPHONE,
98 },
99 };
100
101 /* Headset jack detection gpios */
102 static struct snd_soc_jack_gpio hs_jack_gpios[] = {
103 {
104 .gpio = GPIO37_ZIPITZ2_HEADSET_DETECT,
105 .name = "hsdet-gpio",
106 .report = SND_JACK_HEADSET,
107 .debounce_time = 200,
108 },
109 };
110
111 /* z2 machine dapm widgets */
112 static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
113 SND_SOC_DAPM_HP("Headphone Jack", NULL),
114 SND_SOC_DAPM_MIC("Mic Jack", NULL),
115 SND_SOC_DAPM_SPK("Ext Spk", NULL),
116
117 /* headset is a mic and mono headphone */
118 SND_SOC_DAPM_HP("Headset Jack", NULL),
119 };
120
121 /* Z2 machine audio_map */
122 static const struct snd_soc_dapm_route audio_map[] = {
123
124 /* headphone connected to LOUT1, ROUT1 */
125 {"Headphone Jack", NULL, "LOUT1"},
126 {"Headphone Jack", NULL, "ROUT1"},
127
128 /* ext speaker connected to LOUT2, ROUT2 */
129 {"Ext Spk", NULL , "ROUT2"},
130 {"Ext Spk", NULL , "LOUT2"},
131
132 /* mic is connected to R input 2 - with bias */
133 {"RINPUT2", NULL, "Mic Bias"},
134 {"Mic Bias", NULL, "Mic Jack"},
135 };
136
137 /*
138 * Logic for a wm8750 as connected on a Z2 Device
139 */
140 static int z2_wm8750_init(struct snd_soc_pcm_runtime *rtd)
141 {
142 struct snd_soc_codec *codec = rtd->codec;
143 struct snd_soc_dapm_context *dapm = &codec->dapm;
144 int ret;
145
146 /* NC codec pins */
147 snd_soc_dapm_disable_pin(dapm, "LINPUT3");
148 snd_soc_dapm_disable_pin(dapm, "RINPUT3");
149 snd_soc_dapm_disable_pin(dapm, "OUT3");
150 snd_soc_dapm_disable_pin(dapm, "MONO");
151
152 /* Add z2 specific widgets */
153 snd_soc_dapm_new_controls(dapm, wm8750_dapm_widgets,
154 ARRAY_SIZE(wm8750_dapm_widgets));
155
156 /* Set up z2 specific audio paths */
157 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
158
159 ret = snd_soc_dapm_sync(dapm);
160 if (ret)
161 goto err;
162
163 /* Jack detection API stuff */
164 ret = snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
165 &hs_jack);
166 if (ret)
167 goto err;
168
169 ret = snd_soc_jack_add_pins(&hs_jack, ARRAY_SIZE(hs_jack_pins),
170 hs_jack_pins);
171 if (ret)
172 goto err;
173
174 ret = snd_soc_jack_add_gpios(&hs_jack, ARRAY_SIZE(hs_jack_gpios),
175 hs_jack_gpios);
176 if (ret)
177 goto err;
178
179 return 0;
180
181 err:
182 return ret;
183 }
184
185 static struct snd_soc_ops z2_ops = {
186 .hw_params = z2_hw_params,
187 };
188
189 /* z2 digital audio interface glue - connects codec <--> CPU */
190 static struct snd_soc_dai_link z2_dai = {
191 .name = "wm8750",
192 .stream_name = "WM8750",
193 .cpu_dai_name = "pxa2xx-i2s",
194 .codec_dai_name = "wm8750-hifi",
195 .platform_name = "pxa-pcm-audio",
196 .codec_name = "wm8750-codec.0-001a",
197 .init = z2_wm8750_init,
198 .ops = &z2_ops,
199 };
200
201 /* z2 audio machine driver */
202 static struct snd_soc_card snd_soc_z2 = {
203 .name = "Z2",
204 .dai_link = &z2_dai,
205 .num_links = 1,
206 };
207
208 static struct platform_device *z2_snd_device;
209
210 static int __init z2_init(void)
211 {
212 int ret;
213
214 if (!machine_is_zipit2())
215 return -ENODEV;
216
217 z2_snd_device = platform_device_alloc("soc-audio", -1);
218 if (!z2_snd_device)
219 return -ENOMEM;
220
221 platform_set_drvdata(z2_snd_device, &snd_soc_z2);
222 ret = platform_device_add(z2_snd_device);
223
224 if (ret)
225 platform_device_put(z2_snd_device);
226
227 return ret;
228 }
229
230 static void __exit z2_exit(void)
231 {
232 platform_device_unregister(z2_snd_device);
233 }
234
235 module_init(z2_init);
236 module_exit(z2_exit);
237
238 MODULE_AUTHOR("Ken McGuire <kenm@desertweyr.com>, "
239 "Marek Vasut <marek.vasut@gmail.com>");
240 MODULE_DESCRIPTION("ALSA SoC ZipitZ2");
241 MODULE_LICENSE("GPL");
This page took 0.035945 seconds and 5 git commands to generate.