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