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