Merge series "ASoC: Intel: machine driver updates for 5.9" from Pierre-Louis Bossart...
[platform/kernel/linux-rpi.git] / sound / soc / intel / boards / bdw-rt5677.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ASoC machine driver for Intel Broadwell platforms with RT5677 codec
4  *
5  * Copyright (c) 2014, The Chromium OS Authors.  All rights reserved.
6  */
7
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>
19
20 #include "../common/sst-dsp.h"
21 #include "../haswell/sst-haswell-ipc.h"
22
23 #include "../../codecs/rt5677.h"
24
25 struct bdw_rt5677_priv {
26         struct gpio_desc *gpio_hp_en;
27         struct snd_soc_component *component;
28 };
29
30 static int bdw_rt5677_event_hp(struct snd_soc_dapm_widget *w,
31                         struct snd_kcontrol *k, int event)
32 {
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);
36
37         if (SND_SOC_DAPM_EVENT_ON(event))
38                 msleep(70);
39
40         gpiod_set_value_cansleep(bdw_rt5677->gpio_hp_en,
41                 SND_SOC_DAPM_EVENT_ON(event));
42
43         return 0;
44 }
45
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),
52 };
53
54 static const struct snd_soc_dapm_route bdw_rt5677_map[] = {
55         /* Speakers */
56         {"Speaker", NULL, "PDM1L"},
57         {"Speaker", NULL, "PDM1R"},
58
59         /* Headset jack connectors */
60         {"Headphone", NULL, "LOUT1"},
61         {"Headphone", NULL, "LOUT2"},
62         {"IN1P", NULL, "Headset Mic"},
63         {"IN1N", NULL, "Headset Mic"},
64
65         /* Digital MICs
66          * Local DMICs: the two DMICs on the mainboard
67          * Remote DMICs: the two DMICs on the camera module
68          */
69         {"DMIC L1", NULL, "Remote DMICs"},
70         {"DMIC R1", NULL, "Remote DMICs"},
71         {"DMIC L2", NULL, "Local DMICs"},
72         {"DMIC R2", NULL, "Local DMICs"},
73
74         /* CODEC BE connections */
75         {"SSP0 CODEC IN", NULL, "AIF1 Capture"},
76         {"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
77         {"DSP Capture", NULL, "DSP Buffer"},
78
79         /* DSP Clock Connections */
80         { "DSP Buffer", NULL, "SSP0 CODEC IN" },
81         { "SSP0 CODEC IN", NULL, "DSPTX" },
82 };
83
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"),
90 };
91
92
93 static struct snd_soc_jack headphone_jack;
94 static struct snd_soc_jack mic_jack;
95
96 static struct snd_soc_jack_pin headphone_jack_pin = {
97         .pin    = "Headphone",
98         .mask   = SND_JACK_HEADPHONE,
99 };
100
101 static struct snd_soc_jack_pin mic_jack_pin = {
102         .pin    = "Headset Mic",
103         .mask   = SND_JACK_MICROPHONE,
104 };
105
106 static struct snd_soc_jack_gpio headphone_jack_gpio = {
107         .name                   = "plug-det",
108         .report                 = SND_JACK_HEADPHONE,
109         .debounce_time          = 200,
110 };
111
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,
117 };
118
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,
126 };
127
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 };
131
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 },
137 };
138
139 static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
140                         struct snd_pcm_hw_params *params)
141 {
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);
146
147         /* The ADSP will covert the FE rate to 48k, stereo */
148         rate->min = rate->max = 48000;
149         channels->min = channels->max = 2;
150
151         /* set SSP0 to 16 bit */
152         params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
153         return 0;
154 }
155
156 static int bdw_rt5677_hw_params(struct snd_pcm_substream *substream,
157         struct snd_pcm_hw_params *params)
158 {
159         struct snd_soc_pcm_runtime *rtd = substream->private_data;
160         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
161         int ret;
162
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;
168         }
169
170         return ret;
171 }
172
173 static int bdw_rt5677_dsp_hw_params(struct snd_pcm_substream *substream,
174         struct snd_pcm_hw_params *params)
175 {
176         struct snd_soc_pcm_runtime *rtd = substream->private_data;
177         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
178         int ret;
179
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;
185         }
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;
191         }
192
193         return 0;
194 }
195
196 static const struct snd_soc_ops bdw_rt5677_ops = {
197         .hw_params = bdw_rt5677_hw_params,
198 };
199
200 static const struct snd_soc_ops bdw_rt5677_dsp_ops = {
201         .hw_params = bdw_rt5677_dsp_hw_params,
202 };
203
204 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
205 static int bdw_rt5677_rtd_init(struct snd_soc_pcm_runtime *rtd)
206 {
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;
211
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;
219         }
220
221         return 0;
222 }
223 #endif
224
225 static const unsigned int channels[] = {
226         2,
227 };
228
229 static const struct snd_pcm_hw_constraint_list constraints_channels = {
230         .count = ARRAY_SIZE(channels),
231         .list = channels,
232         .mask = 0,
233 };
234
235 static int bdw_rt5677_fe_startup(struct snd_pcm_substream *substream)
236 {
237         struct snd_pcm_runtime *runtime = substream->runtime;
238
239         /* Board supports stereo configuration only */
240         runtime->hw.channels_max = 2;
241         return snd_pcm_hw_constraint_list(runtime, 0,
242                                           SNDRV_PCM_HW_PARAM_CHANNELS,
243                                           &constraints_channels);
244 }
245
246 static const struct snd_soc_ops bdw_rt5677_fe_ops = {
247         .startup = bdw_rt5677_fe_startup,
248 };
249
250 static int bdw_rt5677_init(struct snd_soc_pcm_runtime *rtd)
251 {
252         struct bdw_rt5677_priv *bdw_rt5677 =
253                         snd_soc_card_get_drvdata(rtd->card);
254         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
255         struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
256         int ret;
257
258         ret = devm_acpi_dev_add_driver_gpios(component->dev, bdw_rt5677_gpios);
259         if (ret)
260                 dev_warn(component->dev, "Failed to add driver gpios\n");
261
262         /* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1.
263          * The ASRC clock source is clk_i2s1_asrc.
264          */
265         rt5677_sel_asrc_clk_src(component, RT5677_DA_STEREO_FILTER |
266                         RT5677_AD_STEREO1_FILTER | RT5677_I2S1_SOURCE,
267                         RT5677_CLK_SEL_I2S1_ASRC);
268         /* Enable codec ASRC function for Mono ADC L.
269          * The ASRC clock source is clk_sys2_asrc.
270          */
271         rt5677_sel_asrc_clk_src(component, RT5677_AD_MONO_L_FILTER,
272                         RT5677_CLK_SEL_SYS2);
273
274         /* Request rt5677 GPIO for headphone amp control */
275         bdw_rt5677->gpio_hp_en = gpiod_get(component->dev, "headphone-enable",
276                                            GPIOD_OUT_LOW);
277         if (IS_ERR(bdw_rt5677->gpio_hp_en)) {
278                 dev_err(component->dev, "Can't find HP_AMP_SHDN_L gpio\n");
279                 return PTR_ERR(bdw_rt5677->gpio_hp_en);
280         }
281
282         /* Create and initialize headphone jack */
283         if (!snd_soc_card_jack_new(rtd->card, "Headphone Jack",
284                         SND_JACK_HEADPHONE, &headphone_jack,
285                         &headphone_jack_pin, 1)) {
286                 headphone_jack_gpio.gpiod_dev = component->dev;
287                 if (snd_soc_jack_add_gpios(&headphone_jack, 1,
288                                 &headphone_jack_gpio))
289                         dev_err(component->dev, "Can't add headphone jack gpio\n");
290         } else {
291                 dev_err(component->dev, "Can't create headphone jack\n");
292         }
293
294         /* Create and initialize mic jack */
295         if (!snd_soc_card_jack_new(rtd->card, "Mic Jack",
296                         SND_JACK_MICROPHONE, &mic_jack,
297                         &mic_jack_pin, 1)) {
298                 mic_jack_gpio.gpiod_dev = component->dev;
299                 if (snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio))
300                         dev_err(component->dev, "Can't add mic jack gpio\n");
301         } else {
302                 dev_err(component->dev, "Can't create mic jack\n");
303         }
304         bdw_rt5677->component = component;
305
306         snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
307         return 0;
308 }
309
310 static void bdw_rt5677_exit(struct snd_soc_pcm_runtime *rtd)
311 {
312         struct bdw_rt5677_priv *bdw_rt5677 =
313                         snd_soc_card_get_drvdata(rtd->card);
314
315         /*
316          * The .exit() can be reached without going through the .init()
317          * so explicitly test if the gpiod is valid
318          */
319         if (!IS_ERR_OR_NULL(bdw_rt5677->gpio_hp_en))
320                 gpiod_put(bdw_rt5677->gpio_hp_en);
321 }
322
323 /* broadwell digital audio interface glue - connects codec <--> CPU */
324 SND_SOC_DAILINK_DEF(dummy,
325         DAILINK_COMP_ARRAY(COMP_DUMMY()));
326
327 SND_SOC_DAILINK_DEF(fe,
328         DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
329
330 SND_SOC_DAILINK_DEF(platform,
331         DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio")));
332
333 SND_SOC_DAILINK_DEF(be,
334         DAILINK_COMP_ARRAY(COMP_CODEC("i2c-RT5677CE:00", "rt5677-aif1")));
335
336 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
337 SND_SOC_DAILINK_DEF(ssp0_port,
338             DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
339 #endif
340
341 /* Wake on voice interface */
342 SND_SOC_DAILINK_DEFS(dsp,
343         DAILINK_COMP_ARRAY(COMP_CPU("spi-RT5677AA:00")),
344         DAILINK_COMP_ARRAY(COMP_CODEC("i2c-RT5677CE:00", "rt5677-dspbuffer")),
345         DAILINK_COMP_ARRAY(COMP_PLATFORM("spi-RT5677AA:00")));
346
347 static struct snd_soc_dai_link bdw_rt5677_dais[] = {
348         /* Front End DAI links */
349         {
350                 .name = "System PCM",
351                 .stream_name = "System Playback/Capture",
352                 .dynamic = 1,
353 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
354                 .init = bdw_rt5677_rtd_init,
355 #endif
356                 .trigger = {
357                         SND_SOC_DPCM_TRIGGER_POST,
358                         SND_SOC_DPCM_TRIGGER_POST
359                 },
360                 .dpcm_capture = 1,
361                 .dpcm_playback = 1,
362                 .ops = &bdw_rt5677_fe_ops,
363                 SND_SOC_DAILINK_REG(fe, dummy, platform),
364         },
365
366         /* Non-DPCM links */
367         {
368                 .name = "Codec DSP",
369                 .stream_name = "Wake on Voice",
370                 .capture_only = 1,
371                 .ops = &bdw_rt5677_dsp_ops,
372                 SND_SOC_DAILINK_REG(dsp),
373         },
374
375         /* Back End DAI links */
376         {
377                 /* SSP0 - Codec */
378                 .name = "Codec",
379                 .id = 0,
380                 .no_pcm = 1,
381                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
382                         SND_SOC_DAIFMT_CBS_CFS,
383                 .ignore_pmdown_time = 1,
384                 .be_hw_params_fixup = broadwell_ssp0_fixup,
385                 .ops = &bdw_rt5677_ops,
386                 .dpcm_playback = 1,
387                 .dpcm_capture = 1,
388                 .init = bdw_rt5677_init,
389                 .exit = bdw_rt5677_exit,
390 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
391                 SND_SOC_DAILINK_REG(dummy, be, dummy),
392 #else
393                 SND_SOC_DAILINK_REG(ssp0_port, be, platform),
394 #endif
395         },
396 };
397
398 static int bdw_rt5677_suspend_pre(struct snd_soc_card *card)
399 {
400         struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
401         struct snd_soc_dapm_context *dapm;
402
403         if (bdw_rt5677->component) {
404                 dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
405                 snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
406         }
407         return 0;
408 }
409
410 static int bdw_rt5677_resume_post(struct snd_soc_card *card)
411 {
412         struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card);
413         struct snd_soc_dapm_context *dapm;
414
415         if (bdw_rt5677->component) {
416                 dapm = snd_soc_component_get_dapm(bdw_rt5677->component);
417                 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
418         }
419         return 0;
420 }
421
422 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
423 /* use space before codec name to simplify card ID, and simplify driver name */
424 #define CARD_NAME "bdw rt5677" /* card name will be 'sof-bdw rt5677' */
425 #define DRIVER_NAME "SOF"
426 #else
427 #define CARD_NAME "bdw-rt5677"
428 #define DRIVER_NAME NULL /* card name will be used for driver name */
429 #endif
430
431 /* ASoC machine driver for Broadwell DSP + RT5677 */
432 static struct snd_soc_card bdw_rt5677_card = {
433         .name = CARD_NAME,
434         .driver_name = DRIVER_NAME,
435         .owner = THIS_MODULE,
436         .dai_link = bdw_rt5677_dais,
437         .num_links = ARRAY_SIZE(bdw_rt5677_dais),
438         .dapm_widgets = bdw_rt5677_widgets,
439         .num_dapm_widgets = ARRAY_SIZE(bdw_rt5677_widgets),
440         .dapm_routes = bdw_rt5677_map,
441         .num_dapm_routes = ARRAY_SIZE(bdw_rt5677_map),
442         .controls = bdw_rt5677_controls,
443         .num_controls = ARRAY_SIZE(bdw_rt5677_controls),
444         .fully_routed = true,
445         .suspend_pre = bdw_rt5677_suspend_pre,
446         .resume_post = bdw_rt5677_resume_post,
447 };
448
449 static int bdw_rt5677_probe(struct platform_device *pdev)
450 {
451         struct bdw_rt5677_priv *bdw_rt5677;
452         struct snd_soc_acpi_mach *mach;
453         int ret;
454
455         bdw_rt5677_card.dev = &pdev->dev;
456
457         /* Allocate driver private struct */
458         bdw_rt5677 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5677_priv),
459                 GFP_KERNEL);
460         if (!bdw_rt5677) {
461                 dev_err(&pdev->dev, "Can't allocate bdw_rt5677\n");
462                 return -ENOMEM;
463         }
464
465         /* override plaform name, if required */
466         mach = pdev->dev.platform_data;
467         ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt5677_card,
468                                                     mach->mach_params.platform);
469         if (ret)
470                 return ret;
471
472         snd_soc_card_set_drvdata(&bdw_rt5677_card, bdw_rt5677);
473
474         return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5677_card);
475 }
476
477 static struct platform_driver bdw_rt5677_audio = {
478         .probe = bdw_rt5677_probe,
479         .driver = {
480                 .name = "bdw-rt5677",
481         },
482 };
483
484 module_platform_driver(bdw_rt5677_audio)
485
486 /* Module information */
487 MODULE_AUTHOR("Ben Zhang");
488 MODULE_DESCRIPTION("Intel Broadwell RT5677 machine driver");
489 MODULE_LICENSE("GPL v2");
490 MODULE_ALIAS("platform:bdw-rt5677");