xref: /openbmc/linux/sound/soc/intel/boards/bxt_rt298.c (revision 239480ab)
1 /*
2  * Intel Broxton-P I2S Machine Driver
3  *
4  * Copyright (C) 2014-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/pcm.h>
23 #include <sound/soc.h>
24 #include <sound/jack.h>
25 #include <sound/pcm_params.h>
26 #include "../../codecs/hdac_hdmi.h"
27 #include "../../codecs/rt298.h"
28 
29 /* Headset jack detection DAPM pins */
30 static struct snd_soc_jack broxton_headset;
31 static struct snd_soc_jack broxton_hdmi[3];
32 
33 struct bxt_hdmi_pcm {
34 	struct list_head head;
35 	struct snd_soc_dai *codec_dai;
36 	int device;
37 };
38 
39 struct bxt_rt286_private {
40 	struct list_head hdmi_pcm_list;
41 };
42 
43 enum {
44 	BXT_DPCM_AUDIO_PB = 0,
45 	BXT_DPCM_AUDIO_CP,
46 	BXT_DPCM_AUDIO_REF_CP,
47 	BXT_DPCM_AUDIO_DMIC_CP,
48 	BXT_DPCM_AUDIO_HDMI1_PB,
49 	BXT_DPCM_AUDIO_HDMI2_PB,
50 	BXT_DPCM_AUDIO_HDMI3_PB,
51 };
52 
53 static struct snd_soc_jack_pin broxton_headset_pins[] = {
54 	{
55 		.pin = "Mic Jack",
56 		.mask = SND_JACK_MICROPHONE,
57 	},
58 	{
59 		.pin = "Headphone Jack",
60 		.mask = SND_JACK_HEADPHONE,
61 	},
62 };
63 
64 static const struct snd_kcontrol_new broxton_controls[] = {
65 	SOC_DAPM_PIN_SWITCH("Speaker"),
66 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
67 	SOC_DAPM_PIN_SWITCH("Mic Jack"),
68 };
69 
70 static const struct snd_soc_dapm_widget broxton_widgets[] = {
71 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
72 	SND_SOC_DAPM_SPK("Speaker", NULL),
73 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
74 	SND_SOC_DAPM_MIC("DMIC2", NULL),
75 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
76 	SND_SOC_DAPM_SPK("HDMI1", NULL),
77 	SND_SOC_DAPM_SPK("HDMI2", NULL),
78 	SND_SOC_DAPM_SPK("HDMI3", NULL),
79 };
80 
81 static const struct snd_soc_dapm_route broxton_rt298_map[] = {
82 	/* speaker */
83 	{"Speaker", NULL, "SPOR"},
84 	{"Speaker", NULL, "SPOL"},
85 
86 	/* HP jack connectors - unknown if we have jack detect */
87 	{"Headphone Jack", NULL, "HPO Pin"},
88 
89 	/* other jacks */
90 	{"MIC1", NULL, "Mic Jack"},
91 
92 	/* digital mics */
93 	{"DMIC1 Pin", NULL, "DMIC2"},
94 	{"DMic", NULL, "SoC DMIC"},
95 
96 	{"HDMI1", NULL, "hif5-0 Output"},
97 	{"HDMI2", NULL, "hif6-0 Output"},
98 	{"HDMI2", NULL, "hif7-0 Output"},
99 
100 	/* CODEC BE connections */
101 	{ "AIF1 Playback", NULL, "ssp5 Tx"},
102 	{ "ssp5 Tx", NULL, "codec0_out"},
103 	{ "ssp5 Tx", NULL, "codec1_out"},
104 
105 	{ "codec0_in", NULL, "ssp5 Rx" },
106 	{ "ssp5 Rx", NULL, "AIF1 Capture" },
107 
108 	{ "dmic01_hifi", NULL, "DMIC01 Rx" },
109 	{ "DMIC01 Rx", NULL, "Capture" },
110 
111 	{ "hifi3", NULL, "iDisp3 Tx"},
112 	{ "iDisp3 Tx", NULL, "iDisp3_out"},
113 	{ "hifi2", NULL, "iDisp2 Tx"},
114 	{ "iDisp2 Tx", NULL, "iDisp2_out"},
115 	{ "hifi1", NULL, "iDisp1 Tx"},
116 	{ "iDisp1 Tx", NULL, "iDisp1_out"},
117 
118 };
119 
120 static int broxton_rt298_fe_init(struct snd_soc_pcm_runtime *rtd)
121 {
122 	struct snd_soc_dapm_context *dapm;
123 	struct snd_soc_component *component = rtd->cpu_dai->component;
124 
125 	dapm = snd_soc_component_get_dapm(component);
126 	snd_soc_dapm_ignore_suspend(dapm, "Reference Capture");
127 
128 	return 0;
129 }
130 
131 static int broxton_rt298_codec_init(struct snd_soc_pcm_runtime *rtd)
132 {
133 	struct snd_soc_codec *codec = rtd->codec;
134 	int ret = 0;
135 
136 	ret = snd_soc_card_jack_new(rtd->card, "Headset",
137 		SND_JACK_HEADSET | SND_JACK_BTN_0,
138 		&broxton_headset,
139 		broxton_headset_pins, ARRAY_SIZE(broxton_headset_pins));
140 
141 	if (ret)
142 		return ret;
143 
144 	rt298_mic_detect(codec, &broxton_headset);
145 
146 	snd_soc_dapm_ignore_suspend(&rtd->card->dapm, "SoC DMIC");
147 
148 	return 0;
149 }
150 
151 static int broxton_hdmi_init(struct snd_soc_pcm_runtime *rtd)
152 {
153 	struct bxt_rt286_private *ctx = snd_soc_card_get_drvdata(rtd->card);
154 	struct snd_soc_dai *dai = rtd->codec_dai;
155 	struct bxt_hdmi_pcm *pcm;
156 
157 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
158 	if (!pcm)
159 		return -ENOMEM;
160 
161 	pcm->device = BXT_DPCM_AUDIO_HDMI1_PB + dai->id;
162 	pcm->codec_dai = dai;
163 
164 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
165 
166 	return 0;
167 }
168 
169 static int broxton_ssp5_fixup(struct snd_soc_pcm_runtime *rtd,
170 			struct snd_pcm_hw_params *params)
171 {
172 	struct snd_interval *rate = hw_param_interval(params,
173 					SNDRV_PCM_HW_PARAM_RATE);
174 	struct snd_interval *channels = hw_param_interval(params,
175 					SNDRV_PCM_HW_PARAM_CHANNELS);
176 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
177 
178 	/* The ADSP will covert the FE rate to 48k, stereo */
179 	rate->min = rate->max = 48000;
180 	channels->min = channels->max = 2;
181 
182 	/* set SSP5 to 24 bit */
183 	snd_mask_none(fmt);
184 	snd_mask_set(fmt, SNDRV_PCM_FORMAT_S24_LE);
185 
186 	return 0;
187 }
188 
189 static int broxton_rt298_hw_params(struct snd_pcm_substream *substream,
190 	struct snd_pcm_hw_params *params)
191 {
192 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
193 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
194 	int ret;
195 
196 	ret = snd_soc_dai_set_sysclk(codec_dai, RT298_SCLK_S_PLL,
197 					19200000, SND_SOC_CLOCK_IN);
198 	if (ret < 0) {
199 		dev_err(rtd->dev, "can't set codec sysclk configuration\n");
200 		return ret;
201 	}
202 
203 	return ret;
204 }
205 
206 static const struct snd_soc_ops broxton_rt298_ops = {
207 	.hw_params = broxton_rt298_hw_params,
208 };
209 
210 static unsigned int rates[] = {
211 	48000,
212 };
213 
214 static struct snd_pcm_hw_constraint_list constraints_rates = {
215 	.count = ARRAY_SIZE(rates),
216 	.list  = rates,
217 	.mask = 0,
218 };
219 
220 static int broxton_dmic_fixup(struct snd_soc_pcm_runtime *rtd,
221 				struct snd_pcm_hw_params *params)
222 {
223 	struct snd_interval *channels = hw_param_interval(params,
224 						SNDRV_PCM_HW_PARAM_CHANNELS);
225 	if (params_channels(params) == 2)
226 		channels->min = channels->max = 2;
227 	else
228 		channels->min = channels->max = 4;
229 
230 	return 0;
231 }
232 
233 static unsigned int channels_dmic[] = {
234 	2, 4,
235 };
236 
237 static struct snd_pcm_hw_constraint_list constraints_dmic_channels = {
238 	.count = ARRAY_SIZE(channels_dmic),
239 	.list = channels_dmic,
240 	.mask = 0,
241 };
242 
243 static int broxton_dmic_startup(struct snd_pcm_substream *substream)
244 {
245 	struct snd_pcm_runtime *runtime = substream->runtime;
246 
247 	runtime->hw.channels_max = 4;
248 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
249 					&constraints_dmic_channels);
250 
251 	return snd_pcm_hw_constraint_list(substream->runtime, 0,
252 				SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
253 }
254 
255 static const struct snd_soc_ops broxton_dmic_ops = {
256 	.startup = broxton_dmic_startup,
257 };
258 
259 static unsigned int channels[] = {
260 	2,
261 };
262 
263 static struct snd_pcm_hw_constraint_list constraints_channels = {
264 	.count = ARRAY_SIZE(channels),
265 	.list = channels,
266 	.mask = 0,
267 };
268 
269 static int bxt_fe_startup(struct snd_pcm_substream *substream)
270 {
271 	struct snd_pcm_runtime *runtime = substream->runtime;
272 
273 	/*
274 	 * on this platform for PCM device we support:
275 	 *      48Khz
276 	 *      stereo
277 	 *	16-bit audio
278 	 */
279 
280 	runtime->hw.channels_max = 2;
281 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
282 				&constraints_channels);
283 
284 	runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
285 	snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
286 	snd_pcm_hw_constraint_list(runtime, 0,
287 				SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
288 
289 	return 0;
290 }
291 
292 static const struct snd_soc_ops broxton_rt286_fe_ops = {
293 	.startup = bxt_fe_startup,
294 };
295 
296 /* broxton digital audio interface glue - connects codec <--> CPU */
297 static struct snd_soc_dai_link broxton_rt298_dais[] = {
298 	/* Front End DAI links */
299 	[BXT_DPCM_AUDIO_PB] =
300 	{
301 		.name = "Bxt Audio Port",
302 		.stream_name = "Audio",
303 		.cpu_dai_name = "System Pin",
304 		.platform_name = "0000:00:0e.0",
305 		.nonatomic = 1,
306 		.dynamic = 1,
307 		.codec_name = "snd-soc-dummy",
308 		.codec_dai_name = "snd-soc-dummy-dai",
309 		.init = broxton_rt298_fe_init,
310 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
311 		.dpcm_playback = 1,
312 		.ops = &broxton_rt286_fe_ops,
313 	},
314 	[BXT_DPCM_AUDIO_CP] =
315 	{
316 		.name = "Bxt Audio Capture Port",
317 		.stream_name = "Audio Record",
318 		.cpu_dai_name = "System Pin",
319 		.platform_name = "0000:00:0e.0",
320 		.nonatomic = 1,
321 		.dynamic = 1,
322 		.codec_name = "snd-soc-dummy",
323 		.codec_dai_name = "snd-soc-dummy-dai",
324 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
325 		.dpcm_capture = 1,
326 		.ops = &broxton_rt286_fe_ops,
327 	},
328 	[BXT_DPCM_AUDIO_REF_CP] =
329 	{
330 		.name = "Bxt Audio Reference cap",
331 		.stream_name = "refcap",
332 		.cpu_dai_name = "Reference Pin",
333 		.codec_name = "snd-soc-dummy",
334 		.codec_dai_name = "snd-soc-dummy-dai",
335 		.platform_name = "0000:00:0e.0",
336 		.init = NULL,
337 		.dpcm_capture = 1,
338 		.nonatomic = 1,
339 		.dynamic = 1,
340 	},
341 	[BXT_DPCM_AUDIO_DMIC_CP] =
342 	{
343 		.name = "Bxt Audio DMIC cap",
344 		.stream_name = "dmiccap",
345 		.cpu_dai_name = "DMIC Pin",
346 		.codec_name = "snd-soc-dummy",
347 		.codec_dai_name = "snd-soc-dummy-dai",
348 		.platform_name = "0000:00:0e.0",
349 		.init = NULL,
350 		.dpcm_capture = 1,
351 		.nonatomic = 1,
352 		.dynamic = 1,
353 		.ops = &broxton_dmic_ops,
354 	},
355 	[BXT_DPCM_AUDIO_HDMI1_PB] =
356 	{
357 		.name = "Bxt HDMI Port1",
358 		.stream_name = "Hdmi1",
359 		.cpu_dai_name = "HDMI1 Pin",
360 		.codec_name = "snd-soc-dummy",
361 		.codec_dai_name = "snd-soc-dummy-dai",
362 		.platform_name = "0000:00:0e.0",
363 		.dpcm_playback = 1,
364 		.init = NULL,
365 		.nonatomic = 1,
366 		.dynamic = 1,
367 	},
368 	[BXT_DPCM_AUDIO_HDMI2_PB] =
369 	{
370 		.name = "Bxt HDMI Port2",
371 		.stream_name = "Hdmi2",
372 		.cpu_dai_name = "HDMI2 Pin",
373 		.codec_name = "snd-soc-dummy",
374 		.codec_dai_name = "snd-soc-dummy-dai",
375 		.platform_name = "0000:00:0e.0",
376 		.dpcm_playback = 1,
377 		.init = NULL,
378 		.nonatomic = 1,
379 		.dynamic = 1,
380 	},
381 	[BXT_DPCM_AUDIO_HDMI3_PB] =
382 	{
383 		.name = "Bxt HDMI Port3",
384 		.stream_name = "Hdmi3",
385 		.cpu_dai_name = "HDMI3 Pin",
386 		.codec_name = "snd-soc-dummy",
387 		.codec_dai_name = "snd-soc-dummy-dai",
388 		.platform_name = "0000:00:0e.0",
389 		.dpcm_playback = 1,
390 		.init = NULL,
391 		.nonatomic = 1,
392 		.dynamic = 1,
393 	},
394 	/* Back End DAI links */
395 	{
396 		/* SSP5 - Codec */
397 		.name = "SSP5-Codec",
398 		.id = 0,
399 		.cpu_dai_name = "SSP5 Pin",
400 		.platform_name = "0000:00:0e.0",
401 		.no_pcm = 1,
402 		.codec_name = "i2c-INT343A:00",
403 		.codec_dai_name = "rt298-aif1",
404 		.init = broxton_rt298_codec_init,
405 		.dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF |
406 						SND_SOC_DAIFMT_CBS_CFS,
407 		.ignore_pmdown_time = 1,
408 		.be_hw_params_fixup = broxton_ssp5_fixup,
409 		.ops = &broxton_rt298_ops,
410 		.dpcm_playback = 1,
411 		.dpcm_capture = 1,
412 	},
413 	{
414 		.name = "dmic01",
415 		.id = 1,
416 		.cpu_dai_name = "DMIC01 Pin",
417 		.codec_name = "dmic-codec",
418 		.codec_dai_name = "dmic-hifi",
419 		.platform_name = "0000:00:0e.0",
420 		.be_hw_params_fixup = broxton_dmic_fixup,
421 		.ignore_suspend = 1,
422 		.dpcm_capture = 1,
423 		.no_pcm = 1,
424 	},
425 	{
426 		.name = "iDisp1",
427 		.id = 3,
428 		.cpu_dai_name = "iDisp1 Pin",
429 		.codec_name = "ehdaudio0D2",
430 		.codec_dai_name = "intel-hdmi-hifi1",
431 		.platform_name = "0000:00:0e.0",
432 		.init = broxton_hdmi_init,
433 		.dpcm_playback = 1,
434 		.no_pcm = 1,
435 	},
436 	{
437 		.name = "iDisp2",
438 		.id = 4,
439 		.cpu_dai_name = "iDisp2 Pin",
440 		.codec_name = "ehdaudio0D2",
441 		.codec_dai_name = "intel-hdmi-hifi2",
442 		.platform_name = "0000:00:0e.0",
443 		.init = broxton_hdmi_init,
444 		.dpcm_playback = 1,
445 		.no_pcm = 1,
446 	},
447 	{
448 		.name = "iDisp3",
449 		.id = 5,
450 		.cpu_dai_name = "iDisp3 Pin",
451 		.codec_name = "ehdaudio0D2",
452 		.codec_dai_name = "intel-hdmi-hifi3",
453 		.platform_name = "0000:00:0e.0",
454 		.init = broxton_hdmi_init,
455 		.dpcm_playback = 1,
456 		.no_pcm = 1,
457 	},
458 };
459 
460 #define NAME_SIZE	32
461 static int bxt_card_late_probe(struct snd_soc_card *card)
462 {
463 	struct bxt_rt286_private *ctx = snd_soc_card_get_drvdata(card);
464 	struct bxt_hdmi_pcm *pcm;
465 	struct snd_soc_codec *codec = NULL;
466 	int err, i = 0;
467 	char jack_name[NAME_SIZE];
468 
469 	list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
470 		codec = pcm->codec_dai->codec;
471 		snprintf(jack_name, sizeof(jack_name),
472 			"HDMI/DP, pcm=%d Jack", pcm->device);
473 		err = snd_soc_card_jack_new(card, jack_name,
474 					SND_JACK_AVOUT, &broxton_hdmi[i],
475 					NULL, 0);
476 
477 		if (err)
478 			return err;
479 
480 		err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
481 						&broxton_hdmi[i]);
482 		if (err < 0)
483 			return err;
484 
485 		i++;
486 	}
487 
488 	if (!codec)
489 		return -EINVAL;
490 
491 	return hdac_hdmi_jack_port_init(codec, &card->dapm);
492 }
493 
494 
495 /* broxton audio machine driver for SPT + RT298S */
496 static struct snd_soc_card broxton_rt298 = {
497 	.name = "broxton-rt298",
498 	.owner = THIS_MODULE,
499 	.dai_link = broxton_rt298_dais,
500 	.num_links = ARRAY_SIZE(broxton_rt298_dais),
501 	.controls = broxton_controls,
502 	.num_controls = ARRAY_SIZE(broxton_controls),
503 	.dapm_widgets = broxton_widgets,
504 	.num_dapm_widgets = ARRAY_SIZE(broxton_widgets),
505 	.dapm_routes = broxton_rt298_map,
506 	.num_dapm_routes = ARRAY_SIZE(broxton_rt298_map),
507 	.fully_routed = true,
508 	.late_probe = bxt_card_late_probe,
509 
510 };
511 
512 static int broxton_audio_probe(struct platform_device *pdev)
513 {
514 	struct bxt_rt286_private *ctx;
515 
516 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_ATOMIC);
517 	if (!ctx)
518 		return -ENOMEM;
519 
520 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
521 
522 	broxton_rt298.dev = &pdev->dev;
523 	snd_soc_card_set_drvdata(&broxton_rt298, ctx);
524 
525 	return devm_snd_soc_register_card(&pdev->dev, &broxton_rt298);
526 }
527 
528 static struct platform_driver broxton_audio = {
529 	.probe = broxton_audio_probe,
530 	.driver = {
531 		.name = "bxt_alc298s_i2s",
532 		.pm = &snd_soc_pm_ops,
533 	},
534 };
535 module_platform_driver(broxton_audio)
536 
537 /* Module information */
538 MODULE_AUTHOR("Ramesh Babu <Ramesh.Babu@intel.com>");
539 MODULE_AUTHOR("Senthilnathan Veppur <senthilnathanx.veppur@intel.com>");
540 MODULE_DESCRIPTION("Intel SST Audio for Broxton");
541 MODULE_LICENSE("GPL v2");
542 MODULE_ALIAS("platform:bxt_alc298s_i2s");
543