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