Merge remote-tracking branches 'asoc/topic/davinci' and 'asoc/topic/dwc' into asoc...
[deliverable/linux.git] / sound / soc / intel / boards / skl_nau88l25_max98357a.c
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 .nonatomic = 1,
395 .dynamic = 1,
396 .ops = &skylaye_refcap_ops,
397 },
398 [SKL_DPCM_AUDIO_DMIC_CP] = {
399 .name = "Skl Audio DMIC cap",
400 .stream_name = "dmiccap",
401 .cpu_dai_name = "DMIC Pin",
402 .codec_name = "snd-soc-dummy",
403 .codec_dai_name = "snd-soc-dummy-dai",
404 .platform_name = "0000:00:1f.3",
405 .init = NULL,
406 .dpcm_capture = 1,
407 .nonatomic = 1,
408 .dynamic = 1,
409 .ops = &skylake_dmic_ops,
410 },
411 [SKL_DPCM_AUDIO_HDMI1_PB] = {
412 .name = "Skl 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:1f.3",
418 .dpcm_playback = 1,
419 .init = NULL,
420 .trigger = {
421 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
422 .nonatomic = 1,
423 .dynamic = 1,
424 },
425 [SKL_DPCM_AUDIO_HDMI2_PB] = {
426 .name = "Skl HDMI Port2",
427 .stream_name = "Hdmi2",
428 .cpu_dai_name = "HDMI2 Pin",
429 .codec_name = "snd-soc-dummy",
430 .codec_dai_name = "snd-soc-dummy-dai",
431 .platform_name = "0000:00:1f.3",
432 .dpcm_playback = 1,
433 .init = NULL,
434 .trigger = {
435 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
436 .nonatomic = 1,
437 .dynamic = 1,
438 },
439 [SKL_DPCM_AUDIO_HDMI3_PB] = {
440 .name = "Skl HDMI Port3",
441 .stream_name = "Hdmi3",
442 .cpu_dai_name = "HDMI3 Pin",
443 .codec_name = "snd-soc-dummy",
444 .codec_dai_name = "snd-soc-dummy-dai",
445 .platform_name = "0000:00:1f.3",
446 .trigger = {
447 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
448 .dpcm_playback = 1,
449 .init = NULL,
450 .nonatomic = 1,
451 .dynamic = 1,
452 },
453
454 /* Back End DAI links */
455 {
456 /* SSP0 - Codec */
457 .name = "SSP0-Codec",
458 .id = 0,
459 .cpu_dai_name = "SSP0 Pin",
460 .platform_name = "0000:00:1f.3",
461 .no_pcm = 1,
462 .codec_name = "MX98357A:00",
463 .codec_dai_name = SKL_MAXIM_CODEC_DAI,
464 .dai_fmt = SND_SOC_DAIFMT_I2S |
465 SND_SOC_DAIFMT_NB_NF |
466 SND_SOC_DAIFMT_CBS_CFS,
467 .ignore_pmdown_time = 1,
468 .be_hw_params_fixup = skylake_ssp_fixup,
469 .dpcm_playback = 1,
470 },
471 {
472 /* SSP1 - Codec */
473 .name = "SSP1-Codec",
474 .id = 1,
475 .cpu_dai_name = "SSP1 Pin",
476 .platform_name = "0000:00:1f.3",
477 .no_pcm = 1,
478 .codec_name = "i2c-10508825:00",
479 .codec_dai_name = SKL_NUVOTON_CODEC_DAI,
480 .init = skylake_nau8825_codec_init,
481 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
482 SND_SOC_DAIFMT_CBS_CFS,
483 .ignore_pmdown_time = 1,
484 .be_hw_params_fixup = skylake_ssp_fixup,
485 .ops = &skylake_nau8825_ops,
486 .dpcm_playback = 1,
487 .dpcm_capture = 1,
488 },
489 {
490 .name = "dmic01",
491 .id = 2,
492 .cpu_dai_name = "DMIC01 Pin",
493 .codec_name = "dmic-codec",
494 .codec_dai_name = "dmic-hifi",
495 .platform_name = "0000:00:1f.3",
496 .be_hw_params_fixup = skylake_dmic_fixup,
497 .ignore_suspend = 1,
498 .dpcm_capture = 1,
499 .no_pcm = 1,
500 },
501 {
502 .name = "iDisp1",
503 .id = 3,
504 .cpu_dai_name = "iDisp1 Pin",
505 .codec_name = "ehdaudio0D2",
506 .codec_dai_name = "intel-hdmi-hifi1",
507 .platform_name = "0000:00:1f.3",
508 .dpcm_playback = 1,
509 .init = skylake_hdmi1_init,
510 .no_pcm = 1,
511 },
512 {
513 .name = "iDisp2",
514 .id = 4,
515 .cpu_dai_name = "iDisp2 Pin",
516 .codec_name = "ehdaudio0D2",
517 .codec_dai_name = "intel-hdmi-hifi2",
518 .platform_name = "0000:00:1f.3",
519 .init = skylake_hdmi2_init,
520 .dpcm_playback = 1,
521 .no_pcm = 1,
522 },
523 {
524 .name = "iDisp3",
525 .id = 5,
526 .cpu_dai_name = "iDisp3 Pin",
527 .codec_name = "ehdaudio0D2",
528 .codec_dai_name = "intel-hdmi-hifi3",
529 .platform_name = "0000:00:1f.3",
530 .init = skylake_hdmi3_init,
531 .dpcm_playback = 1,
532 .no_pcm = 1,
533 },
534 };
535
536 /* skylake audio machine driver for SPT + NAU88L25 */
537 static struct snd_soc_card skylake_audio_card = {
538 .name = "sklnau8825max",
539 .owner = THIS_MODULE,
540 .dai_link = skylake_dais,
541 .num_links = ARRAY_SIZE(skylake_dais),
542 .controls = skylake_controls,
543 .num_controls = ARRAY_SIZE(skylake_controls),
544 .dapm_widgets = skylake_widgets,
545 .num_dapm_widgets = ARRAY_SIZE(skylake_widgets),
546 .dapm_routes = skylake_map,
547 .num_dapm_routes = ARRAY_SIZE(skylake_map),
548 .fully_routed = true,
549 };
550
551 static int skylake_audio_probe(struct platform_device *pdev)
552 {
553 skylake_audio_card.dev = &pdev->dev;
554
555 return devm_snd_soc_register_card(&pdev->dev, &skylake_audio_card);
556 }
557
558 static struct platform_driver skylake_audio = {
559 .probe = skylake_audio_probe,
560 .driver = {
561 .name = "skl_nau88l25_max98357a_i2s",
562 .pm = &snd_soc_pm_ops,
563 },
564 };
565
566 module_platform_driver(skylake_audio)
567
568 /* Module information */
569 MODULE_DESCRIPTION("Audio Machine driver-NAU88L25 & MAX98357A in I2S mode");
570 MODULE_AUTHOR("Rohit Ainapure <rohit.m.ainapure@intel.com");
571 MODULE_LICENSE("GPL v2");
572 MODULE_ALIAS("platform:skl_nau88l25_max98357a_i2s");
This page took 0.043586 seconds and 6 git commands to generate.