blob: c82c646b8a0897308c8288b96f62f5b4df10a801 [file] [log] [blame]
Mark Brown0a590b12011-11-28 22:05:41 +00001/*
2 * Littlemill audio support
3 *
4 * Copyright 2011 Wolfson Microelectronics
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 */
11
12#include <sound/soc.h>
13#include <sound/soc-dapm.h>
14#include <sound/jack.h>
15#include <linux/gpio.h>
16#include <linux/module.h>
17
18#include "../codecs/wm8994.h"
19
20static int sample_rate = 44100;
21
22static int littlemill_set_bias_level(struct snd_soc_card *card,
23 struct snd_soc_dapm_context *dapm,
24 enum snd_soc_bias_level level)
25{
Mark Brown0cbe4b32012-04-18 18:20:05 +010026 struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
Mark Brown0a590b12011-11-28 22:05:41 +000027 int ret;
28
Mark Brown0cbe4b32012-04-18 18:20:05 +010029 if (dapm->dev != aif1_dai->dev)
Mark Brown0a590b12011-11-28 22:05:41 +000030 return 0;
31
32 switch (level) {
33 case SND_SOC_BIAS_PREPARE:
34 /*
35 * If we've not already clocked things via hw_params()
36 * then do so now, otherwise these are noops.
37 */
38 if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
Mark Brown0cbe4b32012-04-18 18:20:05 +010039 ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
Mark Brown0a590b12011-11-28 22:05:41 +000040 WM8994_FLL_SRC_MCLK2, 32768,
41 sample_rate * 512);
42 if (ret < 0) {
43 pr_err("Failed to start FLL: %d\n", ret);
44 return ret;
45 }
46
Mark Brown0cbe4b32012-04-18 18:20:05 +010047 ret = snd_soc_dai_set_sysclk(aif1_dai,
Mark Brown0a590b12011-11-28 22:05:41 +000048 WM8994_SYSCLK_FLL1,
49 sample_rate * 512,
50 SND_SOC_CLOCK_IN);
51 if (ret < 0) {
52 pr_err("Failed to set SYSCLK: %d\n", ret);
53 return ret;
54 }
55 }
56 break;
57
58 default:
59 break;
60 }
61
62 return 0;
63}
64
65static int littlemill_set_bias_level_post(struct snd_soc_card *card,
66 struct snd_soc_dapm_context *dapm,
67 enum snd_soc_bias_level level)
68{
Mark Brown0cbe4b32012-04-18 18:20:05 +010069 struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
Mark Brown0a590b12011-11-28 22:05:41 +000070 int ret;
71
Mark Brown0cbe4b32012-04-18 18:20:05 +010072 if (dapm->dev != aif1_dai->dev)
Mark Brown0a590b12011-11-28 22:05:41 +000073 return 0;
74
75 switch (level) {
76 case SND_SOC_BIAS_STANDBY:
Mark Brown0cbe4b32012-04-18 18:20:05 +010077 ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
78 32768, SND_SOC_CLOCK_IN);
79 if (ret < 0) {
80 pr_err("Failed to switch away from FLL1: %d\n", ret);
81 return ret;
82 }
83
84 ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
85 0, 0, 0);
86 if (ret < 0) {
87 pr_err("Failed to stop FLL1: %d\n", ret);
88 return ret;
89 }
90 break;
91
Mark Brown0a590b12011-11-28 22:05:41 +000092 default:
93 break;
94 }
95
96 dapm->bias_level = level;
97
98 return 0;
99}
100
101static int littlemill_hw_params(struct snd_pcm_substream *substream,
102 struct snd_pcm_hw_params *params)
103{
104 struct snd_soc_pcm_runtime *rtd = substream->private_data;
105 struct snd_soc_dai *codec_dai = rtd->codec_dai;
106 int ret;
107
108 sample_rate = params_rate(params);
109
110 ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1,
111 WM8994_FLL_SRC_MCLK2, 32768,
112 sample_rate * 512);
113 if (ret < 0) {
114 pr_err("Failed to start FLL: %d\n", ret);
115 return ret;
116 }
117
118 ret = snd_soc_dai_set_sysclk(codec_dai,
119 WM8994_SYSCLK_FLL1,
120 sample_rate * 512,
121 SND_SOC_CLOCK_IN);
122 if (ret < 0) {
123 pr_err("Failed to set SYSCLK: %d\n", ret);
124 return ret;
125 }
126
127 return 0;
128}
129
130static struct snd_soc_ops littlemill_ops = {
131 .hw_params = littlemill_hw_params,
132};
133
Mark Brown0cbe4b32012-04-18 18:20:05 +0100134static const struct snd_soc_pcm_stream baseband_params = {
135 .formats = SNDRV_PCM_FMTBIT_S32_LE,
136 .rate_min = 8000,
137 .rate_max = 8000,
138 .channels_min = 2,
139 .channels_max = 2,
140};
141
Mark Brown0a590b12011-11-28 22:05:41 +0000142static struct snd_soc_dai_link littlemill_dai[] = {
143 {
144 .name = "CPU",
145 .stream_name = "CPU",
146 .cpu_dai_name = "samsung-i2s.0",
147 .codec_dai_name = "wm8994-aif1",
148 .platform_name = "samsung-audio",
149 .codec_name = "wm8994-codec",
150 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
151 | SND_SOC_DAIFMT_CBM_CFM,
152 .ops = &littlemill_ops,
153 },
Mark Brown0cbe4b32012-04-18 18:20:05 +0100154 {
155 .name = "Baseband",
156 .stream_name = "Baseband",
157 .cpu_dai_name = "wm8994-aif2",
158 .codec_dai_name = "wm1250-ev1",
159 .codec_name = "wm1250-ev1.1-0027",
160 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
161 | SND_SOC_DAIFMT_CBM_CFM,
162 .ignore_suspend = 1,
163 .params = &baseband_params,
164 },
Mark Brown0a590b12011-11-28 22:05:41 +0000165};
166
Mark Brown68a02db2012-05-12 12:35:44 +0100167static int bbclk_ev(struct snd_soc_dapm_widget *w,
168 struct snd_kcontrol *kcontrol, int event)
169{
170 struct snd_soc_card *card = w->dapm->card;
171 struct snd_soc_dai *aif2_dai = card->rtd[1].cpu_dai;
172 int ret;
173
174 switch (event) {
175 case SND_SOC_DAPM_PRE_PMU:
176 ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
177 WM8994_FLL_SRC_BCLK, 64 * 8000,
178 8000 * 256);
179 if (ret < 0) {
180 pr_err("Failed to start FLL: %d\n", ret);
181 return ret;
182 }
183
184 ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_FLL2,
185 8000 * 256,
186 SND_SOC_CLOCK_IN);
187 if (ret < 0) {
188 pr_err("Failed to set SYSCLK: %d\n", ret);
189 return ret;
190 }
191 break;
192 case SND_SOC_DAPM_POST_PMD:
193 ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
194 32768, SND_SOC_CLOCK_IN);
195 if (ret < 0) {
196 pr_err("Failed to switch away from FLL2: %d\n", ret);
197 return ret;
198 }
199
200 ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
201 0, 0, 0);
202 if (ret < 0) {
203 pr_err("Failed to stop FLL2: %d\n", ret);
204 return ret;
205 }
206 break;
207 default:
208 return -EINVAL;
209 }
210
211 return 0;
212}
213
Mark Brown0a590b12011-11-28 22:05:41 +0000214static struct snd_soc_dapm_widget widgets[] = {
215 SND_SOC_DAPM_HP("Headphone", NULL),
Mark Brown8f103162011-12-02 17:36:06 +0000216
217 SND_SOC_DAPM_MIC("AMIC", NULL),
218 SND_SOC_DAPM_MIC("DMIC", NULL),
Mark Brown68a02db2012-05-12 12:35:44 +0100219
220 SND_SOC_DAPM_SUPPLY_S("Baseband Clock", -1, SND_SOC_NOPM, 0, 0,
221 bbclk_ev,
222 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
Mark Brown0a590b12011-11-28 22:05:41 +0000223};
224
225static struct snd_soc_dapm_route audio_paths[] = {
226 { "Headphone", NULL, "HPOUT1L" },
227 { "Headphone", NULL, "HPOUT1R" },
Mark Brown8f103162011-12-02 17:36:06 +0000228
229 { "AMIC", NULL, "MICBIAS1" }, /* Default for AMICBIAS jumper */
230 { "IN1LN", NULL, "AMIC" },
231
232 { "DMIC", NULL, "MICBIAS2" }, /* Default for DMICBIAS jumper */
233 { "DMIC1DAT", NULL, "DMIC" },
234 { "DMIC2DAT", NULL, "DMIC" },
Mark Brown68a02db2012-05-12 12:35:44 +0100235
236 { "AIF2CLK", NULL, "Baseband Clock" },
Mark Brown0a590b12011-11-28 22:05:41 +0000237};
238
Mark Brownf2e20262011-12-02 15:55:52 +0000239static struct snd_soc_jack littlemill_headset;
240
Mark Brown0a590b12011-11-28 22:05:41 +0000241static int littlemill_late_probe(struct snd_soc_card *card)
242{
Mark Brownf2e20262011-12-02 15:55:52 +0000243 struct snd_soc_codec *codec = card->rtd[0].codec;
Mark Brown0cbe4b32012-04-18 18:20:05 +0100244 struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
245 struct snd_soc_dai *aif2_dai = card->rtd[1].cpu_dai;
Mark Brown0a590b12011-11-28 22:05:41 +0000246 int ret;
247
Mark Brown0cbe4b32012-04-18 18:20:05 +0100248 ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
249 32768, SND_SOC_CLOCK_IN);
250 if (ret < 0)
251 return ret;
252
253 ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
Mark Brown0a590b12011-11-28 22:05:41 +0000254 32768, SND_SOC_CLOCK_IN);
255 if (ret < 0)
256 return ret;
257
Mark Brownf2e20262011-12-02 15:55:52 +0000258 ret = snd_soc_jack_new(codec, "Headset",
259 SND_JACK_HEADSET | SND_JACK_MECHANICAL |
260 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
261 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
262 SND_JACK_BTN_4 | SND_JACK_BTN_5,
263 &littlemill_headset);
264 if (ret)
265 return ret;
266
267 /* This will check device compatibility itself */
268 wm8958_mic_detect(codec, &littlemill_headset, NULL, NULL);
269
Mark Brown654a28c2012-02-03 13:15:18 +0000270 /* As will this */
271 wm8994_mic_detect(codec, &littlemill_headset, 1);
272
Mark Brown0a590b12011-11-28 22:05:41 +0000273 return 0;
274}
275
276static struct snd_soc_card littlemill = {
277 .name = "Littlemill",
Axel Lin095d79d2011-12-22 10:53:15 +0800278 .owner = THIS_MODULE,
Mark Brown0a590b12011-11-28 22:05:41 +0000279 .dai_link = littlemill_dai,
280 .num_links = ARRAY_SIZE(littlemill_dai),
281
282 .set_bias_level = littlemill_set_bias_level,
283 .set_bias_level_post = littlemill_set_bias_level_post,
284
285 .dapm_widgets = widgets,
286 .num_dapm_widgets = ARRAY_SIZE(widgets),
287 .dapm_routes = audio_paths,
288 .num_dapm_routes = ARRAY_SIZE(audio_paths),
289
290 .late_probe = littlemill_late_probe,
291};
292
293static __devinit int littlemill_probe(struct platform_device *pdev)
294{
295 struct snd_soc_card *card = &littlemill;
296 int ret;
297
298 card->dev = &pdev->dev;
299
300 ret = snd_soc_register_card(card);
301 if (ret) {
302 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
303 ret);
304 return ret;
305 }
306
307 return 0;
308}
309
310static int __devexit littlemill_remove(struct platform_device *pdev)
311{
312 struct snd_soc_card *card = platform_get_drvdata(pdev);
313
314 snd_soc_unregister_card(card);
315
316 return 0;
317}
318
319static struct platform_driver littlemill_driver = {
320 .driver = {
321 .name = "littlemill",
322 .owner = THIS_MODULE,
323 .pm = &snd_soc_pm_ops,
324 },
325 .probe = littlemill_probe,
326 .remove = __devexit_p(littlemill_remove),
327};
328
329module_platform_driver(littlemill_driver);
330
331MODULE_DESCRIPTION("Littlemill audio support");
332MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
333MODULE_LICENSE("GPL");
334MODULE_ALIAS("platform:littlemill");