treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / tegra / tegra_wm8903.c
blobf08d3489c3cf3148ab00dc2f45d7066d15f38af3
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * tegra_wm8903.c - Tegra machine ASoC driver for boards using WM8903 codec.
5 * Author: Stephen Warren <swarren@nvidia.com>
6 * Copyright (C) 2010-2012 - NVIDIA, Inc.
8 * Based on code copyright/by:
10 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
12 * Copyright 2007 Wolfson Microelectronics PLC.
13 * Author: Graeme Gregory
14 * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/gpio.h>
21 #include <linux/of_gpio.h>
23 #include <sound/core.h>
24 #include <sound/jack.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
29 #include "../codecs/wm8903.h"
31 #include "tegra_asoc_utils.h"
33 #define DRV_NAME "tegra-snd-wm8903"
35 struct tegra_wm8903 {
36 int gpio_spkr_en;
37 int gpio_hp_det;
38 int gpio_hp_mute;
39 int gpio_int_mic_en;
40 int gpio_ext_mic_en;
41 struct tegra_asoc_utils_data util_data;
44 static int tegra_wm8903_hw_params(struct snd_pcm_substream *substream,
45 struct snd_pcm_hw_params *params)
47 struct snd_soc_pcm_runtime *rtd = substream->private_data;
48 struct snd_soc_dai *codec_dai = rtd->codec_dai;
49 struct snd_soc_card *card = rtd->card;
50 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
51 int srate, mclk;
52 int err;
54 srate = params_rate(params);
55 switch (srate) {
56 case 64000:
57 case 88200:
58 case 96000:
59 mclk = 128 * srate;
60 break;
61 default:
62 mclk = 256 * srate;
63 break;
65 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
66 while (mclk < 6000000)
67 mclk *= 2;
69 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
70 if (err < 0) {
71 dev_err(card->dev, "Can't configure clocks\n");
72 return err;
75 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
76 SND_SOC_CLOCK_IN);
77 if (err < 0) {
78 dev_err(card->dev, "codec_dai clock not set\n");
79 return err;
82 return 0;
85 static const struct snd_soc_ops tegra_wm8903_ops = {
86 .hw_params = tegra_wm8903_hw_params,
89 static struct snd_soc_jack tegra_wm8903_hp_jack;
91 static struct snd_soc_jack_pin tegra_wm8903_hp_jack_pins[] = {
93 .pin = "Headphone Jack",
94 .mask = SND_JACK_HEADPHONE,
98 static struct snd_soc_jack_gpio tegra_wm8903_hp_jack_gpio = {
99 .name = "headphone detect",
100 .report = SND_JACK_HEADPHONE,
101 .debounce_time = 150,
102 .invert = 1,
105 static struct snd_soc_jack tegra_wm8903_mic_jack;
107 static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = {
109 .pin = "Mic Jack",
110 .mask = SND_JACK_MICROPHONE,
114 static int tegra_wm8903_event_int_spk(struct snd_soc_dapm_widget *w,
115 struct snd_kcontrol *k, int event)
117 struct snd_soc_dapm_context *dapm = w->dapm;
118 struct snd_soc_card *card = dapm->card;
119 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
121 if (!gpio_is_valid(machine->gpio_spkr_en))
122 return 0;
124 gpio_set_value_cansleep(machine->gpio_spkr_en,
125 SND_SOC_DAPM_EVENT_ON(event));
127 return 0;
130 static int tegra_wm8903_event_hp(struct snd_soc_dapm_widget *w,
131 struct snd_kcontrol *k, int event)
133 struct snd_soc_dapm_context *dapm = w->dapm;
134 struct snd_soc_card *card = dapm->card;
135 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
137 if (!gpio_is_valid(machine->gpio_hp_mute))
138 return 0;
140 gpio_set_value_cansleep(machine->gpio_hp_mute,
141 !SND_SOC_DAPM_EVENT_ON(event));
143 return 0;
146 static const struct snd_soc_dapm_widget tegra_wm8903_dapm_widgets[] = {
147 SND_SOC_DAPM_SPK("Int Spk", tegra_wm8903_event_int_spk),
148 SND_SOC_DAPM_HP("Headphone Jack", tegra_wm8903_event_hp),
149 SND_SOC_DAPM_MIC("Mic Jack", NULL),
152 static const struct snd_kcontrol_new tegra_wm8903_controls[] = {
153 SOC_DAPM_PIN_SWITCH("Int Spk"),
156 static int tegra_wm8903_init(struct snd_soc_pcm_runtime *rtd)
158 struct snd_soc_dai *codec_dai = rtd->codec_dai;
159 struct snd_soc_component *component = codec_dai->component;
160 struct snd_soc_card *card = rtd->card;
161 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
163 if (gpio_is_valid(machine->gpio_hp_det)) {
164 tegra_wm8903_hp_jack_gpio.gpio = machine->gpio_hp_det;
165 snd_soc_card_jack_new(rtd->card, "Headphone Jack",
166 SND_JACK_HEADPHONE, &tegra_wm8903_hp_jack,
167 tegra_wm8903_hp_jack_pins,
168 ARRAY_SIZE(tegra_wm8903_hp_jack_pins));
169 snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack,
171 &tegra_wm8903_hp_jack_gpio);
174 snd_soc_card_jack_new(rtd->card, "Mic Jack", SND_JACK_MICROPHONE,
175 &tegra_wm8903_mic_jack,
176 tegra_wm8903_mic_jack_pins,
177 ARRAY_SIZE(tegra_wm8903_mic_jack_pins));
178 wm8903_mic_detect(component, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE,
181 snd_soc_dapm_force_enable_pin(&card->dapm, "MICBIAS");
183 return 0;
186 static int tegra_wm8903_remove(struct snd_soc_card *card)
188 struct snd_soc_pcm_runtime *rtd =
189 snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
190 struct snd_soc_dai *codec_dai = rtd->codec_dai;
191 struct snd_soc_component *component = codec_dai->component;
193 wm8903_mic_detect(component, NULL, 0, 0);
195 return 0;
198 SND_SOC_DAILINK_DEFS(hifi,
199 DAILINK_COMP_ARRAY(COMP_EMPTY()),
200 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8903-hifi")),
201 DAILINK_COMP_ARRAY(COMP_EMPTY()));
203 static struct snd_soc_dai_link tegra_wm8903_dai = {
204 .name = "WM8903",
205 .stream_name = "WM8903 PCM",
206 .init = tegra_wm8903_init,
207 .ops = &tegra_wm8903_ops,
208 .dai_fmt = SND_SOC_DAIFMT_I2S |
209 SND_SOC_DAIFMT_NB_NF |
210 SND_SOC_DAIFMT_CBS_CFS,
211 SND_SOC_DAILINK_REG(hifi),
214 static struct snd_soc_card snd_soc_tegra_wm8903 = {
215 .name = "tegra-wm8903",
216 .owner = THIS_MODULE,
217 .dai_link = &tegra_wm8903_dai,
218 .num_links = 1,
219 .remove = tegra_wm8903_remove,
220 .controls = tegra_wm8903_controls,
221 .num_controls = ARRAY_SIZE(tegra_wm8903_controls),
222 .dapm_widgets = tegra_wm8903_dapm_widgets,
223 .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
224 .fully_routed = true,
227 static int tegra_wm8903_driver_probe(struct platform_device *pdev)
229 struct device_node *np = pdev->dev.of_node;
230 struct snd_soc_card *card = &snd_soc_tegra_wm8903;
231 struct tegra_wm8903 *machine;
232 int ret;
234 machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8903),
235 GFP_KERNEL);
236 if (!machine)
237 return -ENOMEM;
239 card->dev = &pdev->dev;
240 snd_soc_card_set_drvdata(card, machine);
242 machine->gpio_spkr_en = of_get_named_gpio(np, "nvidia,spkr-en-gpios",
244 if (machine->gpio_spkr_en == -EPROBE_DEFER)
245 return -EPROBE_DEFER;
246 if (gpio_is_valid(machine->gpio_spkr_en)) {
247 ret = devm_gpio_request_one(&pdev->dev, machine->gpio_spkr_en,
248 GPIOF_OUT_INIT_LOW, "spkr_en");
249 if (ret) {
250 dev_err(card->dev, "cannot get spkr_en gpio\n");
251 return ret;
255 machine->gpio_hp_mute = of_get_named_gpio(np, "nvidia,hp-mute-gpios",
257 if (machine->gpio_hp_mute == -EPROBE_DEFER)
258 return -EPROBE_DEFER;
259 if (gpio_is_valid(machine->gpio_hp_mute)) {
260 ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_mute,
261 GPIOF_OUT_INIT_HIGH, "hp_mute");
262 if (ret) {
263 dev_err(card->dev, "cannot get hp_mute gpio\n");
264 return ret;
268 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
269 if (machine->gpio_hp_det == -EPROBE_DEFER)
270 return -EPROBE_DEFER;
272 machine->gpio_int_mic_en = of_get_named_gpio(np,
273 "nvidia,int-mic-en-gpios", 0);
274 if (machine->gpio_int_mic_en == -EPROBE_DEFER)
275 return -EPROBE_DEFER;
276 if (gpio_is_valid(machine->gpio_int_mic_en)) {
277 /* Disable int mic; enable signal is active-high */
278 ret = devm_gpio_request_one(&pdev->dev,
279 machine->gpio_int_mic_en,
280 GPIOF_OUT_INIT_LOW, "int_mic_en");
281 if (ret) {
282 dev_err(card->dev, "cannot get int_mic_en gpio\n");
283 return ret;
287 machine->gpio_ext_mic_en = of_get_named_gpio(np,
288 "nvidia,ext-mic-en-gpios", 0);
289 if (machine->gpio_ext_mic_en == -EPROBE_DEFER)
290 return -EPROBE_DEFER;
291 if (gpio_is_valid(machine->gpio_ext_mic_en)) {
292 /* Enable ext mic; enable signal is active-low */
293 ret = devm_gpio_request_one(&pdev->dev,
294 machine->gpio_ext_mic_en,
295 GPIOF_OUT_INIT_LOW, "ext_mic_en");
296 if (ret) {
297 dev_err(card->dev, "cannot get ext_mic_en gpio\n");
298 return ret;
302 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
303 if (ret)
304 goto err;
306 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
307 if (ret)
308 goto err;
310 tegra_wm8903_dai.codecs->of_node = of_parse_phandle(np,
311 "nvidia,audio-codec", 0);
312 if (!tegra_wm8903_dai.codecs->of_node) {
313 dev_err(&pdev->dev,
314 "Property 'nvidia,audio-codec' missing or invalid\n");
315 ret = -EINVAL;
316 goto err;
319 tegra_wm8903_dai.cpus->of_node = of_parse_phandle(np,
320 "nvidia,i2s-controller", 0);
321 if (!tegra_wm8903_dai.cpus->of_node) {
322 dev_err(&pdev->dev,
323 "Property 'nvidia,i2s-controller' missing or invalid\n");
324 ret = -EINVAL;
325 goto err;
328 tegra_wm8903_dai.platforms->of_node = tegra_wm8903_dai.cpus->of_node;
330 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
331 if (ret)
332 goto err;
334 ret = snd_soc_register_card(card);
335 if (ret) {
336 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
337 ret);
338 goto err_fini_utils;
341 return 0;
343 err_fini_utils:
344 tegra_asoc_utils_fini(&machine->util_data);
345 err:
346 return ret;
349 static int tegra_wm8903_driver_remove(struct platform_device *pdev)
351 struct snd_soc_card *card = platform_get_drvdata(pdev);
352 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
354 snd_soc_unregister_card(card);
356 tegra_asoc_utils_fini(&machine->util_data);
358 return 0;
361 static const struct of_device_id tegra_wm8903_of_match[] = {
362 { .compatible = "nvidia,tegra-audio-wm8903", },
366 static struct platform_driver tegra_wm8903_driver = {
367 .driver = {
368 .name = DRV_NAME,
369 .pm = &snd_soc_pm_ops,
370 .of_match_table = tegra_wm8903_of_match,
372 .probe = tegra_wm8903_driver_probe,
373 .remove = tegra_wm8903_driver_remove,
375 module_platform_driver(tegra_wm8903_driver);
377 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
378 MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
379 MODULE_LICENSE("GPL");
380 MODULE_ALIAS("platform:" DRV_NAME);
381 MODULE_DEVICE_TABLE(of, tegra_wm8903_of_match);