treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / intel / boards / bdw-rt5677.c
blobbb643c99069dfd9f761955c1c958b1325d6637f5
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ASoC machine driver for Intel Broadwell platforms with RT5677 codec
5 * Copyright (c) 2014, The Chromium OS Authors. All rights reserved.
6 */
8 #include <linux/acpi.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/delay.h>
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/soc.h>
16 #include <sound/pcm_params.h>
17 #include <sound/jack.h>
18 #include <sound/soc-acpi.h>
20 #include "../common/sst-dsp.h"
21 #include "../haswell/sst-haswell-ipc.h"
23 #include "../../codecs/rt5677.h"
25 struct bdw_rt5677_priv {
26 struct gpio_desc *gpio_hp_en;
27 struct snd_soc_component *component;
30 static int bdw_rt5677_event_hp(struct snd_soc_dapm_widget *w,
31 struct snd_kcontrol *k, int event)
33 struct snd_soc_dapm_context *dapm = w->dapm;
34 struct snd_soc_card *card = dapm->card;
35 struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
37 if (SND_SOC_DAPM_EVENT_ON(event))
38 msleep(70);
40 gpiod_set_value_cansleep(bdw_rt5677->gpio_hp_en,
41 SND_SOC_DAPM_EVENT_ON(event));
43 return 0;
46 static const struct snd_soc_dapm_widget bdw_rt5677_widgets[] = {
47 SND_SOC_DAPM_HP("Headphone", bdw_rt5677_event_hp),
48 SND_SOC_DAPM_SPK("Speaker", NULL),
49 SND_SOC_DAPM_MIC("Headset Mic", NULL),
50 SND_SOC_DAPM_MIC("Local DMICs", NULL),
51 SND_SOC_DAPM_MIC("Remote DMICs", NULL),
54 static const struct snd_soc_dapm_route bdw_rt5677_map[] = {
55 /* Speakers */
56 {"Speaker", NULL, "PDM1L"},
57 {"Speaker", NULL, "PDM1R"},
59 /* Headset jack connectors */
60 {"Headphone", NULL, "LOUT1"},
61 {"Headphone", NULL, "LOUT2"},
62 {"IN1P", NULL, "Headset Mic"},
63 {"IN1N", NULL, "Headset Mic"},
65 /* Digital MICs
66 * Local DMICs: the two DMICs on the mainboard
67 * Remote DMICs: the two DMICs on the camera module
69 {"DMIC L1", NULL, "Remote DMICs"},
70 {"DMIC R1", NULL, "Remote DMICs"},
71 {"DMIC L2", NULL, "Local DMICs"},
72 {"DMIC R2", NULL, "Local DMICs"},
74 /* CODEC BE connections */
75 {"SSP0 CODEC IN", NULL, "AIF1 Capture"},
76 {"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
77 {"DSP Capture", NULL, "DSP Buffer"},
79 /* DSP Clock Connections */
80 { "DSP Buffer", NULL, "SSP0 CODEC IN" },
81 { "SSP0 CODEC IN", NULL, "DSPTX" },
84 static const struct snd_kcontrol_new bdw_rt5677_controls[] = {
85 SOC_DAPM_PIN_SWITCH("Speaker"),
86 SOC_DAPM_PIN_SWITCH("Headphone"),
87 SOC_DAPM_PIN_SWITCH("Headset Mic"),
88 SOC_DAPM_PIN_SWITCH("Local DMICs"),
89 SOC_DAPM_PIN_SWITCH("Remote DMICs"),
93 static struct snd_soc_jack headphone_jack;
94 static struct snd_soc_jack mic_jack;
96 static struct snd_soc_jack_pin headphone_jack_pin = {
97 .pin = "Headphone",
98 .mask = SND_JACK_HEADPHONE,
101 static struct snd_soc_jack_pin mic_jack_pin = {
102 .pin = "Headset Mic",
103 .mask = SND_JACK_MICROPHONE,
106 static struct snd_soc_jack_gpio headphone_jack_gpio = {
107 .name = "plug-det",
108 .report = SND_JACK_HEADPHONE,
109 .debounce_time = 200,
112 static struct snd_soc_jack_gpio mic_jack_gpio = {
113 .name = "mic-present",
114 .report = SND_JACK_MICROPHONE,
115 .debounce_time = 200,
116 .invert = 1,
119 /* GPIO indexes defined by ACPI */
120 enum {
121 RT5677_GPIO_PLUG_DET = 0,
122 RT5677_GPIO_MIC_PRESENT_L = 1,
123 RT5677_GPIO_HOTWORD_DET_L = 2,
124 RT5677_GPIO_DSP_INT = 3,
125 RT5677_GPIO_HP_AMP_SHDN_L = 4,
128 static const struct acpi_gpio_params plug_det_gpio = { RT5677_GPIO_PLUG_DET, 0, false };
129 static const struct acpi_gpio_params mic_present_gpio = { RT5677_GPIO_MIC_PRESENT_L, 0, false };
130 static const struct acpi_gpio_params headphone_enable_gpio = { RT5677_GPIO_HP_AMP_SHDN_L, 0, false };
132 static const struct acpi_gpio_mapping bdw_rt5677_gpios[] = {
133 { "plug-det-gpios", &plug_det_gpio, 1 },
134 { "mic-present-gpios", &mic_present_gpio, 1 },
135 { "headphone-enable-gpios", &headphone_enable_gpio, 1 },
136 { NULL },
139 static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
140 struct snd_pcm_hw_params *params)
142 struct snd_interval *rate = hw_param_interval(params,
143 SNDRV_PCM_HW_PARAM_RATE);
144 struct snd_interval *channels = hw_param_interval(params,
145 SNDRV_PCM_HW_PARAM_CHANNELS);
147 /* The ADSP will covert the FE rate to 48k, stereo */
148 rate->min = rate->max = 48000;
149 channels->min = channels->max = 2;
151 /* set SSP0 to 16 bit */
152 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
153 return 0;
156 static int bdw_rt5677_hw_params(struct snd_pcm_substream *substream,
157 struct snd_pcm_hw_params *params)
159 struct snd_soc_pcm_runtime *rtd = substream->private_data;
160 struct snd_soc_dai *codec_dai = rtd->codec_dai;
161 int ret;
163 ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_MCLK, 24576000,
164 SND_SOC_CLOCK_IN);
165 if (ret < 0) {
166 dev_err(rtd->dev, "can't set codec sysclk configuration\n");
167 return ret;
170 return ret;
173 static int bdw_rt5677_dsp_hw_params(struct snd_pcm_substream *substream,
174 struct snd_pcm_hw_params *params)
176 struct snd_soc_pcm_runtime *rtd = substream->private_data;
177 struct snd_soc_dai *codec_dai = rtd->codec_dai;
178 int ret;
180 ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_PLL1, 24576000,
181 SND_SOC_CLOCK_IN);
182 if (ret < 0) {
183 dev_err(rtd->dev, "can't set codec sysclk configuration\n");
184 return ret;
186 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5677_PLL1_S_MCLK,
187 24000000, 24576000);
188 if (ret < 0) {
189 dev_err(rtd->dev, "can't set codec pll configuration\n");
190 return ret;
193 return 0;
196 static const struct snd_soc_ops bdw_rt5677_ops = {
197 .hw_params = bdw_rt5677_hw_params,
200 static const struct snd_soc_ops bdw_rt5677_dsp_ops = {
201 .hw_params = bdw_rt5677_dsp_hw_params,
204 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
205 static int bdw_rt5677_rtd_init(struct snd_soc_pcm_runtime *rtd)
207 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
208 struct sst_pdata *pdata = dev_get_platdata(component->dev);
209 struct sst_hsw *broadwell = pdata->dsp;
210 int ret;
212 /* Set ADSP SSP port settings */
213 ret = sst_hsw_device_set_config(broadwell, SST_HSW_DEVICE_SSP_0,
214 SST_HSW_DEVICE_MCLK_FREQ_24_MHZ,
215 SST_HSW_DEVICE_CLOCK_MASTER, 9);
216 if (ret < 0) {
217 dev_err(rtd->dev, "error: failed to set device config\n");
218 return ret;
221 return 0;
223 #endif
225 static int bdw_rt5677_init(struct snd_soc_pcm_runtime *rtd)
227 struct bdw_rt5677_priv *bdw_rt5677 =
228 snd_soc_card_get_drvdata(rtd->card);
229 struct snd_soc_component *component = rtd->codec_dai->component;
230 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
231 int ret;
233 ret = devm_acpi_dev_add_driver_gpios(component->dev, bdw_rt5677_gpios);
234 if (ret)
235 dev_warn(component->dev, "Failed to add driver gpios\n");
237 /* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1.
238 * The ASRC clock source is clk_i2s1_asrc.
240 rt5677_sel_asrc_clk_src(component, RT5677_DA_STEREO_FILTER |
241 RT5677_AD_STEREO1_FILTER | RT5677_I2S1_SOURCE,
242 RT5677_CLK_SEL_I2S1_ASRC);
243 /* Enable codec ASRC function for Mono ADC L.
244 * The ASRC clock source is clk_sys2_asrc.
246 rt5677_sel_asrc_clk_src(component, RT5677_AD_MONO_L_FILTER,
247 RT5677_CLK_SEL_SYS2);
249 /* Request rt5677 GPIO for headphone amp control */
250 bdw_rt5677->gpio_hp_en = devm_gpiod_get(component->dev, "headphone-enable",
251 GPIOD_OUT_LOW);
252 if (IS_ERR(bdw_rt5677->gpio_hp_en)) {
253 dev_err(component->dev, "Can't find HP_AMP_SHDN_L gpio\n");
254 return PTR_ERR(bdw_rt5677->gpio_hp_en);
257 /* Create and initialize headphone jack */
258 if (!snd_soc_card_jack_new(rtd->card, "Headphone Jack",
259 SND_JACK_HEADPHONE, &headphone_jack,
260 &headphone_jack_pin, 1)) {
261 headphone_jack_gpio.gpiod_dev = component->dev;
262 if (snd_soc_jack_add_gpios(&headphone_jack, 1,
263 &headphone_jack_gpio))
264 dev_err(component->dev, "Can't add headphone jack gpio\n");
265 } else {
266 dev_err(component->dev, "Can't create headphone jack\n");
269 /* Create and initialize mic jack */
270 if (!snd_soc_card_jack_new(rtd->card, "Mic Jack",
271 SND_JACK_MICROPHONE, &mic_jack,
272 &mic_jack_pin, 1)) {
273 mic_jack_gpio.gpiod_dev = component->dev;
274 if (snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio))
275 dev_err(component->dev, "Can't add mic jack gpio\n");
276 } else {
277 dev_err(component->dev, "Can't create mic jack\n");
279 bdw_rt5677->component = component;
281 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
282 return 0;
285 /* broadwell digital audio interface glue - connects codec <--> CPU */
286 SND_SOC_DAILINK_DEF(dummy,
287 DAILINK_COMP_ARRAY(COMP_DUMMY()));
289 SND_SOC_DAILINK_DEF(fe,
290 DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
292 SND_SOC_DAILINK_DEF(platform,
293 DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio")));
295 SND_SOC_DAILINK_DEF(be,
296 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-RT5677CE:00", "rt5677-aif1")));
298 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
299 SND_SOC_DAILINK_DEF(ssp0_port,
300 DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
301 #else
302 SND_SOC_DAILINK_DEF(ssp0_port,
303 DAILINK_COMP_ARRAY(COMP_DUMMY()));
304 #endif
306 /* Wake on voice interface */
307 SND_SOC_DAILINK_DEFS(dsp,
308 DAILINK_COMP_ARRAY(COMP_CPU("spi-RT5677AA:00")),
309 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-RT5677CE:00", "rt5677-dspbuffer")),
310 DAILINK_COMP_ARRAY(COMP_PLATFORM("spi-RT5677AA:00")));
312 static struct snd_soc_dai_link bdw_rt5677_dais[] = {
313 /* Front End DAI links */
315 .name = "System PCM",
316 .stream_name = "System Playback/Capture",
317 .dynamic = 1,
318 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
319 .init = bdw_rt5677_rtd_init,
320 #endif
321 .trigger = {
322 SND_SOC_DPCM_TRIGGER_POST,
323 SND_SOC_DPCM_TRIGGER_POST
325 .dpcm_capture = 1,
326 .dpcm_playback = 1,
327 SND_SOC_DAILINK_REG(fe, dummy, platform),
330 /* Non-DPCM links */
332 .name = "Codec DSP",
333 .stream_name = "Wake on Voice",
334 .ops = &bdw_rt5677_dsp_ops,
335 SND_SOC_DAILINK_REG(dsp),
338 /* Back End DAI links */
340 /* SSP0 - Codec */
341 .name = "Codec",
342 .id = 0,
343 .no_pcm = 1,
344 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
345 SND_SOC_DAIFMT_CBS_CFS,
346 .ignore_suspend = 1,
347 .ignore_pmdown_time = 1,
348 .be_hw_params_fixup = broadwell_ssp0_fixup,
349 .ops = &bdw_rt5677_ops,
350 .dpcm_playback = 1,
351 .dpcm_capture = 1,
352 .init = bdw_rt5677_init,
353 SND_SOC_DAILINK_REG(ssp0_port, be, platform),
357 static int bdw_rt5677_suspend_pre(struct snd_soc_card *card)
359 struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
360 struct snd_soc_dapm_context *dapm;
362 if (bdw_rt5677->component) {
363 dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
364 snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
366 return 0;
369 static int bdw_rt5677_resume_post(struct snd_soc_card *card)
371 struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
372 struct snd_soc_dapm_context *dapm;
374 if (bdw_rt5677->component) {
375 dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
376 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
378 return 0;
381 /* ASoC machine driver for Broadwell DSP + RT5677 */
382 static struct snd_soc_card bdw_rt5677_card = {
383 .name = "bdw-rt5677",
384 .owner = THIS_MODULE,
385 .dai_link = bdw_rt5677_dais,
386 .num_links = ARRAY_SIZE(bdw_rt5677_dais),
387 .dapm_widgets = bdw_rt5677_widgets,
388 .num_dapm_widgets = ARRAY_SIZE(bdw_rt5677_widgets),
389 .dapm_routes = bdw_rt5677_map,
390 .num_dapm_routes = ARRAY_SIZE(bdw_rt5677_map),
391 .controls = bdw_rt5677_controls,
392 .num_controls = ARRAY_SIZE(bdw_rt5677_controls),
393 .fully_routed = true,
394 .suspend_pre = bdw_rt5677_suspend_pre,
395 .resume_post = bdw_rt5677_resume_post,
398 static int bdw_rt5677_probe(struct platform_device *pdev)
400 struct bdw_rt5677_priv *bdw_rt5677;
401 struct snd_soc_acpi_mach *mach;
402 int ret;
404 bdw_rt5677_card.dev = &pdev->dev;
406 /* Allocate driver private struct */
407 bdw_rt5677 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5677_priv),
408 GFP_KERNEL);
409 if (!bdw_rt5677) {
410 dev_err(&pdev->dev, "Can't allocate bdw_rt5677\n");
411 return -ENOMEM;
414 /* override plaform name, if required */
415 mach = (&pdev->dev)->platform_data;
416 ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt5677_card,
417 mach->mach_params.platform);
418 if (ret)
419 return ret;
421 snd_soc_card_set_drvdata(&bdw_rt5677_card, bdw_rt5677);
423 return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5677_card);
426 static struct platform_driver bdw_rt5677_audio = {
427 .probe = bdw_rt5677_probe,
428 .driver = {
429 .name = "bdw-rt5677",
433 module_platform_driver(bdw_rt5677_audio)
435 /* Module information */
436 MODULE_AUTHOR("Ben Zhang");
437 MODULE_DESCRIPTION("Intel Broadwell RT5677 machine driver");
438 MODULE_LICENSE("GPL v2");
439 MODULE_ALIAS("platform:bdw-rt5677");