1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Intel Corporation
4 // sof_sdw_max98373 - Helpers to handle 2x MAX98373
5 // codec devices from generic machine driver
7 #include <linux/device.h>
8 #include <linux/errno.h>
9 #include <sound/control.h>
10 #include <sound/soc.h>
11 #include <sound/soc-acpi.h>
12 #include <sound/soc-dapm.h>
13 #include "sof_sdw_common.h"
14 #include "sof_maxim_common.h"
16 static const struct snd_soc_dapm_widget mx8373_widgets[] = {
17 SND_SOC_DAPM_SPK("Left Spk", NULL),
18 SND_SOC_DAPM_SPK("Right Spk", NULL),
21 static const struct snd_kcontrol_new mx8373_controls[] = {
22 SOC_DAPM_PIN_SWITCH("Left Spk"),
23 SOC_DAPM_PIN_SWITCH("Right Spk"),
26 static int spk_init(struct snd_soc_pcm_runtime *rtd)
28 struct snd_soc_card *card = rtd->card;
31 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
34 if (!card->components)
37 ret = snd_soc_add_card_controls(card, mx8373_controls,
38 ARRAY_SIZE(mx8373_controls));
40 dev_err(card->dev, "mx8373 ctrls addition failed: %d\n", ret);
44 ret = snd_soc_dapm_new_controls(&card->dapm, mx8373_widgets,
45 ARRAY_SIZE(mx8373_widgets));
47 dev_err(card->dev, "mx8373 widgets addition failed: %d\n", ret);
51 ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes, 2);
53 dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
58 static int mx8373_enable_spk_pin(struct snd_pcm_substream *substream, bool enable)
60 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
61 struct snd_soc_dai *codec_dai;
62 struct snd_soc_dai *cpu_dai;
66 /* set spk pin by playback only */
67 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
70 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
71 for_each_rtd_codec_dais(rtd, j, codec_dai) {
72 struct snd_soc_dapm_context *dapm =
73 snd_soc_component_get_dapm(cpu_dai->component);
76 snprintf(pin_name, ARRAY_SIZE(pin_name), "%s Spk",
77 codec_dai->component->name_prefix);
80 ret = snd_soc_dapm_enable_pin(dapm, pin_name);
82 ret = snd_soc_dapm_disable_pin(dapm, pin_name);
85 snd_soc_dapm_sync(dapm);
91 static int mx8373_sdw_prepare(struct snd_pcm_substream *substream)
95 /* according to soc_pcm_prepare dai link prepare is called first */
96 ret = sdw_prepare(substream);
100 return mx8373_enable_spk_pin(substream, true);
103 static int mx8373_sdw_hw_free(struct snd_pcm_substream *substream)
107 /* according to soc_pcm_hw_free dai link free is called first */
108 ret = sdw_hw_free(substream);
112 return mx8373_enable_spk_pin(substream, false);
115 static const struct snd_soc_ops max_98373_sdw_ops = {
116 .startup = sdw_startup,
117 .prepare = mx8373_sdw_prepare,
118 .trigger = sdw_trigger,
119 .hw_free = mx8373_sdw_hw_free,
120 .shutdown = sdw_shutdown,
123 int sof_sdw_mx8373_init(struct snd_soc_card *card,
124 const struct snd_soc_acpi_link_adr *link,
125 struct snd_soc_dai_link *dai_links,
126 struct sof_sdw_codec_info *info,
130 if (info->amp_num == 2)
131 dai_links->init = spk_init;
133 info->late_probe = true;
135 dai_links->ops = &max_98373_sdw_ops;
140 int sof_sdw_mx8373_late_probe(struct snd_soc_card *card)
142 struct snd_soc_dapm_context *dapm = &card->dapm;
144 /* Disable Left and Right Spk pin after boot */
145 snd_soc_dapm_disable_pin(dapm, "Left Spk");
146 snd_soc_dapm_disable_pin(dapm, "Right Spk");
147 return snd_soc_dapm_sync(dapm);