]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/soc/intel/boards/bxt_da7219_max98357a.c
Merge remote-tracking branches 'asoc/topic/adsp', 'asoc/topic/ak4613', 'asoc/topic...
[mirror_ubuntu-bionic-kernel.git] / sound / soc / intel / boards / bxt_da7219_max98357a.c
1 /*
2 * Intel Broxton-P I2S Machine Driver
3 *
4 * Copyright (C) 2016, Intel Corporation. All rights reserved.
5 *
6 * Modified from:
7 * Intel Skylake I2S Machine driver
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <sound/core.h>
22 #include <sound/jack.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include "../../codecs/hdac_hdmi.h"
27 #include "../../codecs/da7219.h"
28 #include "../../codecs/da7219-aad.h"
29
30 #define BXT_DIALOG_CODEC_DAI "da7219-hifi"
31 #define BXT_MAXIM_CODEC_DAI "HiFi"
32 #define DUAL_CHANNEL 2
33 #define QUAD_CHANNEL 4
34
35 static struct snd_soc_jack broxton_headset;
36 static struct snd_soc_jack broxton_hdmi[3];
37
38 struct bxt_hdmi_pcm {
39 struct list_head head;
40 struct snd_soc_dai *codec_dai;
41 int device;
42 };
43
44 struct bxt_card_private {
45 struct list_head hdmi_pcm_list;
46 };
47
48 enum {
49 BXT_DPCM_AUDIO_PB = 0,
50 BXT_DPCM_AUDIO_CP,
51 BXT_DPCM_AUDIO_REF_CP,
52 BXT_DPCM_AUDIO_DMIC_CP,
53 BXT_DPCM_AUDIO_HDMI1_PB,
54 BXT_DPCM_AUDIO_HDMI2_PB,
55 BXT_DPCM_AUDIO_HDMI3_PB,
56 };
57
58 static inline struct snd_soc_dai *bxt_get_codec_dai(struct snd_soc_card *card)
59 {
60 struct snd_soc_pcm_runtime *rtd;
61
62 list_for_each_entry(rtd, &card->rtd_list, list) {
63
64 if (!strncmp(rtd->codec_dai->name, BXT_DIALOG_CODEC_DAI,
65 strlen(BXT_DIALOG_CODEC_DAI)))
66 return rtd->codec_dai;
67 }
68
69 return NULL;
70 }
71
72 static int platform_clock_control(struct snd_soc_dapm_widget *w,
73 struct snd_kcontrol *k, int event)
74 {
75 int ret = 0;
76 struct snd_soc_dapm_context *dapm = w->dapm;
77 struct snd_soc_card *card = dapm->card;
78 struct snd_soc_dai *codec_dai;
79
80 codec_dai = bxt_get_codec_dai(card);
81 if (!codec_dai) {
82 dev_err(card->dev, "Codec dai not found; Unable to set/unset codec pll\n");
83 return -EIO;
84 }
85
86 if (SND_SOC_DAPM_EVENT_OFF(event)) {
87 ret = snd_soc_dai_set_pll(codec_dai, 0,
88 DA7219_SYSCLK_MCLK, 0, 0);
89 if (ret)
90 dev_err(card->dev, "failed to stop PLL: %d\n", ret);
91 } else if(SND_SOC_DAPM_EVENT_ON(event)) {
92 ret = snd_soc_dai_set_sysclk(codec_dai,
93 DA7219_CLKSRC_MCLK, 19200000, SND_SOC_CLOCK_IN);
94 if (ret)
95 dev_err(card->dev, "can't set codec sysclk configuration\n");
96
97 ret = snd_soc_dai_set_pll(codec_dai, 0,
98 DA7219_SYSCLK_PLL_SRM, 0, DA7219_PLL_FREQ_OUT_98304);
99 if (ret)
100 dev_err(card->dev, "failed to start PLL: %d\n", ret);
101 }
102
103 return ret;
104 }
105
106 static const struct snd_kcontrol_new broxton_controls[] = {
107 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
108 SOC_DAPM_PIN_SWITCH("Headset Mic"),
109 SOC_DAPM_PIN_SWITCH("Spk"),
110 };
111
112 static const struct snd_soc_dapm_widget broxton_widgets[] = {
113 SND_SOC_DAPM_HP("Headphone Jack", NULL),
114 SND_SOC_DAPM_MIC("Headset Mic", NULL),
115 SND_SOC_DAPM_SPK("Spk", NULL),
116 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
117 SND_SOC_DAPM_SPK("HDMI1", NULL),
118 SND_SOC_DAPM_SPK("HDMI2", NULL),
119 SND_SOC_DAPM_SPK("HDMI3", NULL),
120 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
121 platform_clock_control, SND_SOC_DAPM_POST_PMD|SND_SOC_DAPM_PRE_PMU),
122 };
123
124 static const struct snd_soc_dapm_route broxton_map[] = {
125 /* HP jack connectors - unknown if we have jack detection */
126 {"Headphone Jack", NULL, "HPL"},
127 {"Headphone Jack", NULL, "HPR"},
128
129 /* speaker */
130 {"Spk", NULL, "Speaker"},
131
132 /* other jacks */
133 {"MIC", NULL, "Headset Mic"},
134
135 /* digital mics */
136 {"DMic", NULL, "SoC DMIC"},
137
138 /* CODEC BE connections */
139 {"HiFi Playback", NULL, "ssp5 Tx"},
140 {"ssp5 Tx", NULL, "codec0_out"},
141
142 {"Playback", NULL, "ssp1 Tx"},
143 {"ssp1 Tx", NULL, "codec1_out"},
144
145 {"codec0_in", NULL, "ssp1 Rx"},
146 {"ssp1 Rx", NULL, "Capture"},
147
148 {"HDMI1", NULL, "hif5-0 Output"},
149 {"HDMI2", NULL, "hif6-0 Output"},
150 {"HDMI2", NULL, "hif7-0 Output"},
151
152 {"hifi3", NULL, "iDisp3 Tx"},
153 {"iDisp3 Tx", NULL, "iDisp3_out"},
154 {"hifi2", NULL, "iDisp2 Tx"},
155 {"iDisp2 Tx", NULL, "iDisp2_out"},
156 {"hifi1", NULL, "iDisp1 Tx"},
157 {"iDisp1 Tx", NULL, "iDisp1_out"},
158
159 /* DMIC */
160 {"dmic01_hifi", NULL, "DMIC01 Rx"},
161 {"DMIC01 Rx", NULL, "DMIC AIF"},
162
163 { "Headphone Jack", NULL, "Platform Clock" },
164 { "Headset Mic", NULL, "Platform Clock" },
165 };
166
167 static int broxton_ssp_fixup(struct snd_soc_pcm_runtime *rtd,
168 struct snd_pcm_hw_params *params)
169 {
170 struct snd_interval *rate = hw_param_interval(params,
171 SNDRV_PCM_HW_PARAM_RATE);
172 struct snd_interval *channels = hw_param_interval(params,
173 SNDRV_PCM_HW_PARAM_CHANNELS);
174 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
175
176 /* The ADSP will convert the FE rate to 48k, stereo */
177 rate->min = rate->max = 48000;
178 channels->min = channels->max = DUAL_CHANNEL;
179
180 /* set SSP to 24 bit */
181 snd_mask_none(fmt);
182 snd_mask_set(fmt, SNDRV_PCM_FORMAT_S24_LE);
183
184 return 0;
185 }
186
187 static int broxton_da7219_codec_init(struct snd_soc_pcm_runtime *rtd)
188 {
189 int ret;
190 struct snd_soc_codec *codec = rtd->codec;
191
192 /*
193 * Headset buttons map to the google Reference headset.
194 * These can be configured by userspace.
195 */
196 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
197 SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
198 SND_JACK_BTN_2 | SND_JACK_BTN_3 | SND_JACK_LINEOUT,
199 &broxton_headset, NULL, 0);
200 if (ret) {
201 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
202 return ret;
203 }
204
205 da7219_aad_jack_det(codec, &broxton_headset);
206
207 snd_soc_dapm_ignore_suspend(&rtd->card->dapm, "SoC DMIC");
208
209 return ret;
210 }
211
212 static int broxton_hdmi_init(struct snd_soc_pcm_runtime *rtd)
213 {
214 struct bxt_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
215 struct snd_soc_dai *dai = rtd->codec_dai;
216 struct bxt_hdmi_pcm *pcm;
217
218 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
219 if (!pcm)
220 return -ENOMEM;
221
222 pcm->device = BXT_DPCM_AUDIO_HDMI1_PB + dai->id;
223 pcm->codec_dai = dai;
224
225 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
226
227 return 0;
228 }
229
230 static int broxton_da7219_fe_init(struct snd_soc_pcm_runtime *rtd)
231 {
232 struct snd_soc_dapm_context *dapm;
233 struct snd_soc_component *component = rtd->cpu_dai->component;
234
235 dapm = snd_soc_component_get_dapm(component);
236 snd_soc_dapm_ignore_suspend(dapm, "Reference Capture");
237
238 return 0;
239 }
240
241 static unsigned int rates[] = {
242 48000,
243 };
244
245 static struct snd_pcm_hw_constraint_list constraints_rates = {
246 .count = ARRAY_SIZE(rates),
247 .list = rates,
248 .mask = 0,
249 };
250
251 static unsigned int channels[] = {
252 DUAL_CHANNEL,
253 };
254
255 static struct snd_pcm_hw_constraint_list constraints_channels = {
256 .count = ARRAY_SIZE(channels),
257 .list = channels,
258 .mask = 0,
259 };
260
261 static unsigned int channels_quad[] = {
262 QUAD_CHANNEL,
263 };
264
265 static struct snd_pcm_hw_constraint_list constraints_channels_quad = {
266 .count = ARRAY_SIZE(channels_quad),
267 .list = channels_quad,
268 .mask = 0,
269 };
270
271 static int bxt_fe_startup(struct snd_pcm_substream *substream)
272 {
273 struct snd_pcm_runtime *runtime = substream->runtime;
274
275 /*
276 * On this platform for PCM device we support,
277 * 48Khz
278 * stereo
279 * 16 bit audio
280 */
281
282 runtime->hw.channels_max = DUAL_CHANNEL;
283 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
284 &constraints_channels);
285
286 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
287 snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
288
289 snd_pcm_hw_constraint_list(runtime, 0,
290 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
291
292 return 0;
293 }
294
295 static const struct snd_soc_ops broxton_da7219_fe_ops = {
296 .startup = bxt_fe_startup,
297 };
298
299 static int broxton_dmic_fixup(struct snd_soc_pcm_runtime *rtd,
300 struct snd_pcm_hw_params *params)
301 {
302 struct snd_interval *channels = hw_param_interval(params,
303 SNDRV_PCM_HW_PARAM_CHANNELS);
304 if (params_channels(params) == 2)
305 channels->min = channels->max = 2;
306 else
307 channels->min = channels->max = 4;
308
309 return 0;
310 }
311
312 static int broxton_dmic_startup(struct snd_pcm_substream *substream)
313 {
314 struct snd_pcm_runtime *runtime = substream->runtime;
315
316 runtime->hw.channels_min = runtime->hw.channels_max = QUAD_CHANNEL;
317 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
318 &constraints_channels_quad);
319
320 return snd_pcm_hw_constraint_list(substream->runtime, 0,
321 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
322 }
323
324 static const struct snd_soc_ops broxton_dmic_ops = {
325 .startup = broxton_dmic_startup,
326 };
327
328 static const unsigned int rates_16000[] = {
329 16000,
330 };
331
332 static const struct snd_pcm_hw_constraint_list constraints_16000 = {
333 .count = ARRAY_SIZE(rates_16000),
334 .list = rates_16000,
335 };
336
337 static int broxton_refcap_startup(struct snd_pcm_substream *substream)
338 {
339 return snd_pcm_hw_constraint_list(substream->runtime, 0,
340 SNDRV_PCM_HW_PARAM_RATE,
341 &constraints_16000);
342 };
343
344 static const struct snd_soc_ops broxton_refcap_ops = {
345 .startup = broxton_refcap_startup,
346 };
347
348 /* broxton digital audio interface glue - connects codec <--> CPU */
349 static struct snd_soc_dai_link broxton_dais[] = {
350 /* Front End DAI links */
351 [BXT_DPCM_AUDIO_PB] =
352 {
353 .name = "Bxt Audio Port",
354 .stream_name = "Audio",
355 .cpu_dai_name = "System Pin",
356 .platform_name = "0000:00:0e.0",
357 .dynamic = 1,
358 .codec_name = "snd-soc-dummy",
359 .codec_dai_name = "snd-soc-dummy-dai",
360 .nonatomic = 1,
361 .init = broxton_da7219_fe_init,
362 .trigger = {
363 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
364 .dpcm_playback = 1,
365 .ops = &broxton_da7219_fe_ops,
366 },
367 [BXT_DPCM_AUDIO_CP] =
368 {
369 .name = "Bxt Audio Capture Port",
370 .stream_name = "Audio Record",
371 .cpu_dai_name = "System Pin",
372 .platform_name = "0000:00:0e.0",
373 .dynamic = 1,
374 .codec_name = "snd-soc-dummy",
375 .codec_dai_name = "snd-soc-dummy-dai",
376 .nonatomic = 1,
377 .trigger = {
378 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
379 .dpcm_capture = 1,
380 .ops = &broxton_da7219_fe_ops,
381 },
382 [BXT_DPCM_AUDIO_REF_CP] =
383 {
384 .name = "Bxt Audio Reference cap",
385 .stream_name = "Refcap",
386 .cpu_dai_name = "Reference Pin",
387 .codec_name = "snd-soc-dummy",
388 .codec_dai_name = "snd-soc-dummy-dai",
389 .platform_name = "0000:00:0e.0",
390 .init = NULL,
391 .dpcm_capture = 1,
392 .nonatomic = 1,
393 .dynamic = 1,
394 .ops = &broxton_refcap_ops,
395 },
396 [BXT_DPCM_AUDIO_DMIC_CP] =
397 {
398 .name = "Bxt Audio DMIC cap",
399 .stream_name = "dmiccap",
400 .cpu_dai_name = "DMIC Pin",
401 .codec_name = "snd-soc-dummy",
402 .codec_dai_name = "snd-soc-dummy-dai",
403 .platform_name = "0000:00:0e.0",
404 .init = NULL,
405 .dpcm_capture = 1,
406 .nonatomic = 1,
407 .dynamic = 1,
408 .ops = &broxton_dmic_ops,
409 },
410 [BXT_DPCM_AUDIO_HDMI1_PB] =
411 {
412 .name = "Bxt HDMI Port1",
413 .stream_name = "Hdmi1",
414 .cpu_dai_name = "HDMI1 Pin",
415 .codec_name = "snd-soc-dummy",
416 .codec_dai_name = "snd-soc-dummy-dai",
417 .platform_name = "0000:00:0e.0",
418 .dpcm_playback = 1,
419 .init = NULL,
420 .nonatomic = 1,
421 .dynamic = 1,
422 },
423 [BXT_DPCM_AUDIO_HDMI2_PB] =
424 {
425 .name = "Bxt HDMI Port2",
426 .stream_name = "Hdmi2",
427 .cpu_dai_name = "HDMI2 Pin",
428 .codec_name = "snd-soc-dummy",
429 .codec_dai_name = "snd-soc-dummy-dai",
430 .platform_name = "0000:00:0e.0",
431 .dpcm_playback = 1,
432 .init = NULL,
433 .nonatomic = 1,
434 .dynamic = 1,
435 },
436 [BXT_DPCM_AUDIO_HDMI3_PB] =
437 {
438 .name = "Bxt HDMI Port3",
439 .stream_name = "Hdmi3",
440 .cpu_dai_name = "HDMI3 Pin",
441 .codec_name = "snd-soc-dummy",
442 .codec_dai_name = "snd-soc-dummy-dai",
443 .platform_name = "0000:00:0e.0",
444 .dpcm_playback = 1,
445 .init = NULL,
446 .nonatomic = 1,
447 .dynamic = 1,
448 },
449 /* Back End DAI links */
450 {
451 /* SSP5 - Codec */
452 .name = "SSP5-Codec",
453 .id = 0,
454 .cpu_dai_name = "SSP5 Pin",
455 .platform_name = "0000:00:0e.0",
456 .no_pcm = 1,
457 .codec_name = "MX98357A:00",
458 .codec_dai_name = BXT_MAXIM_CODEC_DAI,
459 .dai_fmt = SND_SOC_DAIFMT_I2S |
460 SND_SOC_DAIFMT_NB_NF |
461 SND_SOC_DAIFMT_CBS_CFS,
462 .ignore_pmdown_time = 1,
463 .be_hw_params_fixup = broxton_ssp_fixup,
464 .dpcm_playback = 1,
465 },
466 {
467 /* SSP1 - Codec */
468 .name = "SSP1-Codec",
469 .id = 1,
470 .cpu_dai_name = "SSP1 Pin",
471 .platform_name = "0000:00:0e.0",
472 .no_pcm = 1,
473 .codec_name = "i2c-DLGS7219:00",
474 .codec_dai_name = BXT_DIALOG_CODEC_DAI,
475 .init = broxton_da7219_codec_init,
476 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
477 SND_SOC_DAIFMT_CBS_CFS,
478 .ignore_pmdown_time = 1,
479 .be_hw_params_fixup = broxton_ssp_fixup,
480 .dpcm_playback = 1,
481 .dpcm_capture = 1,
482 },
483 {
484 .name = "dmic01",
485 .id = 2,
486 .cpu_dai_name = "DMIC01 Pin",
487 .codec_name = "dmic-codec",
488 .codec_dai_name = "dmic-hifi",
489 .platform_name = "0000:00:0e.0",
490 .ignore_suspend = 1,
491 .be_hw_params_fixup = broxton_dmic_fixup,
492 .dpcm_capture = 1,
493 .no_pcm = 1,
494 },
495 {
496 .name = "iDisp1",
497 .id = 3,
498 .cpu_dai_name = "iDisp1 Pin",
499 .codec_name = "ehdaudio0D2",
500 .codec_dai_name = "intel-hdmi-hifi1",
501 .platform_name = "0000:00:0e.0",
502 .init = broxton_hdmi_init,
503 .dpcm_playback = 1,
504 .no_pcm = 1,
505 },
506 {
507 .name = "iDisp2",
508 .id = 4,
509 .cpu_dai_name = "iDisp2 Pin",
510 .codec_name = "ehdaudio0D2",
511 .codec_dai_name = "intel-hdmi-hifi2",
512 .platform_name = "0000:00:0e.0",
513 .init = broxton_hdmi_init,
514 .dpcm_playback = 1,
515 .no_pcm = 1,
516 },
517 {
518 .name = "iDisp3",
519 .id = 5,
520 .cpu_dai_name = "iDisp3 Pin",
521 .codec_name = "ehdaudio0D2",
522 .codec_dai_name = "intel-hdmi-hifi3",
523 .platform_name = "0000:00:0e.0",
524 .init = broxton_hdmi_init,
525 .dpcm_playback = 1,
526 .no_pcm = 1,
527 },
528 };
529
530 #define NAME_SIZE 32
531 static int bxt_card_late_probe(struct snd_soc_card *card)
532 {
533 struct bxt_card_private *ctx = snd_soc_card_get_drvdata(card);
534 struct bxt_hdmi_pcm *pcm;
535 struct snd_soc_codec *codec = NULL;
536 int err, i = 0;
537 char jack_name[NAME_SIZE];
538
539 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
540 codec = pcm->codec_dai->codec;
541 snprintf(jack_name, sizeof(jack_name),
542 "HDMI/DP, pcm=%d Jack", pcm->device);
543 err = snd_soc_card_jack_new(card, jack_name,
544 SND_JACK_AVOUT, &broxton_hdmi[i],
545 NULL, 0);
546
547 if (err)
548 return err;
549
550 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
551 &broxton_hdmi[i]);
552 if (err < 0)
553 return err;
554
555 i++;
556 }
557
558 if (!codec)
559 return -EINVAL;
560
561 return hdac_hdmi_jack_port_init(codec, &card->dapm);
562 }
563
564 /* broxton audio machine driver for SPT + da7219 */
565 static struct snd_soc_card broxton_audio_card = {
566 .name = "bxtda7219max",
567 .owner = THIS_MODULE,
568 .dai_link = broxton_dais,
569 .num_links = ARRAY_SIZE(broxton_dais),
570 .controls = broxton_controls,
571 .num_controls = ARRAY_SIZE(broxton_controls),
572 .dapm_widgets = broxton_widgets,
573 .num_dapm_widgets = ARRAY_SIZE(broxton_widgets),
574 .dapm_routes = broxton_map,
575 .num_dapm_routes = ARRAY_SIZE(broxton_map),
576 .fully_routed = true,
577 .late_probe = bxt_card_late_probe,
578 };
579
580 static int broxton_audio_probe(struct platform_device *pdev)
581 {
582 struct bxt_card_private *ctx;
583
584 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_ATOMIC);
585 if (!ctx)
586 return -ENOMEM;
587
588 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
589
590 broxton_audio_card.dev = &pdev->dev;
591 snd_soc_card_set_drvdata(&broxton_audio_card, ctx);
592
593 return devm_snd_soc_register_card(&pdev->dev, &broxton_audio_card);
594 }
595
596 static struct platform_driver broxton_audio = {
597 .probe = broxton_audio_probe,
598 .driver = {
599 .name = "bxt_da7219_max98357a_i2s",
600 .pm = &snd_soc_pm_ops,
601 },
602 };
603 module_platform_driver(broxton_audio)
604
605 /* Module information */
606 MODULE_DESCRIPTION("Audio Machine driver-DA7219 & MAX98357A in I2S mode");
607 MODULE_AUTHOR("Sathyanarayana Nujella <sathyanarayana.nujella@intel.com>");
608 MODULE_AUTHOR("Rohit Ainapure <rohit.m.ainapure@intel.com>");
609 MODULE_AUTHOR("Harsha Priya <harshapriya.n@intel.com>");
610 MODULE_AUTHOR("Conrad Cooke <conrad.cooke@intel.com>");
611 MODULE_LICENSE("GPL v2");
612 MODULE_ALIAS("platform:bxt_da7219_max98357a_i2s");