Merge tag 'sound-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[linux-block.git] / sound / soc / intel / boards / sof_sdw_max98373.c
CommitLineData
be82e888
NM
1// SPDX-License-Identifier: GPL-2.0-only
2// Copyright (c) 2020 Intel Corporation
3//
4// sof_sdw_max98373 - Helpers to handle 2x MAX98373
5// codec devices from generic machine driver
6
7#include <linux/device.h>
8#include <linux/errno.h>
3f2c6564 9#include <sound/control.h>
be82e888
NM
10#include <sound/soc.h>
11#include <sound/soc-acpi.h>
3f2c6564 12#include <sound/soc-dapm.h>
be82e888
NM
13#include "sof_sdw_common.h"
14#include "sof_maxim_common.h"
15
16static const struct snd_soc_dapm_widget mx8373_widgets[] = {
17 SND_SOC_DAPM_SPK("Left Spk", NULL),
18 SND_SOC_DAPM_SPK("Right Spk", NULL),
19};
20
21static const struct snd_kcontrol_new mx8373_controls[] = {
22 SOC_DAPM_PIN_SWITCH("Left Spk"),
23 SOC_DAPM_PIN_SWITCH("Right Spk"),
24};
25
26static int spk_init(struct snd_soc_pcm_runtime *rtd)
27{
28 struct snd_soc_card *card = rtd->card;
29 int ret;
30
31 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
32 "%s spk:mx8373",
33 card->components);
34 if (!card->components)
35 return -ENOMEM;
36
37 ret = snd_soc_add_card_controls(card, mx8373_controls,
38 ARRAY_SIZE(mx8373_controls));
39 if (ret) {
40 dev_err(card->dev, "mx8373 ctrls addition failed: %d\n", ret);
41 return ret;
42 }
43
44 ret = snd_soc_dapm_new_controls(&card->dapm, mx8373_widgets,
45 ARRAY_SIZE(mx8373_widgets));
46 if (ret) {
47 dev_err(card->dev, "mx8373 widgets addition failed: %d\n", ret);
48 return ret;
49 }
50
51 ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes, 2);
52 if (ret)
53 dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
54
55 return ret;
56}
57
33c85168 58static int mx8373_enable_spk_pin(struct snd_pcm_substream *substream, bool enable)
7cc3b56f 59{
33c85168
RW
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;
7cc3b56f 63 int ret;
33c85168 64 int j;
7cc3b56f 65
33c85168
RW
66 /* set spk pin by playback only */
67 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
68 return 0;
69
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);
74 char pin_name[16];
75
76 snprintf(pin_name, ARRAY_SIZE(pin_name), "%s Spk",
77 codec_dai->component->name_prefix);
78
79 if (enable)
80 ret = snd_soc_dapm_enable_pin(dapm, pin_name);
81 else
82 ret = snd_soc_dapm_disable_pin(dapm, pin_name);
83
84 if (!ret)
85 snd_soc_dapm_sync(dapm);
7cc3b56f
RW
86 }
87
33c85168
RW
88 return 0;
89}
90
91static int mx8373_sdw_prepare(struct snd_pcm_substream *substream)
92{
d4321277 93 int ret;
33c85168
RW
94
95 /* according to soc_pcm_prepare dai link prepare is called first */
96 ret = sdw_prepare(substream);
97 if (ret < 0)
98 return ret;
99
100 return mx8373_enable_spk_pin(substream, true);
101}
102
103static int mx8373_sdw_hw_free(struct snd_pcm_substream *substream)
104{
d4321277 105 int ret;
33c85168
RW
106
107 /* according to soc_pcm_hw_free dai link free is called first */
108 ret = sdw_hw_free(substream);
109 if (ret < 0)
110 return ret;
111
112 return mx8373_enable_spk_pin(substream, false);
7cc3b56f
RW
113}
114
be82e888
NM
115static const struct snd_soc_ops max_98373_sdw_ops = {
116 .startup = sdw_startup,
33c85168
RW
117 .prepare = mx8373_sdw_prepare,
118 .trigger = sdw_trigger,
119 .hw_free = mx8373_sdw_hw_free,
be82e888
NM
120 .shutdown = sdw_shutdown,
121};
122
e9fcbaff
YZ
123static int mx8373_sdw_late_probe(struct snd_soc_card *card)
124{
125 struct snd_soc_dapm_context *dapm = &card->dapm;
126
127 /* Disable Left and Right Spk pin after boot */
128 snd_soc_dapm_disable_pin(dapm, "Left Spk");
129 snd_soc_dapm_disable_pin(dapm, "Right Spk");
130 return snd_soc_dapm_sync(dapm);
131}
132
cdf99c9a
PLB
133int sof_sdw_mx8373_init(struct snd_soc_card *card,
134 const struct snd_soc_acpi_link_adr *link,
be82e888
NM
135 struct snd_soc_dai_link *dai_links,
136 struct sof_sdw_codec_info *info,
137 bool playback)
138{
139 info->amp_num++;
140 if (info->amp_num == 2)
141 dai_links->init = spk_init;
142
e9fcbaff 143 info->codec_card_late_probe = mx8373_sdw_late_probe;
be3afa12 144
be82e888
NM
145 dai_links->ops = &max_98373_sdw_ops;
146
147 return 0;
148}