blob: a4a9fc7e8c762c610f8f1f616fc516894e8506da [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040016#include <linux/module.h>
Mark Brown9b8dc662011-04-12 17:24:39 -070017
Mark Browna9ba6152011-06-24 12:10:44 +010018#include "../codecs/wm8996.h"
Mark Brownea3e98e2011-04-11 23:42:25 -070019#include "../codecs/wm9081.h"
Mark Brown9b8dc662011-04-12 17:24:39 -070020
Mark Browna9ba6152011-06-24 12:10:44 +010021#define WM8996_HPSEL_GPIO 214
Mark Brown36281372011-12-11 11:31:22 +080022#define MCLK_AUDIO_RATE (512 * 48000)
Mark Brown68688e72011-04-12 00:00:36 -070023
Mark Brownea0a5912011-04-11 23:32:03 -070024static int speyside_set_bias_level(struct snd_soc_card *card,
Mark Brownd4c60052011-06-06 19:13:23 +010025 struct snd_soc_dapm_context *dapm,
Mark Brownea0a5912011-04-11 23:32:03 -070026 enum snd_soc_bias_level level)
27{
28 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
29 int ret;
30
Mark Brownd4c60052011-06-06 19:13:23 +010031 if (dapm->dev != codec_dai->dev)
32 return 0;
33
Mark Brownea0a5912011-04-11 23:32:03 -070034 switch (level) {
35 case SND_SOC_BIAS_STANDBY:
Mark Browna9ba6152011-06-24 12:10:44 +010036 ret = snd_soc_dai_set_sysclk(codec_dai, WM8996_SYSCLK_MCLK2,
Mark Brownea0a5912011-04-11 23:32:03 -070037 32768, SND_SOC_CLOCK_IN);
38 if (ret < 0)
39 return ret;
40
Mark Browna9ba6152011-06-24 12:10:44 +010041 ret = snd_soc_dai_set_pll(codec_dai, WM8996_FLL_MCLK2,
Mark Brownea0a5912011-04-11 23:32:03 -070042 0, 0, 0);
43 if (ret < 0) {
44 pr_err("Failed to stop FLL\n");
45 return ret;
46 }
Mark Brown85e9e762011-06-06 19:12:44 +010047 break;
Mark Brownea0a5912011-04-11 23:32:03 -070048
49 default:
50 break;
51 }
52
53 return 0;
54}
55
Mark Brown85e9e762011-06-06 19:12:44 +010056static int speyside_set_bias_level_post(struct snd_soc_card *card,
57 struct snd_soc_dapm_context *dapm,
58 enum snd_soc_bias_level level)
59{
60 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
61 int ret;
62
63 if (dapm->dev != codec_dai->dev)
64 return 0;
65
66 switch (level) {
67 case SND_SOC_BIAS_PREPARE:
68 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
69 ret = snd_soc_dai_set_pll(codec_dai, 0,
Mark Browna9ba6152011-06-24 12:10:44 +010070 WM8996_FLL_MCLK2,
Mark Brown36281372011-12-11 11:31:22 +080071 32768, MCLK_AUDIO_RATE);
Mark Brown85e9e762011-06-06 19:12:44 +010072 if (ret < 0) {
73 pr_err("Failed to start FLL\n");
74 return ret;
75 }
76
77 ret = snd_soc_dai_set_sysclk(codec_dai,
Mark Browna9ba6152011-06-24 12:10:44 +010078 WM8996_SYSCLK_FLL,
Mark Brown36281372011-12-11 11:31:22 +080079 MCLK_AUDIO_RATE,
Mark Brown85e9e762011-06-06 19:12:44 +010080 SND_SOC_CLOCK_IN);
81 if (ret < 0)
82 return ret;
83 }
84 break;
85
86 default:
87 break;
88 }
89
90 card->dapm.bias_level = level;
91
92 return 0;
93}
94
Mark Brown68688e72011-04-12 00:00:36 -070095static struct snd_soc_jack speyside_headset;
96
97/* Headset jack detection DAPM pins */
98static struct snd_soc_jack_pin speyside_headset_pins[] = {
99 {
100 .pin = "Headset Mic",
101 .mask = SND_JACK_MICROPHONE,
102 },
Mark Brown68688e72011-04-12 00:00:36 -0700103};
104
105/* Default the headphone selection to active high */
106static int speyside_jack_polarity;
107
108static int speyside_get_micbias(struct snd_soc_dapm_widget *source,
109 struct snd_soc_dapm_widget *sink)
110{
111 if (speyside_jack_polarity && (strcmp(source->name, "MICB1") == 0))
112 return 1;
113 if (!speyside_jack_polarity && (strcmp(source->name, "MICB2") == 0))
114 return 1;
115
116 return 0;
117}
118
119static void speyside_set_polarity(struct snd_soc_codec *codec,
120 int polarity)
121{
122 speyside_jack_polarity = !polarity;
Mark Browna9ba6152011-06-24 12:10:44 +0100123 gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
Mark Brown68688e72011-04-12 00:00:36 -0700124
125 /* Re-run DAPM to make sure we're using the correct mic bias */
126 snd_soc_dapm_sync(&codec->dapm);
127}
128
Mark Browna9ba6152011-06-24 12:10:44 +0100129static int speyside_wm8996_init(struct snd_soc_pcm_runtime *rtd)
Mark Brownea0a5912011-04-11 23:32:03 -0700130{
131 struct snd_soc_dai *dai = rtd->codec_dai;
Mark Brown68688e72011-04-12 00:00:36 -0700132 struct snd_soc_codec *codec = rtd->codec;
133 int ret;
Mark Brownea0a5912011-04-11 23:32:03 -0700134
Mark Browna9ba6152011-06-24 12:10:44 +0100135 ret = snd_soc_dai_set_sysclk(dai, WM8996_SYSCLK_MCLK2, 32768, 0);
Mark Brown68688e72011-04-12 00:00:36 -0700136 if (ret < 0)
137 return ret;
138
Mark Browna9ba6152011-06-24 12:10:44 +0100139 ret = gpio_request(WM8996_HPSEL_GPIO, "HP_SEL");
Mark Brown68688e72011-04-12 00:00:36 -0700140 if (ret != 0)
141 pr_err("Failed to request HP_SEL GPIO: %d\n", ret);
Mark Browna9ba6152011-06-24 12:10:44 +0100142 gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
Mark Brown68688e72011-04-12 00:00:36 -0700143
144 ret = snd_soc_jack_new(codec, "Headset",
Mark Brown45cf3672011-09-04 07:54:55 -0700145 SND_JACK_LINEOUT | SND_JACK_HEADSET |
146 SND_JACK_BTN_0,
Mark Brown68688e72011-04-12 00:00:36 -0700147 &speyside_headset);
148 if (ret)
149 return ret;
150
151 ret = snd_soc_jack_add_pins(&speyside_headset,
152 ARRAY_SIZE(speyside_headset_pins),
153 speyside_headset_pins);
154 if (ret)
155 return ret;
156
Mark Browna9ba6152011-06-24 12:10:44 +0100157 wm8996_detect(codec, &speyside_headset, speyside_set_polarity);
Mark Brown68688e72011-04-12 00:00:36 -0700158
159 return 0;
Mark Brownea0a5912011-04-11 23:32:03 -0700160}
161
Mark Brownb7a5d142011-04-12 17:37:52 -0700162static int speyside_late_probe(struct snd_soc_card *card)
163{
164 snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
165 snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic");
166 snd_soc_dapm_ignore_suspend(&card->dapm, "Main AMIC");
167 snd_soc_dapm_ignore_suspend(&card->dapm, "Main DMIC");
Mark Brown5b895ee2011-11-27 15:54:49 +0000168 snd_soc_dapm_ignore_suspend(&card->dapm, "Main Speaker");
Mark Brownb7a5d142011-04-12 17:37:52 -0700169 snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Output");
170 snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Input");
171
172 return 0;
173}
174
Mark Brown9b8dc662011-04-12 17:24:39 -0700175static struct snd_soc_dai_link speyside_dai[] = {
176 {
177 .name = "CPU",
178 .stream_name = "CPU",
179 .cpu_dai_name = "samsung-i2s.0",
Mark Browna9ba6152011-06-24 12:10:44 +0100180 .codec_dai_name = "wm8996-aif1",
Mark Brown9b8dc662011-04-12 17:24:39 -0700181 .platform_name = "samsung-audio",
Mark Browna9ba6152011-06-24 12:10:44 +0100182 .codec_name = "wm8996.1-001a",
183 .init = speyside_wm8996_init,
Mark Brown26e67812011-09-27 16:48:49 +0100184 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
185 | SND_SOC_DAIFMT_CBM_CFM,
Mark Brown9b8dc662011-04-12 17:24:39 -0700186 },
Mark Brown556e4fb2011-04-12 14:15:10 -0700187 {
188 .name = "Baseband",
189 .stream_name = "Baseband",
Mark Browna9ba6152011-06-24 12:10:44 +0100190 .cpu_dai_name = "wm8996-aif2",
Mark Brown556e4fb2011-04-12 14:15:10 -0700191 .codec_dai_name = "wm1250-ev1",
192 .codec_name = "wm1250-ev1.1-0027",
Mark Brown26e67812011-09-27 16:48:49 +0100193 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
194 | SND_SOC_DAIFMT_CBM_CFM,
Mark Brownb7a5d142011-04-12 17:37:52 -0700195 .ignore_suspend = 1,
Mark Brown556e4fb2011-04-12 14:15:10 -0700196 },
Mark Brown9b8dc662011-04-12 17:24:39 -0700197};
198
Mark Brownea3e98e2011-04-11 23:42:25 -0700199static int speyside_wm9081_init(struct snd_soc_dapm_context *dapm)
200{
Mark Brownea3e98e2011-04-11 23:42:25 -0700201 /* At any time the WM9081 is active it will have this clock */
Mark Brownda1c6ea2011-08-24 20:09:01 +0100202 return snd_soc_codec_set_sysclk(dapm->codec, WM9081_SYSCLK_MCLK, 0,
Mark Brown36281372011-12-11 11:31:22 +0800203 MCLK_AUDIO_RATE, 0);
Mark Brownea3e98e2011-04-11 23:42:25 -0700204}
205
206static struct snd_soc_aux_dev speyside_aux_dev[] = {
207 {
208 .name = "wm9081",
209 .codec_name = "wm9081.1-006c",
210 .init = speyside_wm9081_init,
211 },
212};
213
214static struct snd_soc_codec_conf speyside_codec_conf[] = {
215 {
216 .dev_name = "wm9081.1-006c",
217 .name_prefix = "Sub",
218 },
219};
220
Mark Brownea0e60d2011-04-12 00:09:53 -0700221static const struct snd_kcontrol_new controls[] = {
222 SOC_DAPM_PIN_SWITCH("Main Speaker"),
223 SOC_DAPM_PIN_SWITCH("Main DMIC"),
224 SOC_DAPM_PIN_SWITCH("Main AMIC"),
Mark Brown556e4fb2011-04-12 14:15:10 -0700225 SOC_DAPM_PIN_SWITCH("WM1250 Input"),
226 SOC_DAPM_PIN_SWITCH("WM1250 Output"),
Mark Brown68d5a592011-08-04 18:13:45 +0900227 SOC_DAPM_PIN_SWITCH("Headphone"),
Mark Brownea0e60d2011-04-12 00:09:53 -0700228};
229
Mark Brownecfb1ad2011-04-11 23:09:15 -0700230static struct snd_soc_dapm_widget widgets[] = {
231 SND_SOC_DAPM_HP("Headphone", NULL),
Mark Brown68688e72011-04-12 00:00:36 -0700232 SND_SOC_DAPM_MIC("Headset Mic", NULL),
Mark Brownecfb1ad2011-04-11 23:09:15 -0700233
234 SND_SOC_DAPM_SPK("Main Speaker", NULL),
235
236 SND_SOC_DAPM_MIC("Main AMIC", NULL),
237 SND_SOC_DAPM_MIC("Main DMIC", NULL),
238};
239
240static struct snd_soc_dapm_route audio_paths[] = {
Mark Brown68688e72011-04-12 00:00:36 -0700241 { "IN1RN", NULL, "MICB1" },
242 { "IN1RP", NULL, "MICB1" },
243 { "IN1RN", NULL, "MICB2" },
244 { "IN1RP", NULL, "MICB2" },
245 { "MICB1", NULL, "Headset Mic", speyside_get_micbias },
246 { "MICB2", NULL, "Headset Mic", speyside_get_micbias },
247
Mark Brownecfb1ad2011-04-11 23:09:15 -0700248 { "IN1LP", NULL, "MICB2" },
Mark Brown68688e72011-04-12 00:00:36 -0700249 { "IN1RN", NULL, "MICB1" },
Mark Brownecfb1ad2011-04-11 23:09:15 -0700250 { "MICB2", NULL, "Main AMIC" },
251
252 { "DMIC1DAT", NULL, "MICB1" },
253 { "DMIC2DAT", NULL, "MICB1" },
254 { "MICB1", NULL, "Main DMIC" },
255
256 { "Headphone", NULL, "HPOUT1L" },
257 { "Headphone", NULL, "HPOUT1R" },
258
Mark Brownea3e98e2011-04-11 23:42:25 -0700259 { "Sub IN1", NULL, "HPOUT2L" },
260 { "Sub IN2", NULL, "HPOUT2R" },
261
262 { "Main Speaker", NULL, "Sub SPKN" },
263 { "Main Speaker", NULL, "Sub SPKP" },
Mark Brownecfb1ad2011-04-11 23:09:15 -0700264 { "Main Speaker", NULL, "SPKDAT" },
265};
266
Mark Brown9b8dc662011-04-12 17:24:39 -0700267static struct snd_soc_card speyside = {
268 .name = "Speyside",
Axel Lin095d79d2011-12-22 10:53:15 +0800269 .owner = THIS_MODULE,
Mark Brown9b8dc662011-04-12 17:24:39 -0700270 .dai_link = speyside_dai,
271 .num_links = ARRAY_SIZE(speyside_dai),
Mark Brownea3e98e2011-04-11 23:42:25 -0700272 .aux_dev = speyside_aux_dev,
273 .num_aux_devs = ARRAY_SIZE(speyside_aux_dev),
274 .codec_conf = speyside_codec_conf,
275 .num_configs = ARRAY_SIZE(speyside_codec_conf),
Mark Brownecfb1ad2011-04-11 23:09:15 -0700276
Mark Brownea0a5912011-04-11 23:32:03 -0700277 .set_bias_level = speyside_set_bias_level,
Mark Brown995e54f2011-06-08 16:11:18 +0100278 .set_bias_level_post = speyside_set_bias_level_post,
Mark Brownea0a5912011-04-11 23:32:03 -0700279
Mark Brownea0e60d2011-04-12 00:09:53 -0700280 .controls = controls,
281 .num_controls = ARRAY_SIZE(controls),
Mark Brownecfb1ad2011-04-11 23:09:15 -0700282 .dapm_widgets = widgets,
283 .num_dapm_widgets = ARRAY_SIZE(widgets),
284 .dapm_routes = audio_paths,
285 .num_dapm_routes = ARRAY_SIZE(audio_paths),
Mark Brown39afd662011-11-23 21:33:07 +0000286 .fully_routed = true,
Mark Brownb7a5d142011-04-12 17:37:52 -0700287
288 .late_probe = speyside_late_probe,
Mark Brown9b8dc662011-04-12 17:24:39 -0700289};
290
291static __devinit int speyside_probe(struct platform_device *pdev)
292{
293 struct snd_soc_card *card = &speyside;
294 int ret;
295
296 card->dev = &pdev->dev;
297
298 ret = snd_soc_register_card(card);
299 if (ret) {
300 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
301 ret);
302 return ret;
303 }
304
305 return 0;
306}
307
308static int __devexit speyside_remove(struct platform_device *pdev)
309{
310 struct snd_soc_card *card = platform_get_drvdata(pdev);
311
312 snd_soc_unregister_card(card);
313
314 return 0;
315}
316
317static struct platform_driver speyside_driver = {
318 .driver = {
319 .name = "speyside",
320 .owner = THIS_MODULE,
321 .pm = &snd_soc_pm_ops,
322 },
323 .probe = speyside_probe,
324 .remove = __devexit_p(speyside_remove),
325};
326
Mark Browne00c3f52011-11-23 15:20:13 +0000327module_platform_driver(speyside_driver);
Mark Brown9b8dc662011-04-12 17:24:39 -0700328
329MODULE_DESCRIPTION("Speyside audio support");
330MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
331MODULE_LICENSE("GPL");
332MODULE_ALIAS("platform:speyside");