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