Commit | Line | Data |
---|---|---|
5033f43c | 1 | /* sound/soc/samsung/smartq_wm8987.c |
ce93a370 MC |
2 | * |
3 | * Copyright 2010 Maurus Cuelenaere <mcuelenaere@gmail.com> | |
4 | * | |
5 | * Based on smdk6410_wm8987.c | |
6 | * Copyright 2007 Wolfson Microelectronics PLC. - linux@wolfsonmicro.com | |
7 | * Graeme Gregory - graeme.gregory@wolfsonmicro.com | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify it | |
10 | * under the terms of the GNU General Public License as published by the | |
11 | * Free Software Foundation; either version 2 of the License, or (at your | |
12 | * option) any later version. | |
13 | * | |
14 | */ | |
15 | ||
16 | #include <linux/module.h> | |
17 | #include <linux/platform_device.h> | |
18 | #include <linux/gpio.h> | |
19 | ||
20 | #include <sound/pcm.h> | |
21 | #include <sound/pcm_params.h> | |
1c8f2c42 | 22 | #include <sound/soc.h> |
ce93a370 MC |
23 | #include <sound/jack.h> |
24 | ||
25 | #include <asm/mach-types.h> | |
26 | ||
4b640cf3 | 27 | #include "dma.h" |
b9493d6c | 28 | #include "i2s.h" |
ce93a370 MC |
29 | |
30 | #include "../codecs/wm8750.h" | |
31 | ||
32 | /* | |
33 | * WM8987 is register compatible with WM8750, so using that as base driver. | |
34 | */ | |
35 | ||
36 | static struct snd_soc_card snd_soc_smartq; | |
37 | ||
38 | static int smartq_hifi_hw_params(struct snd_pcm_substream *substream, | |
39 | struct snd_pcm_hw_params *params) | |
40 | { | |
41 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
74bd21e9 AL |
42 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
43 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
ce93a370 MC |
44 | unsigned int clk = 0; |
45 | int ret; | |
46 | ||
ce93a370 MC |
47 | switch (params_rate(params)) { |
48 | case 8000: | |
49 | case 16000: | |
50 | case 32000: | |
51 | case 48000: | |
52 | case 96000: | |
53 | clk = 12288000; | |
54 | break; | |
55 | case 11025: | |
56 | case 22050: | |
57 | case 44100: | |
58 | case 88200: | |
59 | clk = 11289600; | |
60 | break; | |
61 | } | |
62 | ||
63 | /* set codec DAI configuration */ | |
64 | ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S | | |
65 | SND_SOC_DAIFMT_NB_NF | | |
66 | SND_SOC_DAIFMT_CBS_CFS); | |
67 | if (ret < 0) | |
68 | return ret; | |
69 | ||
70 | /* set cpu DAI configuration */ | |
71 | ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S | | |
72 | SND_SOC_DAIFMT_NB_NF | | |
73 | SND_SOC_DAIFMT_CBS_CFS); | |
74 | if (ret < 0) | |
75 | return ret; | |
76 | ||
b9493d6c JB |
77 | /* Use PCLK for I2S signal generation */ |
78 | ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_RCLKSRC_0, | |
79 | 0, SND_SOC_CLOCK_IN); | |
ce93a370 MC |
80 | if (ret < 0) |
81 | return ret; | |
82 | ||
b9493d6c JB |
83 | /* Gate the RCLK output on PAD */ |
84 | ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_CDCLK, | |
85 | 0, SND_SOC_CLOCK_IN); | |
ce93a370 MC |
86 | if (ret < 0) |
87 | return ret; | |
88 | ||
b9493d6c JB |
89 | /* set the codec system clock for DAC and ADC */ |
90 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8750_SYSCLK, clk, | |
91 | SND_SOC_CLOCK_IN); | |
ce93a370 MC |
92 | if (ret < 0) |
93 | return ret; | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
98 | /* | |
99 | * SmartQ WM8987 HiFi DAI operations. | |
100 | */ | |
101 | static struct snd_soc_ops smartq_hifi_ops = { | |
102 | .hw_params = smartq_hifi_hw_params, | |
103 | }; | |
104 | ||
105 | static struct snd_soc_jack smartq_jack; | |
106 | ||
107 | static struct snd_soc_jack_pin smartq_jack_pins[] = { | |
108 | /* Disable speaker when headphone is plugged in */ | |
109 | { | |
110 | .pin = "Internal Speaker", | |
111 | .mask = SND_JACK_HEADPHONE, | |
ce93a370 MC |
112 | }, |
113 | }; | |
114 | ||
115 | static struct snd_soc_jack_gpio smartq_jack_gpios[] = { | |
116 | { | |
117 | .gpio = S3C64XX_GPL(12), | |
118 | .name = "headphone detect", | |
119 | .report = SND_JACK_HEADPHONE, | |
120 | .debounce_time = 200, | |
121 | }, | |
122 | }; | |
123 | ||
124 | static const struct snd_kcontrol_new wm8987_smartq_controls[] = { | |
125 | SOC_DAPM_PIN_SWITCH("Internal Speaker"), | |
126 | SOC_DAPM_PIN_SWITCH("Headphone Jack"), | |
127 | SOC_DAPM_PIN_SWITCH("Internal Mic"), | |
128 | }; | |
129 | ||
130 | static int smartq_speaker_event(struct snd_soc_dapm_widget *w, | |
131 | struct snd_kcontrol *k, | |
132 | int event) | |
133 | { | |
134 | gpio_set_value(S3C64XX_GPK(12), SND_SOC_DAPM_EVENT_OFF(event)); | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
139 | static const struct snd_soc_dapm_widget wm8987_dapm_widgets[] = { | |
140 | SND_SOC_DAPM_SPK("Internal Speaker", smartq_speaker_event), | |
141 | SND_SOC_DAPM_HP("Headphone Jack", NULL), | |
142 | SND_SOC_DAPM_MIC("Internal Mic", NULL), | |
143 | }; | |
144 | ||
145 | static const struct snd_soc_dapm_route audio_map[] = { | |
146 | {"Headphone Jack", NULL, "LOUT2"}, | |
147 | {"Headphone Jack", NULL, "ROUT2"}, | |
148 | ||
149 | {"Internal Speaker", NULL, "LOUT2"}, | |
150 | {"Internal Speaker", NULL, "ROUT2"}, | |
151 | ||
152 | {"Mic Bias", NULL, "Internal Mic"}, | |
153 | {"LINPUT2", NULL, "Mic Bias"}, | |
154 | }; | |
155 | ||
74bd21e9 | 156 | static int smartq_wm8987_init(struct snd_soc_pcm_runtime *rtd) |
ce93a370 | 157 | { |
74bd21e9 | 158 | struct snd_soc_codec *codec = rtd->codec; |
ce6120cc | 159 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
ce93a370 MC |
160 | int err = 0; |
161 | ||
162 | /* Add SmartQ specific widgets */ | |
ce6120cc | 163 | snd_soc_dapm_new_controls(dapm, wm8987_dapm_widgets, |
ce93a370 MC |
164 | ARRAY_SIZE(wm8987_dapm_widgets)); |
165 | ||
166 | /* add SmartQ specific controls */ | |
167 | err = snd_soc_add_controls(codec, wm8987_smartq_controls, | |
168 | ARRAY_SIZE(wm8987_smartq_controls)); | |
169 | ||
170 | if (err < 0) | |
171 | return err; | |
172 | ||
173 | /* setup SmartQ specific audio path */ | |
ce6120cc | 174 | snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map)); |
ce93a370 MC |
175 | |
176 | /* set endpoints to not connected */ | |
ce6120cc LG |
177 | snd_soc_dapm_nc_pin(dapm, "LINPUT1"); |
178 | snd_soc_dapm_nc_pin(dapm, "RINPUT1"); | |
179 | snd_soc_dapm_nc_pin(dapm, "OUT3"); | |
180 | snd_soc_dapm_nc_pin(dapm, "ROUT1"); | |
ce93a370 MC |
181 | |
182 | /* set endpoints to default off mode */ | |
ce6120cc LG |
183 | snd_soc_dapm_enable_pin(dapm, "Internal Speaker"); |
184 | snd_soc_dapm_enable_pin(dapm, "Internal Mic"); | |
185 | snd_soc_dapm_disable_pin(dapm, "Headphone Jack"); | |
ce93a370 | 186 | |
ce6120cc | 187 | err = snd_soc_dapm_sync(dapm); |
ce93a370 MC |
188 | if (err) |
189 | return err; | |
190 | ||
191 | /* Headphone jack detection */ | |
74bd21e9 | 192 | err = snd_soc_jack_new(codec, "Headphone Jack", |
ce93a370 MC |
193 | SND_JACK_HEADPHONE, &smartq_jack); |
194 | if (err) | |
195 | return err; | |
196 | ||
197 | err = snd_soc_jack_add_pins(&smartq_jack, ARRAY_SIZE(smartq_jack_pins), | |
198 | smartq_jack_pins); | |
199 | if (err) | |
200 | return err; | |
201 | ||
202 | err = snd_soc_jack_add_gpios(&smartq_jack, | |
203 | ARRAY_SIZE(smartq_jack_gpios), | |
204 | smartq_jack_gpios); | |
205 | ||
206 | return err; | |
207 | } | |
208 | ||
209 | static struct snd_soc_dai_link smartq_dai[] = { | |
210 | { | |
211 | .name = "wm8987", | |
212 | .stream_name = "SmartQ Hi-Fi", | |
b9493d6c | 213 | .cpu_dai_name = "samsung-i2s.0", |
f0fba2ad | 214 | .codec_dai_name = "wm8750-hifi", |
58bb4072 | 215 | .platform_name = "samsung-audio", |
f0fba2ad | 216 | .codec_name = "wm8750-codec.0-0x1a", |
ce93a370 MC |
217 | .init = smartq_wm8987_init, |
218 | .ops = &smartq_hifi_ops, | |
219 | }, | |
220 | }; | |
221 | ||
222 | static struct snd_soc_card snd_soc_smartq = { | |
223 | .name = "SmartQ", | |
ce93a370 MC |
224 | .dai_link = smartq_dai, |
225 | .num_links = ARRAY_SIZE(smartq_dai), | |
226 | }; | |
227 | ||
ce93a370 MC |
228 | static struct platform_device *smartq_snd_device; |
229 | ||
230 | static int __init smartq_init(void) | |
231 | { | |
232 | int ret; | |
233 | ||
234 | if (!machine_is_smartq7() && !machine_is_smartq5()) { | |
235 | pr_info("Only SmartQ is supported by this ASoC driver\n"); | |
236 | return -ENODEV; | |
237 | } | |
238 | ||
239 | smartq_snd_device = platform_device_alloc("soc-audio", -1); | |
240 | if (!smartq_snd_device) | |
241 | return -ENOMEM; | |
242 | ||
f0fba2ad | 243 | platform_set_drvdata(smartq_snd_device, &snd_soc_smartq); |
ce93a370 MC |
244 | |
245 | ret = platform_device_add(smartq_snd_device); | |
246 | if (ret) { | |
247 | platform_device_put(smartq_snd_device); | |
248 | return ret; | |
249 | } | |
250 | ||
251 | /* Initialise GPIOs used by amplifiers */ | |
252 | ret = gpio_request(S3C64XX_GPK(12), "amplifiers shutdown"); | |
253 | if (ret) { | |
254 | dev_err(&smartq_snd_device->dev, "Failed to register GPK12\n"); | |
255 | goto err_unregister_device; | |
256 | } | |
257 | ||
258 | /* Disable amplifiers */ | |
259 | ret = gpio_direction_output(S3C64XX_GPK(12), 1); | |
260 | if (ret) { | |
261 | dev_err(&smartq_snd_device->dev, "Failed to configure GPK12\n"); | |
262 | goto err_free_gpio_amp_shut; | |
263 | } | |
264 | ||
265 | return 0; | |
266 | ||
267 | err_free_gpio_amp_shut: | |
268 | gpio_free(S3C64XX_GPK(12)); | |
269 | err_unregister_device: | |
270 | platform_device_unregister(smartq_snd_device); | |
271 | ||
272 | return ret; | |
273 | } | |
274 | ||
275 | static void __exit smartq_exit(void) | |
276 | { | |
277 | snd_soc_jack_free_gpios(&smartq_jack, ARRAY_SIZE(smartq_jack_gpios), | |
278 | smartq_jack_gpios); | |
279 | ||
280 | platform_device_unregister(smartq_snd_device); | |
281 | } | |
282 | ||
283 | module_init(smartq_init); | |
284 | module_exit(smartq_exit); | |
285 | ||
286 | /* Module information */ | |
287 | MODULE_AUTHOR("Maurus Cuelenaere <mcuelenaere@gmail.com>"); | |
288 | MODULE_DESCRIPTION("ALSA SoC SmartQ WM8987"); | |
289 | MODULE_LICENSE("GPL"); |