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 
34 static struct snd_soc_jack broxton_headset;
35 
36 enum {
37 	BXT_DPCM_AUDIO_PB = 0,
38 	BXT_DPCM_AUDIO_CP,
39 	BXT_DPCM_AUDIO_REF_CP,
40 	BXT_DPCM_AUDIO_DMIC_CP,
41 	BXT_DPCM_AUDIO_HDMI1_PB,
42 	BXT_DPCM_AUDIO_HDMI2_PB,
43 	BXT_DPCM_AUDIO_HDMI3_PB,
44 };
45 
46 static const struct snd_kcontrol_new broxton_controls[] = {
47 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
48 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
49 	SOC_DAPM_PIN_SWITCH("Spk"),
50 };
51 
52 static const struct snd_soc_dapm_widget broxton_widgets[] = {
53 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
54 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
55 	SND_SOC_DAPM_SPK("Spk", NULL),
56 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
57 	SND_SOC_DAPM_SPK("HDMI1", NULL),
58 	SND_SOC_DAPM_SPK("HDMI2", NULL),
59 	SND_SOC_DAPM_SPK("HDMI3", NULL),
60 };
61 
62 static const struct snd_soc_dapm_route broxton_map[] = {
63 	/* HP jack connectors - unknown if we have jack detection */
64 	{"Headphone Jack", NULL, "HPL"},
65 	{"Headphone Jack", NULL, "HPR"},
66 
67 	/* speaker */
68 	{"Spk", NULL, "Speaker"},
69 
70 	/* other jacks */
71 	{"MIC", NULL, "Headset Mic"},
72 
73 	/* digital mics */
74 	{"DMic", NULL, "SoC DMIC"},
75 
76 	/* CODEC BE connections */
77 	{"HiFi Playback", NULL, "ssp5 Tx"},
78 	{"ssp5 Tx", NULL, "codec0_out"},
79 
80 	{"Playback", NULL, "ssp1 Tx"},
81 	{"ssp1 Tx", NULL, "codec1_out"},
82 
83 	{"codec0_in", NULL, "ssp1 Rx"},
84 	{"ssp1 Rx", NULL, "Capture"},
85 
86 	{"HDMI1", NULL, "hif5 Output"},
87 	{"HDMI2", NULL, "hif6 Output"},
88 	{"HDMI3", NULL, "hif7 Output"},
89 
90 	{"hifi3", NULL, "iDisp3 Tx"},
91 	{"iDisp3 Tx", NULL, "iDisp3_out"},
92 	{"hifi2", NULL, "iDisp2 Tx"},
93 	{"iDisp2 Tx", NULL, "iDisp2_out"},
94 	{"hifi1", NULL, "iDisp1 Tx"},
95 	{"iDisp1 Tx", NULL, "iDisp1_out"},
96 
97 	/* DMIC */
98 	{"dmic01_hifi", NULL, "DMIC01 Rx"},
99 	{"DMIC01 Rx", NULL, "DMIC AIF"},
100 };
101 
102 static int broxton_ssp_fixup(struct snd_soc_pcm_runtime *rtd,
103 			struct snd_pcm_hw_params *params)
104 {
105 	struct snd_interval *rate = hw_param_interval(params,
106 			SNDRV_PCM_HW_PARAM_RATE);
107 	struct snd_interval *channels = hw_param_interval(params,
108 			SNDRV_PCM_HW_PARAM_CHANNELS);
109 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
110 
111 	/* The ADSP will convert the FE rate to 48k, stereo */
112 	rate->min = rate->max = 48000;
113 	channels->min = channels->max = DUAL_CHANNEL;
114 
115 	/* set SSP to 24 bit */
116 	snd_mask_none(fmt);
117 	snd_mask_set(fmt, SNDRV_PCM_FORMAT_S24_LE);
118 
119 	return 0;
120 }
121 
122 static int broxton_da7219_codec_init(struct snd_soc_pcm_runtime *rtd)
123 {
124 	int ret;
125 	struct snd_soc_codec *codec = rtd->codec;
126 
127 	/*
128 	 * Headset buttons map to the google Reference headset.
129 	 * These can be configured by userspace.
130 	 */
131 	ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
132 			SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
133 			SND_JACK_BTN_2 | SND_JACK_BTN_3 | SND_JACK_LINEOUT,
134 			&broxton_headset, NULL, 0);
135 	if (ret) {
136 		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
137 		return ret;
138 	}
139 
140 	da7219_aad_jack_det(codec, &broxton_headset);
141 
142 	snd_soc_dapm_ignore_suspend(&rtd->card->dapm, "SoC DMIC");
143 
144 	return ret;
145 }
146 
147 static int broxton_hdmi_init(struct snd_soc_pcm_runtime *rtd)
148 {
149 	struct snd_soc_dai *dai = rtd->codec_dai;
150 
151 	return hdac_hdmi_jack_init(dai, BXT_DPCM_AUDIO_HDMI1_PB + dai->id);
152 }
153 
154 static int broxton_da7219_fe_init(struct snd_soc_pcm_runtime *rtd)
155 {
156 	struct snd_soc_dapm_context *dapm;
157 	struct snd_soc_component *component = rtd->cpu_dai->component;
158 
159 	dapm = snd_soc_component_get_dapm(component);
160 	snd_soc_dapm_ignore_suspend(dapm, "Reference Capture");
161 
162 	return 0;
163 }
164 
165 static unsigned int rates[] = {
166 	48000,
167 };
168 
169 static struct snd_pcm_hw_constraint_list constraints_rates = {
170 	.count = ARRAY_SIZE(rates),
171 	.list  = rates,
172 	.mask = 0,
173 };
174 
175 static unsigned int channels[] = {
176 	DUAL_CHANNEL,
177 };
178 
179 static struct snd_pcm_hw_constraint_list constraints_channels = {
180 	.count = ARRAY_SIZE(channels),
181 	.list = channels,
182 	.mask = 0,
183 };
184 
185 static int bxt_fe_startup(struct snd_pcm_substream *substream)
186 {
187 	struct snd_pcm_runtime *runtime = substream->runtime;
188 
189 	/*
190 	 * On this platform for PCM device we support,
191 	 * 48Khz
192 	 * stereo
193 	 * 16 bit audio
194 	 */
195 
196 	runtime->hw.channels_max = DUAL_CHANNEL;
197 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
198 					   &constraints_channels);
199 
200 	runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
201 	snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
202 
203 	snd_pcm_hw_constraint_list(runtime, 0,
204 				SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
205 
206 	return 0;
207 }
208 
209 static const struct snd_soc_ops broxton_da7219_fe_ops = {
210 	.startup = bxt_fe_startup,
211 };
212 
213 static int broxton_da7219_hw_params(struct snd_pcm_substream *substream,
214 				struct snd_pcm_hw_params *params)
215 {
216 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
217 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
218 	int ret;
219 
220 	ret = snd_soc_dai_set_sysclk(codec_dai,
221 			DA7219_CLKSRC_MCLK, 19200000, SND_SOC_CLOCK_IN);
222 	if (ret < 0)
223 		dev_err(codec_dai->dev, "can't set codec sysclk configuration\n");
224 
225 	ret = snd_soc_dai_set_pll(codec_dai, 0,
226 			DA7219_SYSCLK_PLL_SRM, 0, DA7219_PLL_FREQ_OUT_98304);
227 	if (ret < 0) {
228 		dev_err(codec_dai->dev, "failed to start PLL: %d\n", ret);
229 		return -EIO;
230 	}
231 
232 	return ret;
233 }
234 
235 static int broxton_da7219_hw_free(struct snd_pcm_substream *substream)
236 {
237 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
238 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
239 	int ret;
240 
241 	ret = snd_soc_dai_set_pll(codec_dai, 0,
242 			DA7219_SYSCLK_MCLK, 0, 0);
243 	if (ret < 0) {
244 		dev_err(codec_dai->dev, "failed to stop PLL: %d\n", ret);
245 		return -EIO;
246 	}
247 
248 	return ret;
249 }
250 
251 static struct snd_soc_ops broxton_da7219_ops = {
252 	.hw_params = broxton_da7219_hw_params,
253 	.hw_free = broxton_da7219_hw_free,
254 };
255 
256 static int broxton_dmic_fixup(struct snd_soc_pcm_runtime *rtd,
257 			struct snd_pcm_hw_params *params)
258 {
259 	struct snd_interval *channels = hw_param_interval(params,
260 						SNDRV_PCM_HW_PARAM_CHANNELS);
261 	channels->min = channels->max = DUAL_CHANNEL;
262 
263 	return 0;
264 }
265 
266 static int broxton_dmic_startup(struct snd_pcm_substream *substream)
267 {
268 	struct snd_pcm_runtime *runtime = substream->runtime;
269 
270 	runtime->hw.channels_max = DUAL_CHANNEL;
271 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
272 			&constraints_channels);
273 
274 	return snd_pcm_hw_constraint_list(substream->runtime, 0,
275 			SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
276 }
277 
278 static const struct snd_soc_ops broxton_dmic_ops = {
279 	.startup = broxton_dmic_startup,
280 };
281 
282 static const unsigned int rates_16000[] = {
283 	16000,
284 };
285 
286 static const struct snd_pcm_hw_constraint_list constraints_16000 = {
287 	.count = ARRAY_SIZE(rates_16000),
288 	.list  = rates_16000,
289 };
290 
291 static int broxton_refcap_startup(struct snd_pcm_substream *substream)
292 {
293 	return snd_pcm_hw_constraint_list(substream->runtime, 0,
294 			SNDRV_PCM_HW_PARAM_RATE,
295 			&constraints_16000);
296 };
297 
298 static struct snd_soc_ops broxton_refcap_ops = {
299 	.startup = broxton_refcap_startup,
300 };
301 
302 /* broxton digital audio interface glue - connects codec <--> CPU */
303 static struct snd_soc_dai_link broxton_dais[] = {
304 	/* Front End DAI links */
305 	[BXT_DPCM_AUDIO_PB] =
306 	{
307 		.name = "Bxt Audio Port",
308 		.stream_name = "Audio",
309 		.cpu_dai_name = "System Pin",
310 		.platform_name = "0000:00:0e.0",
311 		.dynamic = 1,
312 		.codec_name = "snd-soc-dummy",
313 		.codec_dai_name = "snd-soc-dummy-dai",
314 		.nonatomic = 1,
315 		.init = broxton_da7219_fe_init,
316 		.trigger = {
317 			SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
318 		.dpcm_playback = 1,
319 		.ops = &broxton_da7219_fe_ops,
320 	},
321 	[BXT_DPCM_AUDIO_CP] =
322 	{
323 		.name = "Bxt Audio Capture Port",
324 		.stream_name = "Audio Record",
325 		.cpu_dai_name = "System Pin",
326 		.platform_name = "0000:00:0e.0",
327 		.dynamic = 1,
328 		.codec_name = "snd-soc-dummy",
329 		.codec_dai_name = "snd-soc-dummy-dai",
330 		.nonatomic = 1,
331 		.trigger = {
332 			SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
333 		.dpcm_capture = 1,
334 		.ops = &broxton_da7219_fe_ops,
335 	},
336 	[BXT_DPCM_AUDIO_REF_CP] =
337 	{
338 		.name = "Bxt Audio Reference cap",
339 		.stream_name = "Refcap",
340 		.cpu_dai_name = "Reference Pin",
341 		.codec_name = "snd-soc-dummy",
342 		.codec_dai_name = "snd-soc-dummy-dai",
343 		.platform_name = "0000:00:0e.0",
344 		.init = NULL,
345 		.dpcm_capture = 1,
346 		.ignore_suspend = 1,
347 		.nonatomic = 1,
348 		.dynamic = 1,
349 		.ops = &broxton_refcap_ops,
350 	},
351 	[BXT_DPCM_AUDIO_DMIC_CP]
352 	{
353 		.name = "Bxt Audio DMIC cap",
354 		.stream_name = "dmiccap",
355 		.cpu_dai_name = "DMIC Pin",
356 		.codec_name = "snd-soc-dummy",
357 		.codec_dai_name = "snd-soc-dummy-dai",
358 		.platform_name = "0000:00:0e.0",
359 		.init = NULL,
360 		.dpcm_capture = 1,
361 		.nonatomic = 1,
362 		.dynamic = 1,
363 		.ops = &broxton_dmic_ops,
364 	},
365 	[BXT_DPCM_AUDIO_HDMI1_PB] =
366 	{
367 		.name = "Bxt HDMI Port1",
368 		.stream_name = "Hdmi1",
369 		.cpu_dai_name = "HDMI1 Pin",
370 		.codec_name = "snd-soc-dummy",
371 		.codec_dai_name = "snd-soc-dummy-dai",
372 		.platform_name = "0000:00:0e.0",
373 		.dpcm_playback = 1,
374 		.init = NULL,
375 		.nonatomic = 1,
376 		.dynamic = 1,
377 	},
378 	[BXT_DPCM_AUDIO_HDMI2_PB] =
379 	{
380 		.name = "Bxt HDMI Port2",
381 		.stream_name = "Hdmi2",
382 		.cpu_dai_name = "HDMI2 Pin",
383 		.codec_name = "snd-soc-dummy",
384 		.codec_dai_name = "snd-soc-dummy-dai",
385 		.platform_name = "0000:00:0e.0",
386 		.dpcm_playback = 1,
387 		.init = NULL,
388 		.nonatomic = 1,
389 		.dynamic = 1,
390 	},
391 	[BXT_DPCM_AUDIO_HDMI3_PB] =
392 	{
393 		.name = "Bxt HDMI Port3",
394 		.stream_name = "Hdmi3",
395 		.cpu_dai_name = "HDMI3 Pin",
396 		.codec_name = "snd-soc-dummy",
397 		.codec_dai_name = "snd-soc-dummy-dai",
398 		.platform_name = "0000:00:0e.0",
399 		.dpcm_playback = 1,
400 		.init = NULL,
401 		.nonatomic = 1,
402 		.dynamic = 1,
403 	},
404 	/* Back End DAI links */
405 	{
406 		/* SSP5 - Codec */
407 		.name = "SSP5-Codec",
408 		.id = 0,
409 		.cpu_dai_name = "SSP5 Pin",
410 		.platform_name = "0000:00:0e.0",
411 		.no_pcm = 1,
412 		.codec_name = "MX98357A:00",
413 		.codec_dai_name = BXT_MAXIM_CODEC_DAI,
414 		.dai_fmt = SND_SOC_DAIFMT_I2S |
415 			SND_SOC_DAIFMT_NB_NF |
416 			SND_SOC_DAIFMT_CBS_CFS,
417 		.ignore_pmdown_time = 1,
418 		.be_hw_params_fixup = broxton_ssp_fixup,
419 		.dpcm_playback = 1,
420 	},
421 	{
422 		/* SSP1 - Codec */
423 		.name = "SSP1-Codec",
424 		.id = 1,
425 		.cpu_dai_name = "SSP1 Pin",
426 		.platform_name = "0000:00:0e.0",
427 		.no_pcm = 1,
428 		.codec_name = "i2c-DLGS7219:00",
429 		.codec_dai_name = BXT_DIALOG_CODEC_DAI,
430 		.init = broxton_da7219_codec_init,
431 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
432 			SND_SOC_DAIFMT_CBS_CFS,
433 		.ignore_pmdown_time = 1,
434 		.be_hw_params_fixup = broxton_ssp_fixup,
435 		.ops = &broxton_da7219_ops,
436 		.dpcm_playback = 1,
437 		.dpcm_capture = 1,
438 	},
439 	{
440 		.name = "dmic01",
441 		.id = 2,
442 		.cpu_dai_name = "DMIC01 Pin",
443 		.codec_name = "dmic-codec",
444 		.codec_dai_name = "dmic-hifi",
445 		.platform_name = "0000:00:0e.0",
446 		.ignore_suspend = 1,
447 		.be_hw_params_fixup = broxton_dmic_fixup,
448 		.dpcm_capture = 1,
449 		.no_pcm = 1,
450 	},
451 	{
452 		.name = "iDisp1",
453 		.id = 3,
454 		.cpu_dai_name = "iDisp1 Pin",
455 		.codec_name = "ehdaudio0D2",
456 		.codec_dai_name = "intel-hdmi-hifi1",
457 		.platform_name = "0000:00:0e.0",
458 		.init = broxton_hdmi_init,
459 		.dpcm_playback = 1,
460 		.no_pcm = 1,
461 	},
462 	{
463 		.name = "iDisp2",
464 		.id = 4,
465 		.cpu_dai_name = "iDisp2 Pin",
466 		.codec_name = "ehdaudio0D2",
467 		.codec_dai_name = "intel-hdmi-hifi2",
468 		.platform_name = "0000:00:0e.0",
469 		.init = broxton_hdmi_init,
470 		.dpcm_playback = 1,
471 		.no_pcm = 1,
472 	},
473 	{
474 		.name = "iDisp3",
475 		.id = 5,
476 		.cpu_dai_name = "iDisp3 Pin",
477 		.codec_name = "ehdaudio0D2",
478 		.codec_dai_name = "intel-hdmi-hifi3",
479 		.platform_name = "0000:00:0e.0",
480 		.init = broxton_hdmi_init,
481 		.dpcm_playback = 1,
482 		.no_pcm = 1,
483 	},
484 };
485 
486 /* broxton audio machine driver for SPT + da7219 */
487 static struct snd_soc_card broxton_audio_card = {
488 	.name = "bxtda7219max",
489 	.owner = THIS_MODULE,
490 	.dai_link = broxton_dais,
491 	.num_links = ARRAY_SIZE(broxton_dais),
492 	.controls = broxton_controls,
493 	.num_controls = ARRAY_SIZE(broxton_controls),
494 	.dapm_widgets = broxton_widgets,
495 	.num_dapm_widgets = ARRAY_SIZE(broxton_widgets),
496 	.dapm_routes = broxton_map,
497 	.num_dapm_routes = ARRAY_SIZE(broxton_map),
498 	.fully_routed = true,
499 };
500 
501 static int broxton_audio_probe(struct platform_device *pdev)
502 {
503 	broxton_audio_card.dev = &pdev->dev;
504 	return devm_snd_soc_register_card(&pdev->dev, &broxton_audio_card);
505 }
506 
507 static struct platform_driver broxton_audio = {
508 	.probe = broxton_audio_probe,
509 	.driver = {
510 		.name = "bxt_da7219_max98357a_i2s",
511 		.pm = &snd_soc_pm_ops,
512 	},
513 };
514 module_platform_driver(broxton_audio)
515 
516 /* Module information */
517 MODULE_DESCRIPTION("Audio Machine driver-DA7219 & MAX98357A in I2S mode");
518 MODULE_AUTHOR("Sathyanarayana Nujella <sathyanarayana.nujella@intel.com>");
519 MODULE_AUTHOR("Rohit Ainapure <rohit.m.ainapure@intel.com>");
520 MODULE_AUTHOR("Harsha Priya <harshapriya.n@intel.com>");
521 MODULE_AUTHOR("Conrad Cooke <conrad.cooke@intel.com>");
522 MODULE_LICENSE("GPL v2");
523 MODULE_ALIAS("platform:bxt_da7219_max98357a_i2s");
524