ASoC: simple-card: simplify code
[deliverable/linux.git] / sound / soc / tegra / tegra_wm8903.c
CommitLineData
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 49struct 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 58static 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;
40db77a0 63 struct snd_soc_codec *codec = codec_dai->codec;
c244d477 64 struct snd_soc_card *card = codec->card;
dc0a50af 65 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
07541396 66 int srate, mclk;
a8bf1ba1
SW
67 int err;
68
69 srate = params_rate(params);
70 switch (srate) {
71 case 64000:
72 case 88200:
73 case 96000:
74 mclk = 128 * srate;
75 break;
76 default:
77 mclk = 256 * srate;
78 break;
79 }
80 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
81 while (mclk < 6000000)
82 mclk *= 2;
83
07541396 84 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
a8bf1ba1 85 if (err < 0) {
c244d477 86 dev_err(card->dev, "Can't configure clocks\n");
a8bf1ba1
SW
87 return err;
88 }
89
07541396
SW
90 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
91 SND_SOC_CLOCK_IN);
92 if (err < 0) {
93 dev_err(card->dev, "codec_dai clock not set\n");
94 return err;
a8bf1ba1
SW
95 }
96
97 return 0;
98}
99
dc0a50af
SW
100static struct snd_soc_ops tegra_wm8903_ops = {
101 .hw_params = tegra_wm8903_hw_params,
a8bf1ba1
SW
102};
103
dc0a50af 104static struct snd_soc_jack tegra_wm8903_hp_jack;
f7d3e403 105
dc0a50af 106static struct snd_soc_jack_pin tegra_wm8903_hp_jack_pins[] = {
f7d3e403
SW
107 {
108 .pin = "Headphone Jack",
109 .mask = SND_JACK_HEADPHONE,
110 },
111};
112
3eb25f99
SW
113static struct snd_soc_jack_gpio tegra_wm8903_hp_jack_gpio = {
114 .name = "headphone detect",
115 .report = SND_JACK_HEADPHONE,
116 .debounce_time = 150,
117 .invert = 1,
f7d3e403
SW
118};
119
dc0a50af 120static struct snd_soc_jack tegra_wm8903_mic_jack;
41b5f9b3 121
dc0a50af 122static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = {
41b5f9b3
SW
123 {
124 .pin = "Mic Jack",
125 .mask = SND_JACK_MICROPHONE,
126 },
127};
128
dc0a50af 129static int tegra_wm8903_event_int_spk(struct snd_soc_dapm_widget *w,
6e267645
SW
130 struct snd_kcontrol *k, int event)
131{
a32955db
SW
132 struct snd_soc_dapm_context *dapm = w->dapm;
133 struct snd_soc_card *card = dapm->card;
dc0a50af 134 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
6e267645 135
8f5f5e0f 136 if (!gpio_is_valid(machine->gpio_spkr_en))
773b1d3d
SW
137 return 0;
138
8f5f5e0f 139 gpio_set_value_cansleep(machine->gpio_spkr_en,
f9eabc3d 140 SND_SOC_DAPM_EVENT_ON(event));
6e267645
SW
141
142 return 0;
143}
144
773b1d3d
SW
145static int tegra_wm8903_event_hp(struct snd_soc_dapm_widget *w,
146 struct snd_kcontrol *k, int event)
147{
a32955db
SW
148 struct snd_soc_dapm_context *dapm = w->dapm;
149 struct snd_soc_card *card = dapm->card;
773b1d3d 150 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
773b1d3d 151
8f5f5e0f 152 if (!gpio_is_valid(machine->gpio_hp_mute))
773b1d3d
SW
153 return 0;
154
8f5f5e0f 155 gpio_set_value_cansleep(machine->gpio_hp_mute,
773b1d3d
SW
156 !SND_SOC_DAPM_EVENT_ON(event));
157
158 return 0;
159}
160
dc0a50af
SW
161static const struct snd_soc_dapm_widget tegra_wm8903_dapm_widgets[] = {
162 SND_SOC_DAPM_SPK("Int Spk", tegra_wm8903_event_int_spk),
773b1d3d 163 SND_SOC_DAPM_HP("Headphone Jack", tegra_wm8903_event_hp),
62ffac4d
SW
164 SND_SOC_DAPM_MIC("Mic Jack", NULL),
165};
166
dc0a50af 167static const struct snd_kcontrol_new tegra_wm8903_controls[] = {
3d8bc390
SW
168 SOC_DAPM_PIN_SWITCH("Int Spk"),
169};
170
dc0a50af 171static int tegra_wm8903_init(struct snd_soc_pcm_runtime *rtd)
62ffac4d 172{
40db77a0
SW
173 struct snd_soc_dai *codec_dai = rtd->codec_dai;
174 struct snd_soc_codec *codec = codec_dai->codec;
62ffac4d 175 struct snd_soc_dapm_context *dapm = &codec->dapm;
6e267645 176 struct snd_soc_card *card = codec->card;
dc0a50af 177 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
bf1b1328 178
8f5f5e0f
SW
179 if (gpio_is_valid(machine->gpio_hp_det)) {
180 tegra_wm8903_hp_jack_gpio.gpio = machine->gpio_hp_det;
773b1d3d
SW
181 snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
182 &tegra_wm8903_hp_jack);
183 snd_soc_jack_add_pins(&tegra_wm8903_hp_jack,
184 ARRAY_SIZE(tegra_wm8903_hp_jack_pins),
185 tegra_wm8903_hp_jack_pins);
186 snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack,
187 1,
188 &tegra_wm8903_hp_jack_gpio);
189 }
f7d3e403 190
41b5f9b3 191 snd_soc_jack_new(codec, "Mic Jack", SND_JACK_MICROPHONE,
dc0a50af
SW
192 &tegra_wm8903_mic_jack);
193 snd_soc_jack_add_pins(&tegra_wm8903_mic_jack,
194 ARRAY_SIZE(tegra_wm8903_mic_jack_pins),
195 tegra_wm8903_mic_jack_pins);
196 wm8903_mic_detect(codec, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE,
197 0);
41b5f9b3 198
5032dc34 199 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
41b5f9b3 200
62ffac4d
SW
201 return 0;
202}
203
3419ae78
SW
204static int tegra_wm8903_remove(struct snd_soc_card *card)
205{
206 struct snd_soc_pcm_runtime *rtd = &(card->rtd[0]);
207 struct snd_soc_dai *codec_dai = rtd->codec_dai;
208 struct snd_soc_codec *codec = codec_dai->codec;
209
210 wm8903_mic_detect(codec, NULL, 0, 0);
211
212 return 0;
213}
214
dc0a50af 215static struct snd_soc_dai_link tegra_wm8903_dai = {
a8bf1ba1
SW
216 .name = "WM8903",
217 .stream_name = "WM8903 PCM",
a8bf1ba1 218 .codec_dai_name = "wm8903-hifi",
dc0a50af
SW
219 .init = tegra_wm8903_init,
220 .ops = &tegra_wm8903_ops,
408dafc4
SW
221 .dai_fmt = SND_SOC_DAIFMT_I2S |
222 SND_SOC_DAIFMT_NB_NF |
223 SND_SOC_DAIFMT_CBS_CFS,
a8bf1ba1
SW
224};
225
dc0a50af
SW
226static struct snd_soc_card snd_soc_tegra_wm8903 = {
227 .name = "tegra-wm8903",
b16eaf9f 228 .owner = THIS_MODULE,
dc0a50af 229 .dai_link = &tegra_wm8903_dai,
a8bf1ba1 230 .num_links = 1,
dea8b6ee 231
3419ae78
SW
232 .remove = tegra_wm8903_remove,
233
dea8b6ee
SW
234 .controls = tegra_wm8903_controls,
235 .num_controls = ARRAY_SIZE(tegra_wm8903_controls),
236 .dapm_widgets = tegra_wm8903_dapm_widgets,
237 .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
6e5fdba9 238 .fully_routed = true,
a8bf1ba1
SW
239};
240
4652a0d0 241static int tegra_wm8903_driver_probe(struct platform_device *pdev)
a8bf1ba1 242{
f51022f1 243 struct device_node *np = pdev->dev.of_node;
dc0a50af
SW
244 struct snd_soc_card *card = &snd_soc_tegra_wm8903;
245 struct tegra_wm8903 *machine;
a8bf1ba1
SW
246 int ret;
247
e4e4c18a
SW
248 machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8903),
249 GFP_KERNEL);
dc0a50af
SW
250 if (!machine) {
251 dev_err(&pdev->dev, "Can't allocate tegra_wm8903 struct\n");
8f5f5e0f 252 return -ENOMEM;
a8bf1ba1 253 }
a8bf1ba1 254
72de2b1a
SW
255 card->dev = &pdev->dev;
256 platform_set_drvdata(pdev, card);
dc0a50af 257 snd_soc_card_set_drvdata(card, machine);
a8bf1ba1 258
8f5f5e0f
SW
259 machine->gpio_spkr_en = of_get_named_gpio(np, "nvidia,spkr-en-gpios",
260 0);
261 if (machine->gpio_spkr_en == -EPROBE_DEFER)
262 return -EPROBE_DEFER;
263 if (gpio_is_valid(machine->gpio_spkr_en)) {
264 ret = devm_gpio_request_one(&pdev->dev, machine->gpio_spkr_en,
e2d287c1 265 GPIOF_OUT_INIT_LOW, "spkr_en");
f51022f1
SW
266 if (ret) {
267 dev_err(card->dev, "cannot get spkr_en gpio\n");
268 return ret;
269 }
f51022f1
SW
270 }
271
8f5f5e0f
SW
272 machine->gpio_hp_mute = of_get_named_gpio(np, "nvidia,hp-mute-gpios",
273 0);
274 if (machine->gpio_hp_mute == -EPROBE_DEFER)
275 return -EPROBE_DEFER;
276 if (gpio_is_valid(machine->gpio_hp_mute)) {
277 ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_mute,
e2d287c1 278 GPIOF_OUT_INIT_HIGH, "hp_mute");
f51022f1
SW
279 if (ret) {
280 dev_err(card->dev, "cannot get hp_mute gpio\n");
281 return ret;
282 }
f51022f1
SW
283 }
284
8f5f5e0f
SW
285 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
286 if (machine->gpio_hp_det == -EPROBE_DEFER)
287 return -EPROBE_DEFER;
288
289 machine->gpio_int_mic_en = of_get_named_gpio(np,
290 "nvidia,int-mic-en-gpios", 0);
291 if (machine->gpio_int_mic_en == -EPROBE_DEFER)
292 return -EPROBE_DEFER;
293 if (gpio_is_valid(machine->gpio_int_mic_en)) {
e2d287c1 294 /* Disable int mic; enable signal is active-high */
8f5f5e0f
SW
295 ret = devm_gpio_request_one(&pdev->dev,
296 machine->gpio_int_mic_en,
e2d287c1 297 GPIOF_OUT_INIT_LOW, "int_mic_en");
f51022f1
SW
298 if (ret) {
299 dev_err(card->dev, "cannot get int_mic_en gpio\n");
300 return ret;
301 }
f51022f1
SW
302 }
303
8f5f5e0f
SW
304 machine->gpio_ext_mic_en = of_get_named_gpio(np,
305 "nvidia,ext-mic-en-gpios", 0);
306 if (machine->gpio_ext_mic_en == -EPROBE_DEFER)
307 return -EPROBE_DEFER;
308 if (gpio_is_valid(machine->gpio_ext_mic_en)) {
e2d287c1 309 /* Enable ext mic; enable signal is active-low */
8f5f5e0f
SW
310 ret = devm_gpio_request_one(&pdev->dev,
311 machine->gpio_ext_mic_en,
e2d287c1 312 GPIOF_OUT_INIT_LOW, "ext_mic_en");
f51022f1
SW
313 if (ret) {
314 dev_err(card->dev, "cannot get ext_mic_en gpio\n");
315 return ret;
316 }
f51022f1
SW
317 }
318
8f5f5e0f
SW
319 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
320 if (ret)
321 goto err;
322
323 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
324 if (ret)
325 goto err;
326
327 tegra_wm8903_dai.codec_of_node = of_parse_phandle(np,
328 "nvidia,audio-codec", 0);
329 if (!tegra_wm8903_dai.codec_of_node) {
330 dev_err(&pdev->dev,
331 "Property 'nvidia,audio-codec' missing or invalid\n");
332 ret = -EINVAL;
333 goto err;
334 }
335
336 tegra_wm8903_dai.cpu_of_node = of_parse_phandle(np,
337 "nvidia,i2s-controller", 0);
338 if (!tegra_wm8903_dai.cpu_of_node) {
339 dev_err(&pdev->dev,
340 "Property 'nvidia,i2s-controller' missing or invalid\n");
341 ret = -EINVAL;
342 goto err;
343 }
344
345 tegra_wm8903_dai.platform_of_node = tegra_wm8903_dai.cpu_of_node;
346
07cdf36d
SW
347 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
348 if (ret)
518de86b 349 goto err;
07cdf36d 350
72de2b1a 351 ret = snd_soc_register_card(card);
a8bf1ba1 352 if (ret) {
72de2b1a 353 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
a8bf1ba1 354 ret);
acb8303f 355 goto err_fini_utils;
a8bf1ba1
SW
356 }
357
358 return 0;
359
acb8303f 360err_fini_utils:
dc0a50af 361 tegra_asoc_utils_fini(&machine->util_data);
e4e4c18a 362err:
a8bf1ba1
SW
363 return ret;
364}
a8bf1ba1 365
4652a0d0 366static int tegra_wm8903_driver_remove(struct platform_device *pdev)
a8bf1ba1 367{
72de2b1a 368 struct snd_soc_card *card = platform_get_drvdata(pdev);
dc0a50af 369 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
72de2b1a 370
e44fbbd4
SW
371 snd_soc_jack_free_gpios(&tegra_wm8903_hp_jack, 1,
372 &tegra_wm8903_hp_jack_gpio);
29591ed4
SW
373
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 381static const struct of_device_id tegra_wm8903_of_match[] = {
07cdf36d
SW
382 { .compatible = "nvidia,tegra-audio-wm8903", },
383 {},
384};
385
dc0a50af 386static struct platform_driver tegra_wm8903_driver = {
72de2b1a
SW
387 .driver = {
388 .name = DRV_NAME,
389 .owner = THIS_MODULE,
deb2607e 390 .pm = &snd_soc_pm_ops,
07cdf36d 391 .of_match_table = tegra_wm8903_of_match,
72de2b1a 392 },
dc0a50af 393 .probe = tegra_wm8903_driver_probe,
4652a0d0 394 .remove = tegra_wm8903_driver_remove,
72de2b1a 395};
e4e4c18a 396module_platform_driver(tegra_wm8903_driver);
a8bf1ba1
SW
397
398MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
dc0a50af 399MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
a8bf1ba1 400MODULE_LICENSE("GPL");
8eb34207 401MODULE_ALIAS("platform:" DRV_NAME);
07cdf36d 402MODULE_DEVICE_TABLE(of, tegra_wm8903_of_match);
This page took 0.154021 seconds and 5 git commands to generate.