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