blob: b9e213f6cc0630f179d9b811d6b382dac6c64777 [file] [log] [blame]
Mark Brown9b8dc662011-04-12 17:24:39 -07001/*
2 * Speyside 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>
Mark Brown68688e72011-04-12 00:00:36 -070014#include <sound/jack.h>
15#include <linux/gpio.h>
Mark Brown9b8dc662011-04-12 17:24:39 -070016
Mark Browna9ba6152011-06-24 12:10:44 +010017#include "../codecs/wm8996.h"
Mark Brownea3e98e2011-04-11 23:42:25 -070018#include "../codecs/wm9081.h"
Mark Brown9b8dc662011-04-12 17:24:39 -070019
Mark Browna9ba6152011-06-24 12:10:44 +010020#define WM8996_HPSEL_GPIO 214
Mark Brown68688e72011-04-12 00:00:36 -070021
Mark Brownea0a5912011-04-11 23:32:03 -070022static int speyside_set_bias_level(struct snd_soc_card *card,
Mark Brownd4c60052011-06-06 19:13:23 +010023 struct snd_soc_dapm_context *dapm,
Mark Brownea0a5912011-04-11 23:32:03 -070024 enum snd_soc_bias_level level)
25{
26 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
27 int ret;
28
Mark Brownd4c60052011-06-06 19:13:23 +010029 if (dapm->dev != codec_dai->dev)
30 return 0;
31
Mark Brownea0a5912011-04-11 23:32:03 -070032 switch (level) {
33 case SND_SOC_BIAS_STANDBY:
Mark Browna9ba6152011-06-24 12:10:44 +010034 ret = snd_soc_dai_set_sysclk(codec_dai, WM8996_SYSCLK_MCLK2,
Mark Brownea0a5912011-04-11 23:32:03 -070035 32768, SND_SOC_CLOCK_IN);
36 if (ret < 0)
37 return ret;
38
Mark Browna9ba6152011-06-24 12:10:44 +010039 ret = snd_soc_dai_set_pll(codec_dai, WM8996_FLL_MCLK2,
Mark Brownea0a5912011-04-11 23:32:03 -070040 0, 0, 0);
41 if (ret < 0) {
42 pr_err("Failed to stop FLL\n");
43 return ret;
44 }
Mark Brown85e9e762011-06-06 19:12:44 +010045 break;
Mark Brownea0a5912011-04-11 23:32:03 -070046
47 default:
48 break;
49 }
50
51 return 0;
52}
53
Mark Brown85e9e762011-06-06 19:12:44 +010054static int speyside_set_bias_level_post(struct snd_soc_card *card,
55 struct snd_soc_dapm_context *dapm,
56 enum snd_soc_bias_level level)
57{
58 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
59 int ret;
60
61 if (dapm->dev != codec_dai->dev)
62 return 0;
63
64 switch (level) {
65 case SND_SOC_BIAS_PREPARE:
66 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
67 ret = snd_soc_dai_set_pll(codec_dai, 0,
Mark Browna9ba6152011-06-24 12:10:44 +010068 WM8996_FLL_MCLK2,
Mark Brown85e9e762011-06-06 19:12:44 +010069 32768, 48000 * 256);
70 if (ret < 0) {
71 pr_err("Failed to start FLL\n");
72 return ret;
73 }
74
75 ret = snd_soc_dai_set_sysclk(codec_dai,
Mark Browna9ba6152011-06-24 12:10:44 +010076 WM8996_SYSCLK_FLL,
Mark Brown85e9e762011-06-06 19:12:44 +010077 48000 * 256,
78 SND_SOC_CLOCK_IN);
79 if (ret < 0)
80 return ret;
81 }
82 break;
83
84 default:
85 break;
86 }
87
88 card->dapm.bias_level = level;
89
90 return 0;
91}
92
Mark Brown9b8dc662011-04-12 17:24:39 -070093static int speyside_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;
97 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
98 struct snd_soc_dai *codec_dai = rtd->codec_dai;
99 int ret;
100
101 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
102 | SND_SOC_DAIFMT_NB_NF
103 | SND_SOC_DAIFMT_CBM_CFM);
104 if (ret < 0)
105 return ret;
106
107 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S
108 | SND_SOC_DAIFMT_NB_NF
109 | SND_SOC_DAIFMT_CBM_CFM);
110 if (ret < 0)
111 return ret;
112
Mark Brown9b8dc662011-04-12 17:24:39 -0700113 return 0;
114}
115
116static struct snd_soc_ops speyside_ops = {
117 .hw_params = speyside_hw_params,
118};
119
Mark Brown68688e72011-04-12 00:00:36 -0700120static struct snd_soc_jack speyside_headset;
121
122/* Headset jack detection DAPM pins */
123static struct snd_soc_jack_pin speyside_headset_pins[] = {
124 {
125 .pin = "Headset Mic",
126 .mask = SND_JACK_MICROPHONE,
127 },
Mark Brown68688e72011-04-12 00:00:36 -0700128};
129
130/* Default the headphone selection to active high */
131static int speyside_jack_polarity;
132
133static int speyside_get_micbias(struct snd_soc_dapm_widget *source,
134 struct snd_soc_dapm_widget *sink)
135{
136 if (speyside_jack_polarity && (strcmp(source->name, "MICB1") == 0))
137 return 1;
138 if (!speyside_jack_polarity && (strcmp(source->name, "MICB2") == 0))
139 return 1;
140
141 return 0;
142}
143
144static void speyside_set_polarity(struct snd_soc_codec *codec,
145 int polarity)
146{
147 speyside_jack_polarity = !polarity;
Mark Browna9ba6152011-06-24 12:10:44 +0100148 gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
Mark Brown68688e72011-04-12 00:00:36 -0700149
150 /* Re-run DAPM to make sure we're using the correct mic bias */
151 snd_soc_dapm_sync(&codec->dapm);
152}
153
Mark Browna9ba6152011-06-24 12:10:44 +0100154static int speyside_wm8996_init(struct snd_soc_pcm_runtime *rtd)
Mark Brownea0a5912011-04-11 23:32:03 -0700155{
156 struct snd_soc_dai *dai = rtd->codec_dai;
Mark Brown68688e72011-04-12 00:00:36 -0700157 struct snd_soc_codec *codec = rtd->codec;
158 int ret;
Mark Brownea0a5912011-04-11 23:32:03 -0700159
Mark Browna9ba6152011-06-24 12:10:44 +0100160 ret = snd_soc_dai_set_sysclk(dai, WM8996_SYSCLK_MCLK2, 32768, 0);
Mark Brown68688e72011-04-12 00:00:36 -0700161 if (ret < 0)
162 return ret;
163
Mark Browna9ba6152011-06-24 12:10:44 +0100164 ret = gpio_request(WM8996_HPSEL_GPIO, "HP_SEL");
Mark Brown68688e72011-04-12 00:00:36 -0700165 if (ret != 0)
166 pr_err("Failed to request HP_SEL GPIO: %d\n", ret);
Mark Browna9ba6152011-06-24 12:10:44 +0100167 gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
Mark Brown68688e72011-04-12 00:00:36 -0700168
169 ret = snd_soc_jack_new(codec, "Headset",
Mark Brown45cf3672011-09-04 07:54:55 -0700170 SND_JACK_LINEOUT | SND_JACK_HEADSET |
171 SND_JACK_BTN_0,
Mark Brown68688e72011-04-12 00:00:36 -0700172 &speyside_headset);
173 if (ret)
174 return ret;
175
176 ret = snd_soc_jack_add_pins(&speyside_headset,
177 ARRAY_SIZE(speyside_headset_pins),
178 speyside_headset_pins);
179 if (ret)
180 return ret;
181
Mark Browna9ba6152011-06-24 12:10:44 +0100182 wm8996_detect(codec, &speyside_headset, speyside_set_polarity);
Mark Brown68688e72011-04-12 00:00:36 -0700183
184 return 0;
Mark Brownea0a5912011-04-11 23:32:03 -0700185}
186
Mark Brownb7a5d142011-04-12 17:37:52 -0700187static int speyside_late_probe(struct snd_soc_card *card)
188{
189 snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
190 snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic");
191 snd_soc_dapm_ignore_suspend(&card->dapm, "Main AMIC");
192 snd_soc_dapm_ignore_suspend(&card->dapm, "Main DMIC");
193 snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
194 snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Output");
195 snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Input");
196
197 return 0;
198}
199
Mark Brown9b8dc662011-04-12 17:24:39 -0700200static struct snd_soc_dai_link speyside_dai[] = {
201 {
202 .name = "CPU",
203 .stream_name = "CPU",
204 .cpu_dai_name = "samsung-i2s.0",
Mark Browna9ba6152011-06-24 12:10:44 +0100205 .codec_dai_name = "wm8996-aif1",
Mark Brown9b8dc662011-04-12 17:24:39 -0700206 .platform_name = "samsung-audio",
Mark Browna9ba6152011-06-24 12:10:44 +0100207 .codec_name = "wm8996.1-001a",
208 .init = speyside_wm8996_init,
Mark Brown9b8dc662011-04-12 17:24:39 -0700209 .ops = &speyside_ops,
210 },
Mark Brown556e4fb2011-04-12 14:15:10 -0700211 {
212 .name = "Baseband",
213 .stream_name = "Baseband",
Mark Browna9ba6152011-06-24 12:10:44 +0100214 .cpu_dai_name = "wm8996-aif2",
Mark Brown556e4fb2011-04-12 14:15:10 -0700215 .codec_dai_name = "wm1250-ev1",
216 .codec_name = "wm1250-ev1.1-0027",
Mark Brown556e4fb2011-04-12 14:15:10 -0700217 .ops = &speyside_ops,
Mark Brownb7a5d142011-04-12 17:37:52 -0700218 .ignore_suspend = 1,
Mark Brown556e4fb2011-04-12 14:15:10 -0700219 },
Mark Brown9b8dc662011-04-12 17:24:39 -0700220};
221
Mark Brownea3e98e2011-04-11 23:42:25 -0700222static int speyside_wm9081_init(struct snd_soc_dapm_context *dapm)
223{
224 snd_soc_dapm_nc_pin(dapm, "LINEOUT");
225
226 /* At any time the WM9081 is active it will have this clock */
Mark Brownda1c6ea2011-08-24 20:09:01 +0100227 return snd_soc_codec_set_sysclk(dapm->codec, WM9081_SYSCLK_MCLK, 0,
Mark Brownea3e98e2011-04-11 23:42:25 -0700228 48000 * 256, 0);
229}
230
231static struct snd_soc_aux_dev speyside_aux_dev[] = {
232 {
233 .name = "wm9081",
234 .codec_name = "wm9081.1-006c",
235 .init = speyside_wm9081_init,
236 },
237};
238
239static struct snd_soc_codec_conf speyside_codec_conf[] = {
240 {
241 .dev_name = "wm9081.1-006c",
242 .name_prefix = "Sub",
243 },
244};
245
Mark Brownea0e60d2011-04-12 00:09:53 -0700246static const struct snd_kcontrol_new controls[] = {
247 SOC_DAPM_PIN_SWITCH("Main Speaker"),
248 SOC_DAPM_PIN_SWITCH("Main DMIC"),
249 SOC_DAPM_PIN_SWITCH("Main AMIC"),
Mark Brown556e4fb2011-04-12 14:15:10 -0700250 SOC_DAPM_PIN_SWITCH("WM1250 Input"),
251 SOC_DAPM_PIN_SWITCH("WM1250 Output"),
Mark Brown68d5a592011-08-04 18:13:45 +0900252 SOC_DAPM_PIN_SWITCH("Headphone"),
Mark Brownea0e60d2011-04-12 00:09:53 -0700253};
254
Mark Brownecfb1ad2011-04-11 23:09:15 -0700255static struct snd_soc_dapm_widget widgets[] = {
256 SND_SOC_DAPM_HP("Headphone", NULL),
Mark Brown68688e72011-04-12 00:00:36 -0700257 SND_SOC_DAPM_MIC("Headset Mic", NULL),
Mark Brownecfb1ad2011-04-11 23:09:15 -0700258
259 SND_SOC_DAPM_SPK("Main Speaker", NULL),
260
261 SND_SOC_DAPM_MIC("Main AMIC", NULL),
262 SND_SOC_DAPM_MIC("Main DMIC", NULL),
263};
264
265static struct snd_soc_dapm_route audio_paths[] = {
Mark Brown68688e72011-04-12 00:00:36 -0700266 { "IN1RN", NULL, "MICB1" },
267 { "IN1RP", NULL, "MICB1" },
268 { "IN1RN", NULL, "MICB2" },
269 { "IN1RP", NULL, "MICB2" },
270 { "MICB1", NULL, "Headset Mic", speyside_get_micbias },
271 { "MICB2", NULL, "Headset Mic", speyside_get_micbias },
272
Mark Brownecfb1ad2011-04-11 23:09:15 -0700273 { "IN1LP", NULL, "MICB2" },
Mark Brown68688e72011-04-12 00:00:36 -0700274 { "IN1RN", NULL, "MICB1" },
Mark Brownecfb1ad2011-04-11 23:09:15 -0700275 { "MICB2", NULL, "Main AMIC" },
276
277 { "DMIC1DAT", NULL, "MICB1" },
278 { "DMIC2DAT", NULL, "MICB1" },
279 { "MICB1", NULL, "Main DMIC" },
280
281 { "Headphone", NULL, "HPOUT1L" },
282 { "Headphone", NULL, "HPOUT1R" },
283
Mark Brownea3e98e2011-04-11 23:42:25 -0700284 { "Sub IN1", NULL, "HPOUT2L" },
285 { "Sub IN2", NULL, "HPOUT2R" },
286
287 { "Main Speaker", NULL, "Sub SPKN" },
288 { "Main Speaker", NULL, "Sub SPKP" },
Mark Brownecfb1ad2011-04-11 23:09:15 -0700289 { "Main Speaker", NULL, "SPKDAT" },
290};
291
Mark Brown9b8dc662011-04-12 17:24:39 -0700292static struct snd_soc_card speyside = {
293 .name = "Speyside",
294 .dai_link = speyside_dai,
295 .num_links = ARRAY_SIZE(speyside_dai),
Mark Brownea3e98e2011-04-11 23:42:25 -0700296 .aux_dev = speyside_aux_dev,
297 .num_aux_devs = ARRAY_SIZE(speyside_aux_dev),
298 .codec_conf = speyside_codec_conf,
299 .num_configs = ARRAY_SIZE(speyside_codec_conf),
Mark Brownecfb1ad2011-04-11 23:09:15 -0700300
Mark Brownea0a5912011-04-11 23:32:03 -0700301 .set_bias_level = speyside_set_bias_level,
Mark Brown995e54f2011-06-08 16:11:18 +0100302 .set_bias_level_post = speyside_set_bias_level_post,
Mark Brownea0a5912011-04-11 23:32:03 -0700303
Mark Brownea0e60d2011-04-12 00:09:53 -0700304 .controls = controls,
305 .num_controls = ARRAY_SIZE(controls),
Mark Brownecfb1ad2011-04-11 23:09:15 -0700306 .dapm_widgets = widgets,
307 .num_dapm_widgets = ARRAY_SIZE(widgets),
308 .dapm_routes = audio_paths,
309 .num_dapm_routes = ARRAY_SIZE(audio_paths),
Mark Brownb7a5d142011-04-12 17:37:52 -0700310
311 .late_probe = speyside_late_probe,
Mark Brown9b8dc662011-04-12 17:24:39 -0700312};
313
314static __devinit int speyside_probe(struct platform_device *pdev)
315{
316 struct snd_soc_card *card = &speyside;
317 int ret;
318
319 card->dev = &pdev->dev;
320
321 ret = snd_soc_register_card(card);
322 if (ret) {
323 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
324 ret);
325 return ret;
326 }
327
328 return 0;
329}
330
331static int __devexit speyside_remove(struct platform_device *pdev)
332{
333 struct snd_soc_card *card = platform_get_drvdata(pdev);
334
335 snd_soc_unregister_card(card);
336
337 return 0;
338}
339
340static struct platform_driver speyside_driver = {
341 .driver = {
342 .name = "speyside",
343 .owner = THIS_MODULE,
344 .pm = &snd_soc_pm_ops,
345 },
346 .probe = speyside_probe,
347 .remove = __devexit_p(speyside_remove),
348};
349
350static int __init speyside_audio_init(void)
351{
352 return platform_driver_register(&speyside_driver);
353}
354module_init(speyside_audio_init);
355
356static void __exit speyside_audio_exit(void)
357{
358 platform_driver_unregister(&speyside_driver);
359}
360module_exit(speyside_audio_exit);
361
362MODULE_DESCRIPTION("Speyside audio support");
363MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
364MODULE_LICENSE("GPL");
365MODULE_ALIAS("platform:speyside");