blob: f21ff608a8199dd09f70b19b0bf92070a86fd28f [file] [log] [blame]
Mark Brown22cb8392011-04-25 18:30:45 +01001/*
Mark Brown64142612011-11-30 13:30:27 +00002 * Tobermory audio support
Mark Brown22cb8392011-04-25 18:30:45 +01003 *
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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040016#include <linux/module.h>
Mark Brown22cb8392011-04-25 18:30:45 +010017
18#include "../codecs/wm8962.h"
19
Mark Brownef473fe2011-09-23 16:05:00 +010020static int sample_rate = 44100;
21
Mark Brown64142612011-11-30 13:30:27 +000022static int tobermory_set_bias_level(struct snd_soc_card *card,
Mark Brown22cb8392011-04-25 18:30:45 +010023 struct snd_soc_dapm_context *dapm,
24 enum snd_soc_bias_level level)
25{
26 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
27 int ret;
28
Mark Brownf79e7ff2011-08-21 12:20:00 +010029 if (dapm->dev != codec_dai->dev)
30 return 0;
31
Mark Brown22cb8392011-04-25 18:30:45 +010032 switch (level) {
33 case SND_SOC_BIAS_PREPARE:
34 if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
35 ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
36 WM8962_FLL_MCLK, 32768,
Mark Brownef473fe2011-09-23 16:05:00 +010037 sample_rate * 512);
Mark Brown22cb8392011-04-25 18:30:45 +010038 if (ret < 0)
Mark Brown67d0c472011-06-29 14:07:24 -070039 pr_err("Failed to start FLL: %d\n", ret);
Mark Brown22cb8392011-04-25 18:30:45 +010040
41 ret = snd_soc_dai_set_sysclk(codec_dai,
42 WM8962_SYSCLK_FLL,
Mark Brownef473fe2011-09-23 16:05:00 +010043 sample_rate * 512,
Mark Brown22cb8392011-04-25 18:30:45 +010044 SND_SOC_CLOCK_IN);
Mark Brown67d0c472011-06-29 14:07:24 -070045 if (ret < 0) {
Mark Brown371e7302011-08-04 10:54:17 +090046 pr_err("Failed to set SYSCLK: %d\n", ret);
Mark Brown22cb8392011-04-25 18:30:45 +010047 return ret;
Mark Brown67d0c472011-06-29 14:07:24 -070048 }
Mark Brown22cb8392011-04-25 18:30:45 +010049 }
50 break;
51
52 default:
53 break;
54 }
55
56 return 0;
57}
58
Mark Brown64142612011-11-30 13:30:27 +000059static int tobermory_set_bias_level_post(struct snd_soc_card *card,
Mark Brown22cb8392011-04-25 18:30:45 +010060 struct snd_soc_dapm_context *dapm,
61 enum snd_soc_bias_level level)
62{
63 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
64 int ret;
65
Mark Brownf79e7ff2011-08-21 12:20:00 +010066 if (dapm->dev != codec_dai->dev)
67 return 0;
68
Mark Brown22cb8392011-04-25 18:30:45 +010069 switch (level) {
70 case SND_SOC_BIAS_STANDBY:
71 ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
72 32768, SND_SOC_CLOCK_IN);
Mark Brown67d0c472011-06-29 14:07:24 -070073 if (ret < 0) {
74 pr_err("Failed to switch away from FLL: %d\n", ret);
Mark Brown22cb8392011-04-25 18:30:45 +010075 return ret;
Mark Brown67d0c472011-06-29 14:07:24 -070076 }
Mark Brown22cb8392011-04-25 18:30:45 +010077
78 ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
79 0, 0, 0);
80 if (ret < 0) {
Mark Brown67d0c472011-06-29 14:07:24 -070081 pr_err("Failed to stop FLL: %d\n", ret);
Mark Brown22cb8392011-04-25 18:30:45 +010082 return ret;
83 }
84 break;
85
86 default:
87 break;
88 }
89
90 dapm->bias_level = level;
91
92 return 0;
93}
94
Mark Brown64142612011-11-30 13:30:27 +000095static int tobermory_hw_params(struct snd_pcm_substream *substream,
Mark Brown22cb8392011-04-25 18:30:45 +010096 struct snd_pcm_hw_params *params)
97{
Mark Brownef473fe2011-09-23 16:05:00 +010098 sample_rate = params_rate(params);
99
Mark Brown22cb8392011-04-25 18:30:45 +0100100 return 0;
101}
102
Mark Brown64142612011-11-30 13:30:27 +0000103static struct snd_soc_ops tobermory_ops = {
104 .hw_params = tobermory_hw_params,
Mark Brown22cb8392011-04-25 18:30:45 +0100105};
106
Mark Brown64142612011-11-30 13:30:27 +0000107static struct snd_soc_dai_link tobermory_dai[] = {
Mark Brown22cb8392011-04-25 18:30:45 +0100108 {
109 .name = "CPU",
110 .stream_name = "CPU",
111 .cpu_dai_name = "samsung-i2s.0",
112 .codec_dai_name = "wm8962",
Padmavathi Vennaa08485d82012-12-07 13:59:21 +0530113 .platform_name = "samsung-i2s.0",
Mark Brown22cb8392011-04-25 18:30:45 +0100114 .codec_name = "wm8962.1-001a",
Mark Brown44fdd432011-09-27 16:42:27 +0100115 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
116 | SND_SOC_DAIFMT_CBM_CFM,
Mark Brown64142612011-11-30 13:30:27 +0000117 .ops = &tobermory_ops,
Mark Brown22cb8392011-04-25 18:30:45 +0100118 },
119};
120
121static const struct snd_kcontrol_new controls[] = {
122 SOC_DAPM_PIN_SWITCH("Main Speaker"),
Mark Brown8c756152011-09-23 16:46:24 +0100123 SOC_DAPM_PIN_SWITCH("DMIC"),
Mark Brown22cb8392011-04-25 18:30:45 +0100124};
125
126static struct snd_soc_dapm_widget widgets[] = {
127 SND_SOC_DAPM_HP("Headphone", NULL),
128 SND_SOC_DAPM_MIC("Headset Mic", NULL),
129
130 SND_SOC_DAPM_MIC("DMIC", NULL),
Mark Brown75644632011-09-23 16:23:11 +0100131 SND_SOC_DAPM_MIC("AMIC", NULL),
Mark Brown22cb8392011-04-25 18:30:45 +0100132
133 SND_SOC_DAPM_SPK("Main Speaker", NULL),
134};
135
136static struct snd_soc_dapm_route audio_paths[] = {
137 { "Headphone", NULL, "HPOUTL" },
138 { "Headphone", NULL, "HPOUTR" },
139
140 { "Main Speaker", NULL, "SPKOUTL" },
141 { "Main Speaker", NULL, "SPKOUTR" },
142
Mark Brown086d7f82011-09-23 16:22:48 +0100143 { "Headset Mic", NULL, "MICBIAS" },
144 { "IN4L", NULL, "Headset Mic" },
145 { "IN4R", NULL, "Headset Mic" },
Mark Brown22cb8392011-04-25 18:30:45 +0100146
Mark Brown75644632011-09-23 16:23:11 +0100147 { "AMIC", NULL, "MICBIAS" },
148 { "IN1L", NULL, "AMIC" },
149 { "IN1R", NULL, "AMIC" },
150
Mark Brown086d7f82011-09-23 16:22:48 +0100151 { "DMIC", NULL, "MICBIAS" },
152 { "DMICDAT", NULL, "DMIC" },
Mark Brown22cb8392011-04-25 18:30:45 +0100153};
154
Mark Brown64142612011-11-30 13:30:27 +0000155static struct snd_soc_jack tobermory_headset;
Mark Brown22cb8392011-04-25 18:30:45 +0100156
157/* Headset jack detection DAPM pins */
Mark Brown64142612011-11-30 13:30:27 +0000158static struct snd_soc_jack_pin tobermory_headset_pins[] = {
Mark Brown22cb8392011-04-25 18:30:45 +0100159 {
160 .pin = "Headset Mic",
161 .mask = SND_JACK_MICROPHONE,
162 },
163 {
164 .pin = "Headphone",
165 .mask = SND_JACK_MICROPHONE,
166 },
167};
168
Mark Brown64142612011-11-30 13:30:27 +0000169static int tobermory_late_probe(struct snd_soc_card *card)
Mark Brown22cb8392011-04-25 18:30:45 +0100170{
171 struct snd_soc_codec *codec = card->rtd[0].codec;
172 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
173 int ret;
174
175 ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
176 32768, SND_SOC_CLOCK_IN);
177 if (ret < 0)
178 return ret;
179
180 ret = snd_soc_jack_new(codec, "Headset",
181 SND_JACK_HEADSET | SND_JACK_BTN_0,
Mark Brown64142612011-11-30 13:30:27 +0000182 &tobermory_headset);
Mark Brown22cb8392011-04-25 18:30:45 +0100183 if (ret)
184 return ret;
185
Mark Brown64142612011-11-30 13:30:27 +0000186 ret = snd_soc_jack_add_pins(&tobermory_headset,
187 ARRAY_SIZE(tobermory_headset_pins),
188 tobermory_headset_pins);
Mark Brown22cb8392011-04-25 18:30:45 +0100189 if (ret)
190 return ret;
191
Mark Brown64142612011-11-30 13:30:27 +0000192 wm8962_mic_detect(codec, &tobermory_headset);
Mark Brown22cb8392011-04-25 18:30:45 +0100193
194 return 0;
195}
196
Mark Brown64142612011-11-30 13:30:27 +0000197static struct snd_soc_card tobermory = {
198 .name = "Tobermory",
Axel Lin095d79d2011-12-22 10:53:15 +0800199 .owner = THIS_MODULE,
Mark Brown64142612011-11-30 13:30:27 +0000200 .dai_link = tobermory_dai,
201 .num_links = ARRAY_SIZE(tobermory_dai),
Mark Brown22cb8392011-04-25 18:30:45 +0100202
Mark Brown64142612011-11-30 13:30:27 +0000203 .set_bias_level = tobermory_set_bias_level,
204 .set_bias_level_post = tobermory_set_bias_level_post,
Mark Brown22cb8392011-04-25 18:30:45 +0100205
206 .controls = controls,
207 .num_controls = ARRAY_SIZE(controls),
208 .dapm_widgets = widgets,
209 .num_dapm_widgets = ARRAY_SIZE(widgets),
210 .dapm_routes = audio_paths,
211 .num_dapm_routes = ARRAY_SIZE(audio_paths),
Mark Brown39afd662011-11-23 21:33:07 +0000212 .fully_routed = true,
Mark Brown22cb8392011-04-25 18:30:45 +0100213
Mark Brown64142612011-11-30 13:30:27 +0000214 .late_probe = tobermory_late_probe,
Mark Brown22cb8392011-04-25 18:30:45 +0100215};
216
Bill Pembertonfdca21a2012-12-07 09:26:15 -0500217static int tobermory_probe(struct platform_device *pdev)
Mark Brown22cb8392011-04-25 18:30:45 +0100218{
Mark Brown64142612011-11-30 13:30:27 +0000219 struct snd_soc_card *card = &tobermory;
Mark Brown22cb8392011-04-25 18:30:45 +0100220 int ret;
221
222 card->dev = &pdev->dev;
223
224 ret = snd_soc_register_card(card);
225 if (ret) {
226 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
227 ret);
228 return ret;
229 }
230
231 return 0;
232}
233
Bill Pembertonfdca21a2012-12-07 09:26:15 -0500234static int tobermory_remove(struct platform_device *pdev)
Mark Brown22cb8392011-04-25 18:30:45 +0100235{
236 struct snd_soc_card *card = platform_get_drvdata(pdev);
237
238 snd_soc_unregister_card(card);
239
240 return 0;
241}
242
Mark Brown64142612011-11-30 13:30:27 +0000243static struct platform_driver tobermory_driver = {
Mark Brown22cb8392011-04-25 18:30:45 +0100244 .driver = {
Mark Brown64142612011-11-30 13:30:27 +0000245 .name = "tobermory",
Mark Brown22cb8392011-04-25 18:30:45 +0100246 .owner = THIS_MODULE,
247 .pm = &snd_soc_pm_ops,
248 },
Mark Brown64142612011-11-30 13:30:27 +0000249 .probe = tobermory_probe,
Bill Pembertonfdca21a2012-12-07 09:26:15 -0500250 .remove = tobermory_remove,
Mark Brown22cb8392011-04-25 18:30:45 +0100251};
252
Mark Brown64142612011-11-30 13:30:27 +0000253module_platform_driver(tobermory_driver);
Mark Brown22cb8392011-04-25 18:30:45 +0100254
Mark Brown64142612011-11-30 13:30:27 +0000255MODULE_DESCRIPTION("Tobermory audio support");
Mark Brown22cb8392011-04-25 18:30:45 +0100256MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
257MODULE_LICENSE("GPL");
Mark Brown64142612011-11-30 13:30:27 +0000258MODULE_ALIAS("platform:tobermory");