Commit | Line | Data |
---|---|---|
2dac9217 MB |
1 | /* |
2 | * zylonite.c -- SoC audio for Zylonite | |
3 | * | |
4 | * Copyright 2008 Wolfson Microelectronics PLC. | |
5 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License as | |
9 | * published by the Free Software Foundation; either version 2 of the | |
10 | * License, or (at your option) any later version. | |
11 | * | |
12 | */ | |
13 | ||
14 | #include <linux/module.h> | |
15 | #include <linux/moduleparam.h> | |
16 | #include <linux/device.h> | |
2c782f59 | 17 | #include <linux/clk.h> |
2dac9217 MB |
18 | #include <linux/i2c.h> |
19 | #include <sound/core.h> | |
20 | #include <sound/pcm.h> | |
21 | #include <sound/pcm_params.h> | |
22 | #include <sound/soc.h> | |
23 | #include <sound/soc-dapm.h> | |
24 | ||
25 | #include "../codecs/wm9713.h" | |
2dac9217 MB |
26 | #include "pxa2xx-ac97.h" |
27 | #include "pxa-ssp.h" | |
28 | ||
2c782f59 MB |
29 | /* |
30 | * There is a physical switch SW15 on the board which changes the MCLK | |
31 | * for the WM9713 between the standard AC97 master clock and the | |
32 | * output of the CLK_POUT signal from the PXA. | |
33 | */ | |
34 | static int clk_pout; | |
35 | module_param(clk_pout, int, 0); | |
36 | MODULE_PARM_DESC(clk_pout, "Use CLK_POUT as WM9713 MCLK (SW15 on board)."); | |
37 | ||
38 | static struct clk *pout; | |
39 | ||
2dac9217 MB |
40 | static struct snd_soc_card zylonite; |
41 | ||
42 | static const struct snd_soc_dapm_widget zylonite_dapm_widgets[] = { | |
43 | SND_SOC_DAPM_HP("Headphone", NULL), | |
44 | SND_SOC_DAPM_MIC("Headset Microphone", NULL), | |
45 | SND_SOC_DAPM_MIC("Handset Microphone", NULL), | |
46 | SND_SOC_DAPM_SPK("Multiactor", NULL), | |
47 | SND_SOC_DAPM_SPK("Headset Earpiece", NULL), | |
48 | }; | |
49 | ||
50 | /* Currently supported audio map */ | |
51 | static const struct snd_soc_dapm_route audio_map[] = { | |
52 | ||
53 | /* Headphone output connected to HPL/HPR */ | |
54 | { "Headphone", NULL, "HPL" }, | |
55 | { "Headphone", NULL, "HPR" }, | |
56 | ||
57 | /* On-board earpiece */ | |
58 | { "Headset Earpiece", NULL, "OUT3" }, | |
59 | ||
60 | /* Headphone mic */ | |
61 | { "MIC2A", NULL, "Mic Bias" }, | |
62 | { "Mic Bias", NULL, "Headset Microphone" }, | |
63 | ||
64 | /* On-board mic */ | |
65 | { "MIC1", NULL, "Mic Bias" }, | |
66 | { "Mic Bias", NULL, "Handset Microphone" }, | |
67 | ||
68 | /* Multiactor differentially connected over SPKL/SPKR */ | |
69 | { "Multiactor", NULL, "SPKL" }, | |
70 | { "Multiactor", NULL, "SPKR" }, | |
71 | }; | |
72 | ||
f0fba2ad | 73 | static int zylonite_wm9713_init(struct snd_soc_pcm_runtime *rtd) |
2dac9217 | 74 | { |
f0fba2ad LG |
75 | struct snd_soc_codec *codec = rtd->codec; |
76 | ||
2c782f59 | 77 | if (clk_pout) |
f0fba2ad | 78 | snd_soc_dai_set_pll(rtd->codec_dai, 0, 0, |
85488037 | 79 | clk_get_rate(pout), 0); |
2dac9217 MB |
80 | |
81 | snd_soc_dapm_new_controls(codec, zylonite_dapm_widgets, | |
82 | ARRAY_SIZE(zylonite_dapm_widgets)); | |
83 | ||
84 | snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); | |
85 | ||
86 | /* Static setup for now */ | |
87 | snd_soc_dapm_enable_pin(codec, "Headphone"); | |
88 | snd_soc_dapm_enable_pin(codec, "Headset Earpiece"); | |
89 | ||
90 | snd_soc_dapm_sync(codec); | |
91 | return 0; | |
92 | } | |
93 | ||
94 | static int zylonite_voice_hw_params(struct snd_pcm_substream *substream, | |
95 | struct snd_pcm_hw_params *params) | |
96 | { | |
97 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad LG |
98 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
99 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
a435869c | 100 | unsigned int pll_out = 0; |
2dac9217 MB |
101 | unsigned int wm9713_div = 0; |
102 | int ret = 0; | |
85fab780 MB |
103 | int rate = params_rate(params); |
104 | int width = snd_pcm_format_physical_width(params_format(params)); | |
105 | ||
106 | /* Only support ratios that we can generate neatly from the AC97 | |
107 | * based master clock - in particular, this excludes 44.1kHz. | |
108 | * In most applications the voice DAC will be used for telephony | |
109 | * data so multiples of 8kHz will be the common case. | |
110 | */ | |
111 | switch (rate) { | |
2dac9217 MB |
112 | case 8000: |
113 | wm9713_div = 12; | |
2dac9217 MB |
114 | break; |
115 | case 16000: | |
116 | wm9713_div = 6; | |
2dac9217 MB |
117 | break; |
118 | case 48000: | |
2dac9217 | 119 | wm9713_div = 2; |
2dac9217 | 120 | break; |
85fab780 MB |
121 | default: |
122 | /* Don't support OSS emulation */ | |
123 | return -EINVAL; | |
2dac9217 MB |
124 | } |
125 | ||
85fab780 MB |
126 | /* Add 1 to the width for the leading clock cycle */ |
127 | pll_out = rate * (width + 1) * 8; | |
2dac9217 | 128 | |
85fab780 | 129 | ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0, 1); |
2dac9217 MB |
130 | if (ret < 0) |
131 | return ret; | |
132 | ||
85488037 | 133 | ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, pll_out); |
a435869c MB |
134 | if (ret < 0) |
135 | return ret; | |
136 | ||
2c782f59 MB |
137 | if (clk_pout) |
138 | ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_PLL_DIV, | |
139 | WM9713_PCMDIV(wm9713_div)); | |
140 | else | |
141 | ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_DIV, | |
142 | WM9713_PCMDIV(wm9713_div)); | |
2dac9217 MB |
143 | if (ret < 0) |
144 | return ret; | |
145 | ||
85fab780 MB |
146 | ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S | |
147 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); | |
148 | if (ret < 0) | |
149 | return ret; | |
150 | ||
151 | ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S | | |
152 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS); | |
153 | if (ret < 0) | |
154 | return ret; | |
155 | ||
2dac9217 MB |
156 | return 0; |
157 | } | |
158 | ||
159 | static struct snd_soc_ops zylonite_voice_ops = { | |
160 | .hw_params = zylonite_voice_hw_params, | |
161 | }; | |
162 | ||
163 | static struct snd_soc_dai_link zylonite_dai[] = { | |
164 | { | |
165 | .name = "AC97", | |
166 | .stream_name = "AC97 HiFi", | |
f0fba2ad LG |
167 | .codec_name = "wm9713-codec", |
168 | .platform_name = "pxa-pcm-audio", | |
169 | .cpu_dai_name = "pxa-ac97.0", | |
170 | .codec_name = "wm9713-hifi", | |
2dac9217 MB |
171 | .init = zylonite_wm9713_init, |
172 | }, | |
173 | { | |
174 | .name = "AC97 Aux", | |
175 | .stream_name = "AC97 Aux", | |
f0fba2ad LG |
176 | .codec_name = "wm9713-codec", |
177 | .platform_name = "pxa-pcm-audio", | |
178 | .cpu_dai_name = "pxa-ac97.1", | |
179 | .codec_name = "wm9713-aux", | |
2dac9217 MB |
180 | }, |
181 | { | |
182 | .name = "WM9713 Voice", | |
183 | .stream_name = "WM9713 Voice", | |
f0fba2ad LG |
184 | .codec_name = "wm9713-codec", |
185 | .platform_name = "pxa-pcm-audio", | |
186 | .cpu_dai_name = "pxa-ssp-dai.2", | |
187 | .codec_name = "wm9713-voice", | |
2dac9217 MB |
188 | .ops = &zylonite_voice_ops, |
189 | }, | |
190 | }; | |
191 | ||
2c782f59 MB |
192 | static int zylonite_probe(struct platform_device *pdev) |
193 | { | |
194 | int ret; | |
195 | ||
196 | if (clk_pout) { | |
197 | pout = clk_get(NULL, "CLK_POUT"); | |
198 | if (IS_ERR(pout)) { | |
199 | dev_err(&pdev->dev, "Unable to obtain CLK_POUT: %ld\n", | |
200 | PTR_ERR(pout)); | |
201 | return PTR_ERR(pout); | |
202 | } | |
203 | ||
204 | ret = clk_enable(pout); | |
205 | if (ret != 0) { | |
206 | dev_err(&pdev->dev, "Unable to enable CLK_POUT: %d\n", | |
207 | ret); | |
208 | clk_put(pout); | |
209 | return ret; | |
210 | } | |
211 | ||
212 | dev_dbg(&pdev->dev, "MCLK enabled at %luHz\n", | |
213 | clk_get_rate(pout)); | |
214 | } | |
215 | ||
216 | return 0; | |
217 | } | |
218 | ||
219 | static int zylonite_remove(struct platform_device *pdev) | |
220 | { | |
221 | if (clk_pout) { | |
222 | clk_disable(pout); | |
223 | clk_put(pout); | |
224 | } | |
225 | ||
226 | return 0; | |
227 | } | |
228 | ||
229 | static int zylonite_suspend_post(struct platform_device *pdev, | |
230 | pm_message_t state) | |
231 | { | |
232 | if (clk_pout) | |
233 | clk_disable(pout); | |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
238 | static int zylonite_resume_pre(struct platform_device *pdev) | |
239 | { | |
240 | int ret = 0; | |
241 | ||
242 | if (clk_pout) { | |
243 | ret = clk_enable(pout); | |
244 | if (ret != 0) | |
245 | dev_err(&pdev->dev, "Unable to enable CLK_POUT: %d\n", | |
246 | ret); | |
247 | } | |
248 | ||
249 | return ret; | |
250 | } | |
251 | ||
2dac9217 MB |
252 | static struct snd_soc_card zylonite = { |
253 | .name = "Zylonite", | |
2c782f59 MB |
254 | .probe = &zylonite_probe, |
255 | .remove = &zylonite_remove, | |
256 | .suspend_post = &zylonite_suspend_post, | |
257 | .resume_pre = &zylonite_resume_pre, | |
2dac9217 MB |
258 | .dai_link = zylonite_dai, |
259 | .num_links = ARRAY_SIZE(zylonite_dai), | |
f0fba2ad | 260 | .owner = THIS_MODULE, |
2dac9217 MB |
261 | }; |
262 | ||
263 | static struct platform_device *zylonite_snd_ac97_device; | |
264 | ||
265 | static int __init zylonite_init(void) | |
266 | { | |
267 | int ret; | |
268 | ||
269 | zylonite_snd_ac97_device = platform_device_alloc("soc-audio", -1); | |
270 | if (!zylonite_snd_ac97_device) | |
271 | return -ENOMEM; | |
272 | ||
f0fba2ad | 273 | platform_set_drvdata(zylonite_snd_ac97_device, &zylonite); |
2dac9217 MB |
274 | |
275 | ret = platform_device_add(zylonite_snd_ac97_device); | |
276 | if (ret != 0) | |
277 | platform_device_put(zylonite_snd_ac97_device); | |
278 | ||
279 | return ret; | |
280 | } | |
281 | ||
282 | static void __exit zylonite_exit(void) | |
283 | { | |
284 | platform_device_unregister(zylonite_snd_ac97_device); | |
285 | } | |
286 | ||
287 | module_init(zylonite_init); | |
288 | module_exit(zylonite_exit); | |
289 | ||
290 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); | |
291 | MODULE_DESCRIPTION("ALSA SoC WM9713 Zylonite"); | |
292 | MODULE_LICENSE("GPL"); |