blob: ceb656695b0fbc2f438779ac8331052063d83643 [file] [log] [blame]
Mark Brown2dac9212008-11-21 14:01:41 +00001/*
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>
Mark Brown2c782f52009-01-16 16:35:52 +000017#include <linux/clk.h>
Mark Brown2dac9212008-11-21 14:01:41 +000018#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>
Mark Brown2dac9212008-11-21 14:01:41 +000023
24#include "../codecs/wm9713.h"
Mark Brown2dac9212008-11-21 14:01:41 +000025#include "pxa2xx-ac97.h"
26#include "pxa-ssp.h"
27
Mark Brown2c782f52009-01-16 16:35:52 +000028/*
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 */
33static int clk_pout;
34module_param(clk_pout, int, 0);
35MODULE_PARM_DESC(clk_pout, "Use CLK_POUT as WM9713 MCLK (SW15 on board).");
36
37static struct clk *pout;
38
Mark Brown2dac9212008-11-21 14:01:41 +000039static struct snd_soc_card zylonite;
40
41static 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 */
50static 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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000072static int zylonite_wm9713_init(struct snd_soc_pcm_runtime *rtd)
Mark Brown2dac9212008-11-21 14:01:41 +000073{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000074 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +020075 struct snd_soc_dapm_context *dapm = &codec->dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000076
Mark Brown2c782f52009-01-16 16:35:52 +000077 if (clk_pout)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000078 snd_soc_dai_set_pll(rtd->codec_dai, 0, 0,
Mark Brown85488032009-09-05 18:52:16 +010079 clk_get_rate(pout), 0);
Mark Brown2dac9212008-11-21 14:01:41 +000080
Liam Girdwoodce6120c2010-11-05 15:53:46 +020081 snd_soc_dapm_new_controls(dapm, zylonite_dapm_widgets,
Mark Brown2dac9212008-11-21 14:01:41 +000082 ARRAY_SIZE(zylonite_dapm_widgets));
83
Liam Girdwoodce6120c2010-11-05 15:53:46 +020084 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Mark Brown2dac9212008-11-21 14:01:41 +000085
86 /* Static setup for now */
Liam Girdwoodce6120c2010-11-05 15:53:46 +020087 snd_soc_dapm_enable_pin(dapm, "Headphone");
88 snd_soc_dapm_enable_pin(dapm, "Headset Earpiece");
Mark Brown2dac9212008-11-21 14:01:41 +000089
Mark Brown2dac9212008-11-21 14:01:41 +000090 return 0;
91}
92
93static int zylonite_voice_hw_params(struct snd_pcm_substream *substream,
94 struct snd_pcm_hw_params *params)
95{
96 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000097 struct snd_soc_dai *codec_dai = rtd->codec_dai;
98 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Mark Browna4358692009-01-23 11:49:45 +000099 unsigned int pll_out = 0;
Mark Brown2dac9212008-11-21 14:01:41 +0000100 unsigned int wm9713_div = 0;
101 int ret = 0;
Mark Brown85fab782009-03-13 14:27:08 +0000102 int rate = params_rate(params);
103 int width = snd_pcm_format_physical_width(params_format(params));
Mark Brown2dac9212008-11-21 14:01:41 +0000104
Mark Brown85fab782009-03-13 14:27:08 +0000105 /* Only support ratios that we can generate neatly from the AC97
106 * based master clock - in particular, this excludes 44.1kHz.
107 * In most applications the voice DAC will be used for telephony
108 * data so multiples of 8kHz will be the common case.
109 */
110 switch (rate) {
Mark Brown2dac9212008-11-21 14:01:41 +0000111 case 8000:
112 wm9713_div = 12;
Mark Brown2dac9212008-11-21 14:01:41 +0000113 break;
114 case 16000:
115 wm9713_div = 6;
Mark Brown2dac9212008-11-21 14:01:41 +0000116 break;
117 case 48000:
Mark Brown2dac9212008-11-21 14:01:41 +0000118 wm9713_div = 2;
Mark Brown2dac9212008-11-21 14:01:41 +0000119 break;
Mark Brown85fab782009-03-13 14:27:08 +0000120 default:
121 /* Don't support OSS emulation */
122 return -EINVAL;
Mark Brown2dac9212008-11-21 14:01:41 +0000123 }
124
Mark Brown85fab782009-03-13 14:27:08 +0000125 /* Add 1 to the width for the leading clock cycle */
126 pll_out = rate * (width + 1) * 8;
Mark Brown2dac9212008-11-21 14:01:41 +0000127
Mark Brown85fab782009-03-13 14:27:08 +0000128 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0, 1);
Mark Brown2dac9212008-11-21 14:01:41 +0000129 if (ret < 0)
130 return ret;
131
Mark Brown85488032009-09-05 18:52:16 +0100132 ret = snd_soc_dai_set_pll(cpu_dai, 0, 0, 0, pll_out);
Mark Browna4358692009-01-23 11:49:45 +0000133 if (ret < 0)
134 return ret;
135
Mark Brown2c782f52009-01-16 16:35:52 +0000136 if (clk_pout)
137 ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_PLL_DIV,
138 WM9713_PCMDIV(wm9713_div));
139 else
140 ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_DIV,
141 WM9713_PCMDIV(wm9713_div));
Mark Brown2dac9212008-11-21 14:01:41 +0000142 if (ret < 0)
143 return ret;
144
Mark Brown85fab782009-03-13 14:27:08 +0000145 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
146 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
147 if (ret < 0)
148 return ret;
149
150 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
151 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
152 if (ret < 0)
153 return ret;
154
Mark Brown2dac9212008-11-21 14:01:41 +0000155 return 0;
156}
157
158static struct snd_soc_ops zylonite_voice_ops = {
159 .hw_params = zylonite_voice_hw_params,
160};
161
162static struct snd_soc_dai_link zylonite_dai[] = {
163{
164 .name = "AC97",
165 .stream_name = "AC97 HiFi",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000166 .codec_name = "wm9713-codec",
167 .platform_name = "pxa-pcm-audio",
Dmitry Eremin-Solenikov4bfc4e22011-02-23 02:29:11 +0300168 .cpu_dai_name = "pxa2xx-ac97",
Antonio Ospiteefd69472011-03-18 12:47:33 +0100169 .codec_dai_name = "wm9713-hifi",
Mark Brown2dac9212008-11-21 14:01:41 +0000170 .init = zylonite_wm9713_init,
171},
172{
173 .name = "AC97 Aux",
174 .stream_name = "AC97 Aux",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000175 .codec_name = "wm9713-codec",
176 .platform_name = "pxa-pcm-audio",
Dmitry Eremin-Solenikov4bfc4e22011-02-23 02:29:11 +0300177 .cpu_dai_name = "pxa2xx-ac97-aux",
Antonio Ospiteefd69472011-03-18 12:47:33 +0100178 .codec_dai_name = "wm9713-aux",
Mark Brown2dac9212008-11-21 14:01:41 +0000179},
180{
181 .name = "WM9713 Voice",
182 .stream_name = "WM9713 Voice",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000183 .codec_name = "wm9713-codec",
184 .platform_name = "pxa-pcm-audio",
185 .cpu_dai_name = "pxa-ssp-dai.2",
Antonio Ospiteefd69472011-03-18 12:47:33 +0100186 .codec_dai_name = "wm9713-voice",
Mark Brown2dac9212008-11-21 14:01:41 +0000187 .ops = &zylonite_voice_ops,
188},
189};
190
Mark Browne7361ec2011-01-26 14:17:20 +0000191static int zylonite_probe(struct snd_soc_card *card)
Mark Brown2c782f52009-01-16 16:35:52 +0000192{
193 int ret;
194
195 if (clk_pout) {
196 pout = clk_get(NULL, "CLK_POUT");
197 if (IS_ERR(pout)) {
Arnd Bergmanneff919a2011-10-01 22:03:34 +0200198 dev_err(card->dev, "Unable to obtain CLK_POUT: %ld\n",
Mark Brown2c782f52009-01-16 16:35:52 +0000199 PTR_ERR(pout));
200 return PTR_ERR(pout);
201 }
202
203 ret = clk_enable(pout);
204 if (ret != 0) {
Arnd Bergmanneff919a2011-10-01 22:03:34 +0200205 dev_err(card->dev, "Unable to enable CLK_POUT: %d\n",
Mark Brown2c782f52009-01-16 16:35:52 +0000206 ret);
207 clk_put(pout);
208 return ret;
209 }
210
Arnd Bergmanneff919a2011-10-01 22:03:34 +0200211 dev_dbg(card->dev, "MCLK enabled at %luHz\n",
Mark Brown2c782f52009-01-16 16:35:52 +0000212 clk_get_rate(pout));
213 }
214
215 return 0;
216}
217
Mark Browne7361ec2011-01-26 14:17:20 +0000218static int zylonite_remove(struct snd_soc_card *card)
Mark Brown2c782f52009-01-16 16:35:52 +0000219{
220 if (clk_pout) {
221 clk_disable(pout);
222 clk_put(pout);
223 }
224
225 return 0;
226}
227
Mark Brown70b2ac12011-01-26 14:05:25 +0000228static int zylonite_suspend_post(struct snd_soc_card *card)
Mark Brown2c782f52009-01-16 16:35:52 +0000229{
230 if (clk_pout)
231 clk_disable(pout);
232
233 return 0;
234}
235
Mark Brown70b2ac12011-01-26 14:05:25 +0000236static int zylonite_resume_pre(struct snd_soc_card *card)
Mark Brown2c782f52009-01-16 16:35:52 +0000237{
238 int ret = 0;
239
240 if (clk_pout) {
241 ret = clk_enable(pout);
242 if (ret != 0)
Arnd Bergmanneff919a2011-10-01 22:03:34 +0200243 dev_err(card->dev, "Unable to enable CLK_POUT: %d\n",
Mark Brown2c782f52009-01-16 16:35:52 +0000244 ret);
245 }
246
247 return ret;
248}
249
Mark Brown2dac9212008-11-21 14:01:41 +0000250static struct snd_soc_card zylonite = {
251 .name = "Zylonite",
Axel Lin561c6a12011-12-22 09:44:43 +0800252 .owner = THIS_MODULE,
Mark Brown2c782f52009-01-16 16:35:52 +0000253 .probe = &zylonite_probe,
254 .remove = &zylonite_remove,
255 .suspend_post = &zylonite_suspend_post,
256 .resume_pre = &zylonite_resume_pre,
Mark Brown2dac9212008-11-21 14:01:41 +0000257 .dai_link = zylonite_dai,
258 .num_links = ARRAY_SIZE(zylonite_dai),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000259 .owner = THIS_MODULE,
Mark Brown2dac9212008-11-21 14:01:41 +0000260};
261
262static struct platform_device *zylonite_snd_ac97_device;
263
264static int __init zylonite_init(void)
265{
266 int ret;
267
268 zylonite_snd_ac97_device = platform_device_alloc("soc-audio", -1);
269 if (!zylonite_snd_ac97_device)
270 return -ENOMEM;
271
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000272 platform_set_drvdata(zylonite_snd_ac97_device, &zylonite);
Mark Brown2dac9212008-11-21 14:01:41 +0000273
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
281static void __exit zylonite_exit(void)
282{
283 platform_device_unregister(zylonite_snd_ac97_device);
284}
285
286module_init(zylonite_init);
287module_exit(zylonite_exit);
288
289MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
290MODULE_DESCRIPTION("ALSA SoC WM9713 Zylonite");
291MODULE_LICENSE("GPL");