Merge branches 'imx/pata' and 'imx/sata' into next/driver
[pandora-kernel.git] / sound / soc / samsung / speyside_wm8962.c
1 /*
2  * Speyside with WM8962 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
17 #include "../codecs/wm8962.h"
18
19 static int speyside_wm8962_set_bias_level(struct snd_soc_card *card,
20                                           struct snd_soc_dapm_context *dapm,
21                                           enum snd_soc_bias_level level)
22 {
23         struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
24         int ret;
25
26         switch (level) {
27         case SND_SOC_BIAS_PREPARE:
28                 if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
29                         ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
30                                                   WM8962_FLL_MCLK, 32768,
31                                                   44100 * 256);
32                         if (ret < 0)
33                                 pr_err("Failed to start FLL: %d\n", ret);
34
35                         ret = snd_soc_dai_set_sysclk(codec_dai,
36                                                      WM8962_SYSCLK_FLL,
37                                                      44100 * 256,
38                                                      SND_SOC_CLOCK_IN);
39                         if (ret < 0) {
40                                 pr_err("Failed to set SYSCLK: %d\n", ret);
41                                 return ret;
42                         }
43                 }
44                 break;
45
46         default:
47                 break;
48         }
49
50         return 0;
51 }
52
53 static int speyside_wm8962_set_bias_level_post(struct snd_soc_card *card,
54                                                struct snd_soc_dapm_context *dapm,
55                                                enum snd_soc_bias_level level)
56 {
57         struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
58         int ret;
59
60         switch (level) {
61         case SND_SOC_BIAS_STANDBY:
62                 ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
63                                              32768, SND_SOC_CLOCK_IN);
64                 if (ret < 0) {
65                         pr_err("Failed to switch away from FLL: %d\n", ret);
66                         return ret;
67                 }
68
69                 ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
70                                           0, 0, 0);
71                 if (ret < 0) {
72                         pr_err("Failed to stop FLL: %d\n", ret);
73                         return ret;
74                 }
75                 break;
76
77         default:
78                 break;
79         }
80
81         dapm->bias_level = level;
82
83         return 0;
84 }
85
86 static int speyside_wm8962_hw_params(struct snd_pcm_substream *substream,
87                               struct snd_pcm_hw_params *params)
88 {
89         struct snd_soc_pcm_runtime *rtd = substream->private_data;
90         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
91         struct snd_soc_dai *codec_dai = rtd->codec_dai;
92         int ret;
93
94         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
95                                          | SND_SOC_DAIFMT_NB_NF
96                                          | SND_SOC_DAIFMT_CBM_CFM);
97         if (ret < 0)
98                 return ret;
99
100         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S
101                                          | SND_SOC_DAIFMT_NB_NF
102                                          | SND_SOC_DAIFMT_CBM_CFM);
103         if (ret < 0)
104                 return ret;
105
106         return 0;
107 }
108
109 static struct snd_soc_ops speyside_wm8962_ops = {
110         .hw_params = speyside_wm8962_hw_params,
111 };
112
113 static struct snd_soc_dai_link speyside_wm8962_dai[] = {
114         {
115                 .name = "CPU",
116                 .stream_name = "CPU",
117                 .cpu_dai_name = "samsung-i2s.0",
118                 .codec_dai_name = "wm8962",
119                 .platform_name = "samsung-audio",
120                 .codec_name = "wm8962.1-001a",
121                 .ops = &speyside_wm8962_ops,
122         },
123 };
124
125 static const struct snd_kcontrol_new controls[] = {
126         SOC_DAPM_PIN_SWITCH("Main Speaker"),
127 };
128
129 static struct snd_soc_dapm_widget widgets[] = {
130         SND_SOC_DAPM_HP("Headphone", NULL),
131         SND_SOC_DAPM_MIC("Headset Mic", NULL),
132
133         SND_SOC_DAPM_MIC("DMIC", NULL),
134
135         SND_SOC_DAPM_SPK("Main Speaker", NULL),
136 };
137
138 static struct snd_soc_dapm_route audio_paths[] = {
139         { "Headphone", NULL, "HPOUTL" },
140         { "Headphone", NULL, "HPOUTR" },
141
142         { "Main Speaker", NULL, "SPKOUTL" },
143         { "Main Speaker", NULL, "SPKOUTR" },
144
145         { "MICBIAS", NULL, "Headset Mic" },
146         { "IN4L", NULL, "MICBIAS" },
147         { "IN4R", NULL, "MICBIAS" },
148
149         { "MICBIAS", NULL, "DMIC" },
150         { "DMICDAT", NULL, "MICBIAS" },
151 };
152
153 static struct snd_soc_jack speyside_wm8962_headset;
154
155 /* Headset jack detection DAPM pins */
156 static struct snd_soc_jack_pin speyside_wm8962_headset_pins[] = {
157         {
158                 .pin = "Headset Mic",
159                 .mask = SND_JACK_MICROPHONE,
160         },
161         {
162                 .pin = "Headphone",
163                 .mask = SND_JACK_MICROPHONE,
164         },
165 };
166
167 static int speyside_wm8962_late_probe(struct snd_soc_card *card)
168 {
169         struct snd_soc_codec *codec = card->rtd[0].codec;
170         struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
171         int ret;
172
173         ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
174                                      32768, SND_SOC_CLOCK_IN);
175         if (ret < 0)
176                 return ret;
177
178         ret = snd_soc_jack_new(codec, "Headset",
179                                SND_JACK_HEADSET | SND_JACK_BTN_0,
180                                &speyside_wm8962_headset);
181         if (ret)
182                 return ret;
183
184         ret = snd_soc_jack_add_pins(&speyside_wm8962_headset,
185                                     ARRAY_SIZE(speyside_wm8962_headset_pins),
186                                     speyside_wm8962_headset_pins);
187         if (ret)
188                 return ret;
189
190         wm8962_mic_detect(codec, &speyside_wm8962_headset);
191
192         return 0;
193 }
194
195 static struct snd_soc_card speyside_wm8962 = {
196         .name = "Speyside WM8962",
197         .dai_link = speyside_wm8962_dai,
198         .num_links = ARRAY_SIZE(speyside_wm8962_dai),
199
200         .set_bias_level = speyside_wm8962_set_bias_level,
201         .set_bias_level_post = speyside_wm8962_set_bias_level_post,
202
203         .controls = controls,
204         .num_controls = ARRAY_SIZE(controls),
205         .dapm_widgets = widgets,
206         .num_dapm_widgets = ARRAY_SIZE(widgets),
207         .dapm_routes = audio_paths,
208         .num_dapm_routes = ARRAY_SIZE(audio_paths),
209
210         .late_probe = speyside_wm8962_late_probe,
211 };
212
213 static __devinit int speyside_wm8962_probe(struct platform_device *pdev)
214 {
215         struct snd_soc_card *card = &speyside_wm8962;
216         int ret;
217
218         card->dev = &pdev->dev;
219
220         ret = snd_soc_register_card(card);
221         if (ret) {
222                 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
223                         ret);
224                 return ret;
225         }
226
227         return 0;
228 }
229
230 static int __devexit speyside_wm8962_remove(struct platform_device *pdev)
231 {
232         struct snd_soc_card *card = platform_get_drvdata(pdev);
233
234         snd_soc_unregister_card(card);
235
236         return 0;
237 }
238
239 static struct platform_driver speyside_wm8962_driver = {
240         .driver = {
241                 .name = "speyside-wm8962",
242                 .owner = THIS_MODULE,
243                 .pm = &snd_soc_pm_ops,
244         },
245         .probe = speyside_wm8962_probe,
246         .remove = __devexit_p(speyside_wm8962_remove),
247 };
248
249 static int __init speyside_wm8962_audio_init(void)
250 {
251         return platform_driver_register(&speyside_wm8962_driver);
252 }
253 module_init(speyside_wm8962_audio_init);
254
255 static void __exit speyside_wm8962_audio_exit(void)
256 {
257         platform_driver_unregister(&speyside_wm8962_driver);
258 }
259 module_exit(speyside_wm8962_audio_exit);
260
261 MODULE_DESCRIPTION("Speyside WM8962 audio support");
262 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
263 MODULE_LICENSE("GPL");
264 MODULE_ALIAS("platform:speyside-wm8962");