1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2020 Intel Corporation
4 #include <linux/module.h>
5 #include <linux/string.h>
7 #include <sound/pcm_params.h>
9 #include <sound/soc-acpi.h>
10 #include <sound/soc-dai.h>
11 #include <sound/soc-dapm.h>
12 #include <sound/sof.h>
13 #include <uapi/sound/asound.h>
14 #include "../common/soc-intel-quirks.h"
15 #include "sof_maxim_common.h"
18 * Common structures and functions
20 static const struct snd_kcontrol_new maxim_2spk_kcontrols[] = {
21 SOC_DAPM_PIN_SWITCH("Left Spk"),
22 SOC_DAPM_PIN_SWITCH("Right Spk"),
26 static const struct snd_soc_dapm_widget maxim_2spk_widgets[] = {
27 SND_SOC_DAPM_SPK("Left Spk", NULL),
28 SND_SOC_DAPM_SPK("Right Spk", NULL),
31 /* helper function to get the number of specific codec */
32 static unsigned int get_num_codecs(const char *hid)
34 struct acpi_device *adev;
35 unsigned int dev_num = 0;
37 for_each_acpi_dev_match(adev, hid, NULL, -1)
46 #define MAX_98373_PIN_NAME 16
48 static const struct snd_soc_dapm_route max_98373_dapm_routes[] = {
50 { "Left Spk", NULL, "Left BE_OUT" },
51 { "Right Spk", NULL, "Right BE_OUT" },
54 static struct snd_soc_codec_conf max_98373_codec_conf[] = {
56 .dlc = COMP_CODEC_CONF(MAX_98373_DEV0_NAME),
57 .name_prefix = "Right",
60 .dlc = COMP_CODEC_CONF(MAX_98373_DEV1_NAME),
61 .name_prefix = "Left",
65 static struct snd_soc_dai_link_component max_98373_components[] = {
67 .name = MAX_98373_DEV0_NAME,
68 .dai_name = MAX_98373_CODEC_DAI,
71 .name = MAX_98373_DEV1_NAME,
72 .dai_name = MAX_98373_CODEC_DAI,
77 * According to the definition of 'DAI Sel Mux' mixer in max98373.c, rx mask
78 * should choose two channels from TDM slots, the LSB of rx mask is left channel
79 * and the other one is right channel.
83 } max_98373_tdm_mask[] = {
89 * The tx mask indicates which channel(s) contains output IV-sense data and
90 * others should set to Hi-Z. Here we get the channel number from codec's ACPI
91 * device property "maxim,vmon-slot-no" and "maxim,imon-slot-no" to generate the
92 * mask. Refer to the max98373_slot_config() function in max98373.c codec driver.
94 static unsigned int max_98373_get_tx_mask(struct device *dev)
99 if (device_property_read_u32(dev, "maxim,vmon-slot-no", &vmon_slot))
102 if (device_property_read_u32(dev, "maxim,imon-slot-no", &imon_slot))
105 dev_dbg(dev, "vmon_slot %d imon_slot %d\n", vmon_slot, imon_slot);
107 return (0x1 << vmon_slot) | (0x1 << imon_slot);
110 static int max_98373_hw_params(struct snd_pcm_substream *substream,
111 struct snd_pcm_hw_params *params)
113 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
114 struct snd_soc_dai_link *dai_link = rtd->dai_link;
115 struct snd_soc_dai *codec_dai;
118 unsigned int tx_mask;
119 unsigned int tx_mask_used = 0x0;
122 for_each_rtd_codec_dais(rtd, i, codec_dai) {
123 if (i >= ARRAY_SIZE(max_98373_tdm_mask)) {
124 dev_err(codec_dai->dev, "only 2 amps are supported\n");
128 switch (dai_link->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
129 case SND_SOC_DAIFMT_DSP_A:
130 case SND_SOC_DAIFMT_DSP_B:
131 /* get the tplg configured tdm slot number */
132 tdm_slots = sof_dai_get_tdm_slots(rtd);
133 if (tdm_slots <= 0) {
134 dev_err(rtd->dev, "invalid tdm slots %d\n",
139 /* get the tx mask from ACPI device properties */
140 tx_mask = max_98373_get_tx_mask(codec_dai->dev);
144 if (tx_mask & tx_mask_used) {
145 dev_err(codec_dai->dev, "invalid tx mask 0x%x, used 0x%x\n",
146 tx_mask, tx_mask_used);
150 tx_mask_used |= tx_mask;
153 * check if tdm slot number is too small for channel
156 if (fls(tx_mask) > tdm_slots) {
157 dev_err(codec_dai->dev, "slot mismatch, tx %d slots %d\n",
158 fls(tx_mask), tdm_slots);
162 if (fls(max_98373_tdm_mask[i].rx) > tdm_slots) {
163 dev_err(codec_dai->dev, "slot mismatch, rx %d slots %d\n",
164 fls(max_98373_tdm_mask[i].rx), tdm_slots);
168 dev_dbg(codec_dai->dev, "set tdm slot: tx 0x%x rx 0x%x slots %d width %d\n",
169 tx_mask, max_98373_tdm_mask[i].rx,
170 tdm_slots, params_width(params));
172 ret = snd_soc_dai_set_tdm_slot(codec_dai, tx_mask,
173 max_98373_tdm_mask[i].rx,
175 params_width(params));
177 dev_err(codec_dai->dev, "fail to set tdm slot, ret %d\n",
183 dev_dbg(codec_dai->dev, "codec is in I2S mode\n");
190 static int max_98373_trigger(struct snd_pcm_substream *substream, int cmd)
192 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
193 struct snd_soc_dai *codec_dai;
194 struct snd_soc_dai *cpu_dai;
198 /* set spk pin by playback only */
199 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
202 cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
203 for_each_rtd_codec_dais(rtd, j, codec_dai) {
204 struct snd_soc_dapm_context *dapm =
205 snd_soc_component_get_dapm(cpu_dai->component);
206 char pin_name[MAX_98373_PIN_NAME];
208 snprintf(pin_name, ARRAY_SIZE(pin_name), "%s Spk",
209 codec_dai->component->name_prefix);
212 case SNDRV_PCM_TRIGGER_START:
213 case SNDRV_PCM_TRIGGER_RESUME:
214 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
215 ret = snd_soc_dapm_enable_pin(dapm, pin_name);
217 snd_soc_dapm_sync(dapm);
219 case SNDRV_PCM_TRIGGER_STOP:
220 case SNDRV_PCM_TRIGGER_SUSPEND:
221 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
222 ret = snd_soc_dapm_disable_pin(dapm, pin_name);
224 snd_soc_dapm_sync(dapm);
234 static const struct snd_soc_ops max_98373_ops = {
235 .hw_params = max_98373_hw_params,
236 .trigger = max_98373_trigger,
239 static int max_98373_spk_codec_init(struct snd_soc_pcm_runtime *rtd)
241 struct snd_soc_card *card = rtd->card;
242 unsigned int num_codecs = get_num_codecs(MAX_98373_ACPI_HID);
245 switch (num_codecs) {
247 ret = snd_soc_dapm_new_controls(&card->dapm, maxim_2spk_widgets,
248 ARRAY_SIZE(maxim_2spk_widgets));
250 dev_err(rtd->dev, "fail to add max98373 widgets, ret %d\n",
255 ret = snd_soc_add_card_controls(card, maxim_2spk_kcontrols,
256 ARRAY_SIZE(maxim_2spk_kcontrols));
258 dev_err(rtd->dev, "fail to add max98373 kcontrols, ret %d\n",
263 ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes,
264 ARRAY_SIZE(max_98373_dapm_routes));
266 dev_err(rtd->dev, "fail to add max98373 routes, ret %d\n",
272 dev_err(rtd->dev, "max98373: invalid num_codecs %d\n", num_codecs);
279 void max_98373_dai_link(struct device *dev, struct snd_soc_dai_link *link)
281 link->codecs = max_98373_components;
282 link->num_codecs = ARRAY_SIZE(max_98373_components);
283 link->init = max_98373_spk_codec_init;
284 link->ops = &max_98373_ops;
286 EXPORT_SYMBOL_NS(max_98373_dai_link, "SND_SOC_INTEL_SOF_MAXIM_COMMON");
288 void max_98373_set_codec_conf(struct snd_soc_card *card)
290 card->codec_conf = max_98373_codec_conf;
291 card->num_configs = ARRAY_SIZE(max_98373_codec_conf);
293 EXPORT_SYMBOL_NS(max_98373_set_codec_conf, "SND_SOC_INTEL_SOF_MAXIM_COMMON");
298 static const struct snd_soc_dapm_route max_98390_dapm_routes[] = {
300 { "Left Spk", NULL, "Left BE_OUT" },
301 { "Right Spk", NULL, "Right BE_OUT" },
304 static const struct snd_kcontrol_new max_98390_tt_kcontrols[] = {
305 SOC_DAPM_PIN_SWITCH("TL Spk"),
306 SOC_DAPM_PIN_SWITCH("TR Spk"),
309 static const struct snd_soc_dapm_widget max_98390_tt_dapm_widgets[] = {
310 SND_SOC_DAPM_SPK("TL Spk", NULL),
311 SND_SOC_DAPM_SPK("TR Spk", NULL),
314 static const struct snd_soc_dapm_route max_98390_tt_dapm_routes[] = {
315 /* Tweeter speaker */
316 { "TL Spk", NULL, "Tweeter Left BE_OUT" },
317 { "TR Spk", NULL, "Tweeter Right BE_OUT" },
320 static struct snd_soc_codec_conf max_98390_cml_codec_conf[] = {
322 .dlc = COMP_CODEC_CONF(MAX_98390_DEV0_NAME),
323 .name_prefix = "Left",
326 .dlc = COMP_CODEC_CONF(MAX_98390_DEV1_NAME),
327 .name_prefix = "Right",
331 static struct snd_soc_codec_conf max_98390_codec_conf[] = {
333 .dlc = COMP_CODEC_CONF(MAX_98390_DEV0_NAME),
334 .name_prefix = "Right",
337 .dlc = COMP_CODEC_CONF(MAX_98390_DEV1_NAME),
338 .name_prefix = "Left",
341 .dlc = COMP_CODEC_CONF(MAX_98390_DEV2_NAME),
342 .name_prefix = "Tweeter Right",
345 .dlc = COMP_CODEC_CONF(MAX_98390_DEV3_NAME),
346 .name_prefix = "Tweeter Left",
350 static struct snd_soc_dai_link_component max_98390_components[] = {
352 .name = MAX_98390_DEV0_NAME,
353 .dai_name = MAX_98390_CODEC_DAI,
356 .name = MAX_98390_DEV1_NAME,
357 .dai_name = MAX_98390_CODEC_DAI,
360 .name = MAX_98390_DEV2_NAME,
361 .dai_name = MAX_98390_CODEC_DAI,
364 .name = MAX_98390_DEV3_NAME,
365 .dai_name = MAX_98390_CODEC_DAI,
369 static const struct {
372 } max_98390_tdm_mask[] = {
373 {.tx = 0x01, .rx = 0x3},
374 {.tx = 0x02, .rx = 0x3},
375 {.tx = 0x04, .rx = 0x3},
376 {.tx = 0x08, .rx = 0x3},
379 static int max_98390_hw_params(struct snd_pcm_substream *substream,
380 struct snd_pcm_hw_params *params)
382 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
383 struct snd_soc_dai_link *dai_link = rtd->dai_link;
384 struct snd_soc_dai *codec_dai;
387 for_each_rtd_codec_dais(rtd, i, codec_dai) {
388 if (i >= ARRAY_SIZE(max_98390_tdm_mask)) {
389 dev_err(codec_dai->dev, "invalid codec index %d\n", i);
393 switch (dai_link->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
394 case SND_SOC_DAIFMT_DSP_A:
395 case SND_SOC_DAIFMT_DSP_B:
397 ret = snd_soc_dai_set_tdm_slot(codec_dai,
398 max_98390_tdm_mask[i].tx,
399 max_98390_tdm_mask[i].rx,
401 params_width(params));
403 dev_err(codec_dai->dev, "fail to set tdm slot, ret %d\n",
409 dev_dbg(codec_dai->dev, "codec is in I2S mode\n");
416 static int max_98390_init(struct snd_soc_pcm_runtime *rtd)
418 struct snd_soc_card *card = rtd->card;
419 unsigned int num_codecs = get_num_codecs(MAX_98390_ACPI_HID);
422 switch (num_codecs) {
424 /* add widgets/controls/dapm for tweeter speakers */
425 ret = snd_soc_dapm_new_controls(&card->dapm, max_98390_tt_dapm_widgets,
426 ARRAY_SIZE(max_98390_tt_dapm_widgets));
428 dev_err(rtd->dev, "unable to add tweeter dapm widgets, ret %d\n",
430 /* Don't need to add routes if widget addition failed */
434 ret = snd_soc_add_card_controls(card, max_98390_tt_kcontrols,
435 ARRAY_SIZE(max_98390_tt_kcontrols));
437 dev_err(rtd->dev, "unable to add tweeter controls, ret %d\n",
442 ret = snd_soc_dapm_add_routes(&card->dapm, max_98390_tt_dapm_routes,
443 ARRAY_SIZE(max_98390_tt_dapm_routes));
445 dev_err(rtd->dev, "unable to add tweeter dapm routes, ret %d\n",
452 /* add regular speakers dapm route */
453 ret = snd_soc_dapm_new_controls(&card->dapm, maxim_2spk_widgets,
454 ARRAY_SIZE(maxim_2spk_widgets));
456 dev_err(rtd->dev, "fail to add max98390 woofer widgets, ret %d\n",
461 ret = snd_soc_add_card_controls(card, maxim_2spk_kcontrols,
462 ARRAY_SIZE(maxim_2spk_kcontrols));
464 dev_err(rtd->dev, "fail to add max98390 woofer kcontrols, ret %d\n",
469 ret = snd_soc_dapm_add_routes(&card->dapm, max_98390_dapm_routes,
470 ARRAY_SIZE(max_98390_dapm_routes));
472 dev_err(rtd->dev, "unable to add dapm routes, ret %d\n",
478 dev_err(rtd->dev, "invalid codec number %d\n", num_codecs);
485 static const struct snd_soc_ops max_98390_ops = {
486 .hw_params = max_98390_hw_params,
489 void max_98390_dai_link(struct device *dev, struct snd_soc_dai_link *link)
491 unsigned int num_codecs = get_num_codecs(MAX_98390_ACPI_HID);
493 link->codecs = max_98390_components;
495 switch (num_codecs) {
498 link->num_codecs = num_codecs;
501 dev_err(dev, "invalid codec number %d for %s\n", num_codecs,
506 link->init = max_98390_init;
507 link->ops = &max_98390_ops;
509 EXPORT_SYMBOL_NS(max_98390_dai_link, "SND_SOC_INTEL_SOF_MAXIM_COMMON");
511 void max_98390_set_codec_conf(struct device *dev, struct snd_soc_card *card)
513 unsigned int num_codecs = get_num_codecs(MAX_98390_ACPI_HID);
515 card->codec_conf = max_98390_codec_conf;
517 switch (num_codecs) {
519 if (soc_intel_is_cml())
520 card->codec_conf = max_98390_cml_codec_conf;
524 card->num_configs = num_codecs;
527 dev_err(dev, "invalid codec number %d for %s\n", num_codecs,
532 EXPORT_SYMBOL_NS(max_98390_set_codec_conf, "SND_SOC_INTEL_SOF_MAXIM_COMMON");
535 * Maxim MAX98357A/MAX98360A
537 static const struct snd_kcontrol_new max_98357a_kcontrols[] = {
538 SOC_DAPM_PIN_SWITCH("Spk"),
541 static const struct snd_soc_dapm_widget max_98357a_dapm_widgets[] = {
542 SND_SOC_DAPM_SPK("Spk", NULL),
545 static const struct snd_soc_dapm_route max_98357a_dapm_routes[] = {
547 {"Spk", NULL, "Speaker"},
550 static struct snd_soc_dai_link_component max_98357a_components[] = {
552 .name = MAX_98357A_DEV0_NAME,
553 .dai_name = MAX_98357A_CODEC_DAI,
557 static struct snd_soc_dai_link_component max_98360a_components[] = {
559 .name = MAX_98360A_DEV0_NAME,
560 .dai_name = MAX_98357A_CODEC_DAI,
564 static int max_98357a_init(struct snd_soc_pcm_runtime *rtd)
566 struct snd_soc_card *card = rtd->card;
569 ret = snd_soc_dapm_new_controls(&card->dapm, max_98357a_dapm_widgets,
570 ARRAY_SIZE(max_98357a_dapm_widgets));
572 dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
573 /* Don't need to add routes if widget addition failed */
577 ret = snd_soc_add_card_controls(card, max_98357a_kcontrols,
578 ARRAY_SIZE(max_98357a_kcontrols));
580 dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
584 ret = snd_soc_dapm_add_routes(&card->dapm, max_98357a_dapm_routes,
585 ARRAY_SIZE(max_98357a_dapm_routes));
588 dev_err(rtd->dev, "unable to add dapm routes, ret %d\n", ret);
593 void max_98357a_dai_link(struct snd_soc_dai_link *link)
595 link->codecs = max_98357a_components;
596 link->num_codecs = ARRAY_SIZE(max_98357a_components);
597 link->init = max_98357a_init;
599 EXPORT_SYMBOL_NS(max_98357a_dai_link, "SND_SOC_INTEL_SOF_MAXIM_COMMON");
601 void max_98360a_dai_link(struct snd_soc_dai_link *link)
603 link->codecs = max_98360a_components;
604 link->num_codecs = ARRAY_SIZE(max_98360a_components);
605 link->init = max_98357a_init;
607 EXPORT_SYMBOL_NS(max_98360a_dai_link, "SND_SOC_INTEL_SOF_MAXIM_COMMON");
609 MODULE_DESCRIPTION("ASoC Intel SOF Maxim helpers");
610 MODULE_LICENSE("GPL");