Merge tag 'x86-urgent-2020-06-11' of git://git.kernel.org/pub/scm/linux/kernel/git...
[platform/kernel/linux-rpi.git] / sound / soc / intel / boards / broadwell.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel Broadwell Wildcatpoint SST Audio
4  *
5  * Copyright (C) 2013, Intel Corporation. All rights reserved.
6  */
7
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <sound/core.h>
11 #include <sound/pcm.h>
12 #include <sound/soc.h>
13 #include <sound/jack.h>
14 #include <sound/pcm_params.h>
15 #include <sound/soc-acpi.h>
16
17 #include "../common/sst-dsp.h"
18 #include "../haswell/sst-haswell-ipc.h"
19
20 #include "../../codecs/rt286.h"
21
22 static struct snd_soc_jack broadwell_headset;
23 /* Headset jack detection DAPM pins */
24 static struct snd_soc_jack_pin broadwell_headset_pins[] = {
25         {
26                 .pin = "Mic Jack",
27                 .mask = SND_JACK_MICROPHONE,
28         },
29         {
30                 .pin = "Headphone Jack",
31                 .mask = SND_JACK_HEADPHONE,
32         },
33 };
34
35 static const struct snd_kcontrol_new broadwell_controls[] = {
36         SOC_DAPM_PIN_SWITCH("Speaker"),
37         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
38 };
39
40 static const struct snd_soc_dapm_widget broadwell_widgets[] = {
41         SND_SOC_DAPM_HP("Headphone Jack", NULL),
42         SND_SOC_DAPM_SPK("Speaker", NULL),
43         SND_SOC_DAPM_MIC("Mic Jack", NULL),
44         SND_SOC_DAPM_MIC("DMIC1", NULL),
45         SND_SOC_DAPM_MIC("DMIC2", NULL),
46         SND_SOC_DAPM_LINE("Line Jack", NULL),
47 };
48
49 static const struct snd_soc_dapm_route broadwell_rt286_map[] = {
50
51         /* speaker */
52         {"Speaker", NULL, "SPOR"},
53         {"Speaker", NULL, "SPOL"},
54
55         /* HP jack connectors - unknown if we have jack deteck */
56         {"Headphone Jack", NULL, "HPO Pin"},
57
58         /* other jacks */
59         {"MIC1", NULL, "Mic Jack"},
60         {"LINE1", NULL, "Line Jack"},
61
62         /* digital mics */
63         {"DMIC1 Pin", NULL, "DMIC1"},
64         {"DMIC2 Pin", NULL, "DMIC2"},
65
66         /* CODEC BE connections */
67         {"SSP0 CODEC IN", NULL, "AIF1 Capture"},
68         {"AIF1 Playback", NULL, "SSP0 CODEC OUT"},
69 };
70
71 static int broadwell_rt286_codec_init(struct snd_soc_pcm_runtime *rtd)
72 {
73         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
74         int ret = 0;
75         ret = snd_soc_card_jack_new(rtd->card, "Headset",
76                 SND_JACK_HEADSET | SND_JACK_BTN_0, &broadwell_headset,
77                 broadwell_headset_pins, ARRAY_SIZE(broadwell_headset_pins));
78         if (ret)
79                 return ret;
80
81         rt286_mic_detect(component, &broadwell_headset);
82         return 0;
83 }
84
85
86 static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd,
87                         struct snd_pcm_hw_params *params)
88 {
89         struct snd_interval *rate = hw_param_interval(params,
90                         SNDRV_PCM_HW_PARAM_RATE);
91         struct snd_interval *channels = hw_param_interval(params,
92                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
93
94         /* The ADSP will covert the FE rate to 48k, stereo */
95         rate->min = rate->max = 48000;
96         channels->min = channels->max = 2;
97
98         /* set SSP0 to 16 bit */
99         params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
100         return 0;
101 }
102
103 static int broadwell_rt286_hw_params(struct snd_pcm_substream *substream,
104         struct snd_pcm_hw_params *params)
105 {
106         struct snd_soc_pcm_runtime *rtd = substream->private_data;
107         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
108         int ret;
109
110         ret = snd_soc_dai_set_sysclk(codec_dai, RT286_SCLK_S_PLL, 24000000,
111                 SND_SOC_CLOCK_IN);
112
113         if (ret < 0) {
114                 dev_err(rtd->dev, "can't set codec sysclk configuration\n");
115                 return ret;
116         }
117
118         return ret;
119 }
120
121 static const struct snd_soc_ops broadwell_rt286_ops = {
122         .hw_params = broadwell_rt286_hw_params,
123 };
124
125 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
126 static int broadwell_rtd_init(struct snd_soc_pcm_runtime *rtd)
127 {
128         struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
129         struct sst_pdata *pdata = dev_get_platdata(component->dev);
130         struct sst_hsw *broadwell = pdata->dsp;
131         int ret;
132
133         /* Set ADSP SSP port settings */
134         ret = sst_hsw_device_set_config(broadwell, SST_HSW_DEVICE_SSP_0,
135                 SST_HSW_DEVICE_MCLK_FREQ_24_MHZ,
136                 SST_HSW_DEVICE_CLOCK_MASTER, 9);
137         if (ret < 0) {
138                 dev_err(rtd->dev, "error: failed to set device config\n");
139                 return ret;
140         }
141
142         return 0;
143 }
144 #endif
145
146 static const unsigned int channels[] = {
147         2,
148 };
149
150 static const struct snd_pcm_hw_constraint_list constraints_channels = {
151         .count = ARRAY_SIZE(channels),
152         .list = channels,
153         .mask = 0,
154 };
155
156 static int broadwell_fe_startup(struct snd_pcm_substream *substream)
157 {
158         struct snd_pcm_runtime *runtime = substream->runtime;
159
160         /* Board supports stereo configuration only */
161         runtime->hw.channels_max = 2;
162         return snd_pcm_hw_constraint_list(runtime, 0,
163                                           SNDRV_PCM_HW_PARAM_CHANNELS,
164                                           &constraints_channels);
165 }
166
167 static const struct snd_soc_ops broadwell_fe_ops = {
168         .startup = broadwell_fe_startup,
169 };
170
171 SND_SOC_DAILINK_DEF(system,
172         DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
173
174 SND_SOC_DAILINK_DEF(offload0,
175         DAILINK_COMP_ARRAY(COMP_CPU("Offload0 Pin")));
176
177 SND_SOC_DAILINK_DEF(offload1,
178         DAILINK_COMP_ARRAY(COMP_CPU("Offload1 Pin")));
179
180 SND_SOC_DAILINK_DEF(loopback,
181         DAILINK_COMP_ARRAY(COMP_CPU("Loopback Pin")));
182
183 SND_SOC_DAILINK_DEF(dummy,
184         DAILINK_COMP_ARRAY(COMP_DUMMY()));
185
186 SND_SOC_DAILINK_DEF(platform,
187         DAILINK_COMP_ARRAY(COMP_PLATFORM("haswell-pcm-audio")));
188
189 SND_SOC_DAILINK_DEF(codec,
190         DAILINK_COMP_ARRAY(COMP_CODEC("i2c-INT343A:00", "rt286-aif1")));
191
192 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
193 SND_SOC_DAILINK_DEF(ssp0_port,
194             DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
195 #endif
196
197 /* broadwell digital audio interface glue - connects codec <--> CPU */
198 static struct snd_soc_dai_link broadwell_rt286_dais[] = {
199         /* Front End DAI links */
200         {
201                 .name = "System PCM",
202                 .stream_name = "System Playback/Capture",
203                 .dynamic = 1,
204 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
205                 .init = broadwell_rtd_init,
206 #endif
207                 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
208                 .ops = &broadwell_fe_ops,
209                 .dpcm_playback = 1,
210                 .dpcm_capture = 1,
211                 SND_SOC_DAILINK_REG(system, dummy, platform),
212         },
213         {
214                 .name = "Offload0",
215                 .stream_name = "Offload0 Playback",
216                 .dynamic = 1,
217                 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
218                 .dpcm_playback = 1,
219                 SND_SOC_DAILINK_REG(offload0, dummy, platform),
220         },
221         {
222                 .name = "Offload1",
223                 .stream_name = "Offload1 Playback",
224                 .dynamic = 1,
225                 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
226                 .dpcm_playback = 1,
227                 SND_SOC_DAILINK_REG(offload1, dummy, platform),
228         },
229         {
230                 .name = "Loopback PCM",
231                 .stream_name = "Loopback",
232                 .dynamic = 1,
233                 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
234                 .dpcm_capture = 1,
235                 SND_SOC_DAILINK_REG(loopback, dummy, platform),
236         },
237         /* Back End DAI links */
238         {
239                 /* SSP0 - Codec */
240                 .name = "Codec",
241                 .id = 0,
242                 .no_pcm = 1,
243                 .init = broadwell_rt286_codec_init,
244                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
245                         SND_SOC_DAIFMT_CBS_CFS,
246                 .ignore_pmdown_time = 1,
247                 .be_hw_params_fixup = broadwell_ssp0_fixup,
248                 .ops = &broadwell_rt286_ops,
249                 .dpcm_playback = 1,
250                 .dpcm_capture = 1,
251 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
252                 SND_SOC_DAILINK_REG(dummy, codec, dummy),
253 #else
254                 SND_SOC_DAILINK_REG(ssp0_port, codec, platform),
255 #endif
256         },
257 };
258
259 static int broadwell_disable_jack(struct snd_soc_card *card)
260 {
261         struct snd_soc_component *component;
262
263         for_each_card_components(card, component) {
264                 if (!strcmp(component->name, "i2c-INT343A:00")) {
265
266                         dev_dbg(component->dev, "disabling jack detect before going to suspend.\n");
267                         rt286_mic_detect(component, NULL);
268                         break;
269                 }
270         }
271
272         return 0;
273 }
274
275 static int broadwell_suspend(struct snd_soc_card *card)
276 {
277         return broadwell_disable_jack(card);
278 }
279
280 static int broadwell_resume(struct snd_soc_card *card){
281         struct snd_soc_component *component;
282
283         for_each_card_components(card, component) {
284                 if (!strcmp(component->name, "i2c-INT343A:00")) {
285
286                         dev_dbg(component->dev, "enabling jack detect for resume.\n");
287                         rt286_mic_detect(component, &broadwell_headset);
288                         break;
289                 }
290         }
291         return 0;
292 }
293
294 /* broadwell audio machine driver for WPT + RT286S */
295 static struct snd_soc_card broadwell_rt286 = {
296         .name = "broadwell-rt286",
297         .owner = THIS_MODULE,
298         .dai_link = broadwell_rt286_dais,
299         .num_links = ARRAY_SIZE(broadwell_rt286_dais),
300         .controls = broadwell_controls,
301         .num_controls = ARRAY_SIZE(broadwell_controls),
302         .dapm_widgets = broadwell_widgets,
303         .num_dapm_widgets = ARRAY_SIZE(broadwell_widgets),
304         .dapm_routes = broadwell_rt286_map,
305         .num_dapm_routes = ARRAY_SIZE(broadwell_rt286_map),
306         .fully_routed = true,
307         .suspend_pre = broadwell_suspend,
308         .resume_post = broadwell_resume,
309 };
310
311 static int broadwell_audio_probe(struct platform_device *pdev)
312 {
313         struct snd_soc_acpi_mach *mach;
314         int ret;
315
316         broadwell_rt286.dev = &pdev->dev;
317
318         /* override plaform name, if required */
319         mach = pdev->dev.platform_data;
320         ret = snd_soc_fixup_dai_links_platform_name(&broadwell_rt286,
321                                                     mach->mach_params.platform);
322         if (ret)
323                 return ret;
324
325         return devm_snd_soc_register_card(&pdev->dev, &broadwell_rt286);
326 }
327
328 static int broadwell_audio_remove(struct platform_device *pdev)
329 {
330         struct snd_soc_card *card = platform_get_drvdata(pdev);
331
332         return broadwell_disable_jack(card);
333 }
334
335 static struct platform_driver broadwell_audio = {
336         .probe = broadwell_audio_probe,
337         .remove = broadwell_audio_remove,
338         .driver = {
339                 .name = "broadwell-audio",
340         },
341 };
342
343 module_platform_driver(broadwell_audio)
344
345 /* Module information */
346 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
347 MODULE_DESCRIPTION("Intel SST Audio for WPT/Broadwell");
348 MODULE_LICENSE("GPL v2");
349 MODULE_ALIAS("platform:broadwell-audio");