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