8541b679f6ebf367db9d1ee7f36433f81e1c84d8
[pandora-kernel.git] / 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 acds = 0;
99         unsigned int wm9713_div = 0;
100         int ret = 0;
101
102         switch (params_rate(params)) {
103         case 8000:
104                 wm9713_div = 12;
105                 break;
106         case 16000:
107                 wm9713_div = 6;
108                 break;
109         case 48000:
110         default:
111                 wm9713_div = 2;
112                 acds = 1;
113                 break;
114         }
115
116         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
117                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
118         if (ret < 0)
119                 return ret;
120
121         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
122                 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
123         if (ret < 0)
124                 return ret;
125
126         ret = snd_soc_dai_set_tdm_slot(cpu_dai,
127                                        params_channels(params),
128                                        params_channels(params));
129         if (ret < 0)
130                 return ret;
131
132         ret = snd_soc_dai_set_clkdiv(cpu_dai, PXA_SSP_AUDIO_DIV_ACDS, acds);
133         if (ret < 0)
134                 return ret;
135
136         ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0, 1);
137         if (ret < 0)
138                 return ret;
139
140         if (clk_pout)
141                 ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_PLL_DIV,
142                                              WM9713_PCMDIV(wm9713_div));
143         else
144                 ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_DIV,
145                                              WM9713_PCMDIV(wm9713_div));
146         if (ret < 0)
147                 return ret;
148
149         return 0;
150 }
151
152 static struct snd_soc_ops zylonite_voice_ops = {
153         .hw_params = zylonite_voice_hw_params,
154 };
155
156 static struct snd_soc_dai_link zylonite_dai[] = {
157 {
158         .name = "AC97",
159         .stream_name = "AC97 HiFi",
160         .cpu_dai = &pxa_ac97_dai[PXA2XX_DAI_AC97_HIFI],
161         .codec_dai = &wm9713_dai[WM9713_DAI_AC97_HIFI],
162         .init = zylonite_wm9713_init,
163 },
164 {
165         .name = "AC97 Aux",
166         .stream_name = "AC97 Aux",
167         .cpu_dai = &pxa_ac97_dai[PXA2XX_DAI_AC97_AUX],
168         .codec_dai = &wm9713_dai[WM9713_DAI_AC97_AUX],
169 },
170 {
171         .name = "WM9713 Voice",
172         .stream_name = "WM9713 Voice",
173         .cpu_dai = &pxa_ssp_dai[PXA_DAI_SSP3],
174         .codec_dai = &wm9713_dai[WM9713_DAI_PCM_VOICE],
175         .ops = &zylonite_voice_ops,
176 },
177 };
178
179 static int zylonite_probe(struct platform_device *pdev)
180 {
181         int ret;
182
183         if (clk_pout) {
184                 pout = clk_get(NULL, "CLK_POUT");
185                 if (IS_ERR(pout)) {
186                         dev_err(&pdev->dev, "Unable to obtain CLK_POUT: %ld\n",
187                                 PTR_ERR(pout));
188                         return PTR_ERR(pout);
189                 }
190
191                 ret = clk_enable(pout);
192                 if (ret != 0) {
193                         dev_err(&pdev->dev, "Unable to enable CLK_POUT: %d\n",
194                                 ret);
195                         clk_put(pout);
196                         return ret;
197                 }
198
199                 dev_dbg(&pdev->dev, "MCLK enabled at %luHz\n",
200                         clk_get_rate(pout));
201         }
202
203         return 0;
204 }
205
206 static int zylonite_remove(struct platform_device *pdev)
207 {
208         if (clk_pout) {
209                 clk_disable(pout);
210                 clk_put(pout);
211         }
212
213         return 0;
214 }
215
216 static int zylonite_suspend_post(struct platform_device *pdev,
217                                  pm_message_t state)
218 {
219         if (clk_pout)
220                 clk_disable(pout);
221
222         return 0;
223 }
224
225 static int zylonite_resume_pre(struct platform_device *pdev)
226 {
227         int ret = 0;
228
229         if (clk_pout) {
230                 ret = clk_enable(pout);
231                 if (ret != 0)
232                         dev_err(&pdev->dev, "Unable to enable CLK_POUT: %d\n",
233                                 ret);
234         }
235
236         return ret;
237 }
238
239 static struct snd_soc_card zylonite = {
240         .name = "Zylonite",
241         .probe = &zylonite_probe,
242         .remove = &zylonite_remove,
243         .suspend_post = &zylonite_suspend_post,
244         .resume_pre = &zylonite_resume_pre,
245         .platform = &pxa2xx_soc_platform,
246         .dai_link = zylonite_dai,
247         .num_links = ARRAY_SIZE(zylonite_dai),
248 };
249
250 static struct snd_soc_device zylonite_snd_ac97_devdata = {
251         .card = &zylonite,
252         .codec_dev = &soc_codec_dev_wm9713,
253 };
254
255 static struct platform_device *zylonite_snd_ac97_device;
256
257 static int __init zylonite_init(void)
258 {
259         int ret;
260
261         zylonite_snd_ac97_device = platform_device_alloc("soc-audio", -1);
262         if (!zylonite_snd_ac97_device)
263                 return -ENOMEM;
264
265         platform_set_drvdata(zylonite_snd_ac97_device,
266                              &zylonite_snd_ac97_devdata);
267         zylonite_snd_ac97_devdata.dev = &zylonite_snd_ac97_device->dev;
268
269         ret = platform_device_add(zylonite_snd_ac97_device);
270         if (ret != 0)
271                 platform_device_put(zylonite_snd_ac97_device);
272
273         return ret;
274 }
275
276 static void __exit zylonite_exit(void)
277 {
278         platform_device_unregister(zylonite_snd_ac97_device);
279 }
280
281 module_init(zylonite_init);
282 module_exit(zylonite_exit);
283
284 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
285 MODULE_DESCRIPTION("ALSA SoC WM9713 Zylonite");
286 MODULE_LICENSE("GPL");