Commit | Line | Data |
---|---|---|
a8bf1ba1 | 1 | /* |
dc0a50af | 2 | * tegra_wm8903.c - Tegra machine ASoC driver for boards using WM8903 codec. |
a8bf1ba1 SW |
3 | * |
4 | * Author: Stephen Warren <swarren@nvidia.com> | |
518de86b | 5 | * Copyright (C) 2010-2012 - NVIDIA, Inc. |
a8bf1ba1 SW |
6 | * |
7 | * Based on code copyright/by: | |
8 | * | |
9 | * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd. | |
10 | * | |
11 | * Copyright 2007 Wolfson Microelectronics PLC. | |
12 | * Author: Graeme Gregory | |
13 | * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License | |
17 | * version 2 as published by the Free Software Foundation. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, but | |
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 | * General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
27 | * 02110-1301 USA | |
28 | * | |
29 | */ | |
30 | ||
a8bf1ba1 | 31 | #include <linux/module.h> |
72de2b1a SW |
32 | #include <linux/platform_device.h> |
33 | #include <linux/slab.h> | |
6e267645 | 34 | #include <linux/gpio.h> |
07cdf36d | 35 | #include <linux/of_gpio.h> |
6e267645 | 36 | |
a8bf1ba1 | 37 | #include <sound/core.h> |
f7d3e403 | 38 | #include <sound/jack.h> |
a8bf1ba1 SW |
39 | #include <sound/pcm.h> |
40 | #include <sound/pcm_params.h> | |
41 | #include <sound/soc.h> | |
42 | ||
41b5f9b3 SW |
43 | #include "../codecs/wm8903.h" |
44 | ||
a8bf1ba1 SW |
45 | #include "tegra_asoc_utils.h" |
46 | ||
7b33af25 | 47 | #define DRV_NAME "tegra-snd-wm8903" |
a8bf1ba1 | 48 | |
dc0a50af | 49 | struct tegra_wm8903 { |
8f5f5e0f SW |
50 | int gpio_spkr_en; |
51 | int gpio_hp_det; | |
52 | int gpio_hp_mute; | |
53 | int gpio_int_mic_en; | |
54 | int gpio_ext_mic_en; | |
d64e57ce | 55 | struct tegra_asoc_utils_data util_data; |
72de2b1a | 56 | }; |
a8bf1ba1 | 57 | |
dc0a50af | 58 | static int tegra_wm8903_hw_params(struct snd_pcm_substream *substream, |
a8bf1ba1 SW |
59 | struct snd_pcm_hw_params *params) |
60 | { | |
61 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
62 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
093c4e5c | 63 | struct snd_soc_card *card = rtd->card; |
dc0a50af | 64 | struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card); |
07541396 | 65 | int srate, mclk; |
a8bf1ba1 SW |
66 | int err; |
67 | ||
68 | srate = params_rate(params); | |
69 | switch (srate) { | |
70 | case 64000: | |
71 | case 88200: | |
72 | case 96000: | |
73 | mclk = 128 * srate; | |
74 | break; | |
75 | default: | |
76 | mclk = 256 * srate; | |
77 | break; | |
78 | } | |
79 | /* FIXME: Codec only requires >= 3MHz if OSR==0 */ | |
80 | while (mclk < 6000000) | |
81 | mclk *= 2; | |
82 | ||
07541396 | 83 | err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk); |
a8bf1ba1 | 84 | if (err < 0) { |
c244d477 | 85 | dev_err(card->dev, "Can't configure clocks\n"); |
a8bf1ba1 SW |
86 | return err; |
87 | } | |
88 | ||
07541396 SW |
89 | err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, |
90 | SND_SOC_CLOCK_IN); | |
91 | if (err < 0) { | |
92 | dev_err(card->dev, "codec_dai clock not set\n"); | |
93 | return err; | |
a8bf1ba1 SW |
94 | } |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
dc0a50af SW |
99 | static struct snd_soc_ops tegra_wm8903_ops = { |
100 | .hw_params = tegra_wm8903_hw_params, | |
a8bf1ba1 SW |
101 | }; |
102 | ||
dc0a50af | 103 | static struct snd_soc_jack tegra_wm8903_hp_jack; |
f7d3e403 | 104 | |
dc0a50af | 105 | static struct snd_soc_jack_pin tegra_wm8903_hp_jack_pins[] = { |
f7d3e403 SW |
106 | { |
107 | .pin = "Headphone Jack", | |
108 | .mask = SND_JACK_HEADPHONE, | |
109 | }, | |
110 | }; | |
111 | ||
3eb25f99 SW |
112 | static struct snd_soc_jack_gpio tegra_wm8903_hp_jack_gpio = { |
113 | .name = "headphone detect", | |
114 | .report = SND_JACK_HEADPHONE, | |
115 | .debounce_time = 150, | |
116 | .invert = 1, | |
f7d3e403 SW |
117 | }; |
118 | ||
dc0a50af | 119 | static struct snd_soc_jack tegra_wm8903_mic_jack; |
41b5f9b3 | 120 | |
dc0a50af | 121 | static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = { |
41b5f9b3 SW |
122 | { |
123 | .pin = "Mic Jack", | |
124 | .mask = SND_JACK_MICROPHONE, | |
125 | }, | |
126 | }; | |
127 | ||
dc0a50af | 128 | static int tegra_wm8903_event_int_spk(struct snd_soc_dapm_widget *w, |
6e267645 SW |
129 | struct snd_kcontrol *k, int event) |
130 | { | |
a32955db SW |
131 | struct snd_soc_dapm_context *dapm = w->dapm; |
132 | struct snd_soc_card *card = dapm->card; | |
dc0a50af | 133 | struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card); |
6e267645 | 134 | |
8f5f5e0f | 135 | if (!gpio_is_valid(machine->gpio_spkr_en)) |
773b1d3d SW |
136 | return 0; |
137 | ||
8f5f5e0f | 138 | gpio_set_value_cansleep(machine->gpio_spkr_en, |
f9eabc3d | 139 | SND_SOC_DAPM_EVENT_ON(event)); |
6e267645 SW |
140 | |
141 | return 0; | |
142 | } | |
143 | ||
773b1d3d SW |
144 | static int tegra_wm8903_event_hp(struct snd_soc_dapm_widget *w, |
145 | struct snd_kcontrol *k, int event) | |
146 | { | |
a32955db SW |
147 | struct snd_soc_dapm_context *dapm = w->dapm; |
148 | struct snd_soc_card *card = dapm->card; | |
773b1d3d | 149 | struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card); |
773b1d3d | 150 | |
8f5f5e0f | 151 | if (!gpio_is_valid(machine->gpio_hp_mute)) |
773b1d3d SW |
152 | return 0; |
153 | ||
8f5f5e0f | 154 | gpio_set_value_cansleep(machine->gpio_hp_mute, |
773b1d3d SW |
155 | !SND_SOC_DAPM_EVENT_ON(event)); |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
dc0a50af SW |
160 | static const struct snd_soc_dapm_widget tegra_wm8903_dapm_widgets[] = { |
161 | SND_SOC_DAPM_SPK("Int Spk", tegra_wm8903_event_int_spk), | |
773b1d3d | 162 | SND_SOC_DAPM_HP("Headphone Jack", tegra_wm8903_event_hp), |
62ffac4d SW |
163 | SND_SOC_DAPM_MIC("Mic Jack", NULL), |
164 | }; | |
165 | ||
dc0a50af | 166 | static const struct snd_kcontrol_new tegra_wm8903_controls[] = { |
3d8bc390 SW |
167 | SOC_DAPM_PIN_SWITCH("Int Spk"), |
168 | }; | |
169 | ||
dc0a50af | 170 | static int tegra_wm8903_init(struct snd_soc_pcm_runtime *rtd) |
62ffac4d | 171 | { |
40db77a0 SW |
172 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
173 | struct snd_soc_codec *codec = codec_dai->codec; | |
62ffac4d | 174 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
093c4e5c | 175 | struct snd_soc_card *card = rtd->card; |
dc0a50af | 176 | struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card); |
bf1b1328 | 177 | |
8f5f5e0f SW |
178 | if (gpio_is_valid(machine->gpio_hp_det)) { |
179 | tegra_wm8903_hp_jack_gpio.gpio = machine->gpio_hp_det; | |
773b1d3d SW |
180 | snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE, |
181 | &tegra_wm8903_hp_jack); | |
182 | snd_soc_jack_add_pins(&tegra_wm8903_hp_jack, | |
183 | ARRAY_SIZE(tegra_wm8903_hp_jack_pins), | |
184 | tegra_wm8903_hp_jack_pins); | |
185 | snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack, | |
186 | 1, | |
187 | &tegra_wm8903_hp_jack_gpio); | |
188 | } | |
f7d3e403 | 189 | |
41b5f9b3 | 190 | snd_soc_jack_new(codec, "Mic Jack", SND_JACK_MICROPHONE, |
dc0a50af SW |
191 | &tegra_wm8903_mic_jack); |
192 | snd_soc_jack_add_pins(&tegra_wm8903_mic_jack, | |
193 | ARRAY_SIZE(tegra_wm8903_mic_jack_pins), | |
194 | tegra_wm8903_mic_jack_pins); | |
195 | wm8903_mic_detect(codec, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE, | |
196 | 0); | |
41b5f9b3 | 197 | |
5032dc34 | 198 | snd_soc_dapm_force_enable_pin(dapm, "MICBIAS"); |
41b5f9b3 | 199 | |
62ffac4d SW |
200 | return 0; |
201 | } | |
202 | ||
3419ae78 SW |
203 | static int tegra_wm8903_remove(struct snd_soc_card *card) |
204 | { | |
205 | struct snd_soc_pcm_runtime *rtd = &(card->rtd[0]); | |
206 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
207 | struct snd_soc_codec *codec = codec_dai->codec; | |
fb6b8e71 SW |
208 | struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card); |
209 | ||
210 | if (gpio_is_valid(machine->gpio_hp_det)) { | |
211 | snd_soc_jack_free_gpios(&tegra_wm8903_hp_jack, 1, | |
212 | &tegra_wm8903_hp_jack_gpio); | |
213 | } | |
3419ae78 SW |
214 | |
215 | wm8903_mic_detect(codec, NULL, 0, 0); | |
216 | ||
217 | return 0; | |
218 | } | |
219 | ||
dc0a50af | 220 | static struct snd_soc_dai_link tegra_wm8903_dai = { |
a8bf1ba1 SW |
221 | .name = "WM8903", |
222 | .stream_name = "WM8903 PCM", | |
a8bf1ba1 | 223 | .codec_dai_name = "wm8903-hifi", |
dc0a50af SW |
224 | .init = tegra_wm8903_init, |
225 | .ops = &tegra_wm8903_ops, | |
408dafc4 SW |
226 | .dai_fmt = SND_SOC_DAIFMT_I2S | |
227 | SND_SOC_DAIFMT_NB_NF | | |
228 | SND_SOC_DAIFMT_CBS_CFS, | |
a8bf1ba1 SW |
229 | }; |
230 | ||
dc0a50af SW |
231 | static struct snd_soc_card snd_soc_tegra_wm8903 = { |
232 | .name = "tegra-wm8903", | |
b16eaf9f | 233 | .owner = THIS_MODULE, |
dc0a50af | 234 | .dai_link = &tegra_wm8903_dai, |
a8bf1ba1 | 235 | .num_links = 1, |
3419ae78 | 236 | .remove = tegra_wm8903_remove, |
dea8b6ee SW |
237 | .controls = tegra_wm8903_controls, |
238 | .num_controls = ARRAY_SIZE(tegra_wm8903_controls), | |
239 | .dapm_widgets = tegra_wm8903_dapm_widgets, | |
240 | .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets), | |
6e5fdba9 | 241 | .fully_routed = true, |
a8bf1ba1 SW |
242 | }; |
243 | ||
4652a0d0 | 244 | static int tegra_wm8903_driver_probe(struct platform_device *pdev) |
a8bf1ba1 | 245 | { |
f51022f1 | 246 | struct device_node *np = pdev->dev.of_node; |
dc0a50af SW |
247 | struct snd_soc_card *card = &snd_soc_tegra_wm8903; |
248 | struct tegra_wm8903 *machine; | |
a8bf1ba1 SW |
249 | int ret; |
250 | ||
e4e4c18a SW |
251 | machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8903), |
252 | GFP_KERNEL); | |
dc0a50af SW |
253 | if (!machine) { |
254 | dev_err(&pdev->dev, "Can't allocate tegra_wm8903 struct\n"); | |
8f5f5e0f | 255 | return -ENOMEM; |
a8bf1ba1 | 256 | } |
a8bf1ba1 | 257 | |
72de2b1a SW |
258 | card->dev = &pdev->dev; |
259 | platform_set_drvdata(pdev, card); | |
dc0a50af | 260 | snd_soc_card_set_drvdata(card, machine); |
a8bf1ba1 | 261 | |
8f5f5e0f SW |
262 | machine->gpio_spkr_en = of_get_named_gpio(np, "nvidia,spkr-en-gpios", |
263 | 0); | |
264 | if (machine->gpio_spkr_en == -EPROBE_DEFER) | |
265 | return -EPROBE_DEFER; | |
266 | if (gpio_is_valid(machine->gpio_spkr_en)) { | |
267 | ret = devm_gpio_request_one(&pdev->dev, machine->gpio_spkr_en, | |
e2d287c1 | 268 | GPIOF_OUT_INIT_LOW, "spkr_en"); |
f51022f1 SW |
269 | if (ret) { |
270 | dev_err(card->dev, "cannot get spkr_en gpio\n"); | |
271 | return ret; | |
272 | } | |
f51022f1 SW |
273 | } |
274 | ||
8f5f5e0f SW |
275 | machine->gpio_hp_mute = of_get_named_gpio(np, "nvidia,hp-mute-gpios", |
276 | 0); | |
277 | if (machine->gpio_hp_mute == -EPROBE_DEFER) | |
278 | return -EPROBE_DEFER; | |
279 | if (gpio_is_valid(machine->gpio_hp_mute)) { | |
280 | ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_mute, | |
e2d287c1 | 281 | GPIOF_OUT_INIT_HIGH, "hp_mute"); |
f51022f1 SW |
282 | if (ret) { |
283 | dev_err(card->dev, "cannot get hp_mute gpio\n"); | |
284 | return ret; | |
285 | } | |
f51022f1 SW |
286 | } |
287 | ||
8f5f5e0f SW |
288 | machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0); |
289 | if (machine->gpio_hp_det == -EPROBE_DEFER) | |
290 | return -EPROBE_DEFER; | |
291 | ||
292 | machine->gpio_int_mic_en = of_get_named_gpio(np, | |
293 | "nvidia,int-mic-en-gpios", 0); | |
294 | if (machine->gpio_int_mic_en == -EPROBE_DEFER) | |
295 | return -EPROBE_DEFER; | |
296 | if (gpio_is_valid(machine->gpio_int_mic_en)) { | |
e2d287c1 | 297 | /* Disable int mic; enable signal is active-high */ |
8f5f5e0f SW |
298 | ret = devm_gpio_request_one(&pdev->dev, |
299 | machine->gpio_int_mic_en, | |
e2d287c1 | 300 | GPIOF_OUT_INIT_LOW, "int_mic_en"); |
f51022f1 SW |
301 | if (ret) { |
302 | dev_err(card->dev, "cannot get int_mic_en gpio\n"); | |
303 | return ret; | |
304 | } | |
f51022f1 SW |
305 | } |
306 | ||
8f5f5e0f SW |
307 | machine->gpio_ext_mic_en = of_get_named_gpio(np, |
308 | "nvidia,ext-mic-en-gpios", 0); | |
309 | if (machine->gpio_ext_mic_en == -EPROBE_DEFER) | |
310 | return -EPROBE_DEFER; | |
311 | if (gpio_is_valid(machine->gpio_ext_mic_en)) { | |
e2d287c1 | 312 | /* Enable ext mic; enable signal is active-low */ |
8f5f5e0f SW |
313 | ret = devm_gpio_request_one(&pdev->dev, |
314 | machine->gpio_ext_mic_en, | |
e2d287c1 | 315 | GPIOF_OUT_INIT_LOW, "ext_mic_en"); |
f51022f1 SW |
316 | if (ret) { |
317 | dev_err(card->dev, "cannot get ext_mic_en gpio\n"); | |
318 | return ret; | |
319 | } | |
f51022f1 SW |
320 | } |
321 | ||
8f5f5e0f SW |
322 | ret = snd_soc_of_parse_card_name(card, "nvidia,model"); |
323 | if (ret) | |
324 | goto err; | |
325 | ||
326 | ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing"); | |
327 | if (ret) | |
328 | goto err; | |
329 | ||
330 | tegra_wm8903_dai.codec_of_node = of_parse_phandle(np, | |
331 | "nvidia,audio-codec", 0); | |
332 | if (!tegra_wm8903_dai.codec_of_node) { | |
333 | dev_err(&pdev->dev, | |
334 | "Property 'nvidia,audio-codec' missing or invalid\n"); | |
335 | ret = -EINVAL; | |
336 | goto err; | |
337 | } | |
338 | ||
339 | tegra_wm8903_dai.cpu_of_node = of_parse_phandle(np, | |
340 | "nvidia,i2s-controller", 0); | |
341 | if (!tegra_wm8903_dai.cpu_of_node) { | |
342 | dev_err(&pdev->dev, | |
343 | "Property 'nvidia,i2s-controller' missing or invalid\n"); | |
344 | ret = -EINVAL; | |
345 | goto err; | |
346 | } | |
347 | ||
348 | tegra_wm8903_dai.platform_of_node = tegra_wm8903_dai.cpu_of_node; | |
349 | ||
07cdf36d SW |
350 | ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev); |
351 | if (ret) | |
518de86b | 352 | goto err; |
07cdf36d | 353 | |
72de2b1a | 354 | ret = snd_soc_register_card(card); |
a8bf1ba1 | 355 | if (ret) { |
72de2b1a | 356 | dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", |
a8bf1ba1 | 357 | ret); |
acb8303f | 358 | goto err_fini_utils; |
a8bf1ba1 SW |
359 | } |
360 | ||
361 | return 0; | |
362 | ||
acb8303f | 363 | err_fini_utils: |
dc0a50af | 364 | tegra_asoc_utils_fini(&machine->util_data); |
e4e4c18a | 365 | err: |
a8bf1ba1 SW |
366 | return ret; |
367 | } | |
a8bf1ba1 | 368 | |
4652a0d0 | 369 | static int tegra_wm8903_driver_remove(struct platform_device *pdev) |
a8bf1ba1 | 370 | { |
72de2b1a | 371 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
dc0a50af | 372 | struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card); |
72de2b1a | 373 | |
29591ed4 SW |
374 | snd_soc_unregister_card(card); |
375 | ||
376 | tegra_asoc_utils_fini(&machine->util_data); | |
6e267645 | 377 | |
72de2b1a SW |
378 | return 0; |
379 | } | |
380 | ||
f6e65744 | 381 | static const struct of_device_id tegra_wm8903_of_match[] = { |
07cdf36d SW |
382 | { .compatible = "nvidia,tegra-audio-wm8903", }, |
383 | {}, | |
384 | }; | |
385 | ||
dc0a50af | 386 | static struct platform_driver tegra_wm8903_driver = { |
72de2b1a SW |
387 | .driver = { |
388 | .name = DRV_NAME, | |
deb2607e | 389 | .pm = &snd_soc_pm_ops, |
07cdf36d | 390 | .of_match_table = tegra_wm8903_of_match, |
72de2b1a | 391 | }, |
dc0a50af | 392 | .probe = tegra_wm8903_driver_probe, |
4652a0d0 | 393 | .remove = tegra_wm8903_driver_remove, |
72de2b1a | 394 | }; |
e4e4c18a | 395 | module_platform_driver(tegra_wm8903_driver); |
a8bf1ba1 SW |
396 | |
397 | MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>"); | |
dc0a50af | 398 | MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver"); |
a8bf1ba1 | 399 | MODULE_LICENSE("GPL"); |
8eb34207 | 400 | MODULE_ALIAS("platform:" DRV_NAME); |
07cdf36d | 401 | MODULE_DEVICE_TABLE(of, tegra_wm8903_of_match); |