1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2020 Intel Corporation. All rights reserved.
4 #include <linux/module.h>
5 #include <linux/string.h>
6 #include <sound/pcm.h>
7 #include <sound/soc.h>
8 #include <sound/soc-dai.h>
9 #include <sound/soc-dapm.h>
10 #include <uapi/sound/asound.h>
11 #include "sof_maxim_common.h"
12 
13 #define MAX_98373_PIN_NAME 16
14 
15 const struct snd_soc_dapm_route max_98373_dapm_routes[] = {
16 	/* speaker */
17 	{ "Left Spk", NULL, "Left BE_OUT" },
18 	{ "Right Spk", NULL, "Right BE_OUT" },
19 };
20 EXPORT_SYMBOL_NS(max_98373_dapm_routes, SND_SOC_INTEL_SOF_MAXIM_COMMON);
21 
22 static struct snd_soc_codec_conf max_98373_codec_conf[] = {
23 	{
24 		.dlc = COMP_CODEC_CONF(MAX_98373_DEV0_NAME),
25 		.name_prefix = "Right",
26 	},
27 	{
28 		.dlc = COMP_CODEC_CONF(MAX_98373_DEV1_NAME),
29 		.name_prefix = "Left",
30 	},
31 };
32 
33 struct snd_soc_dai_link_component max_98373_components[] = {
34 	{  /* For Right */
35 		.name = MAX_98373_DEV0_NAME,
36 		.dai_name = MAX_98373_CODEC_DAI,
37 	},
38 	{  /* For Left */
39 		.name = MAX_98373_DEV1_NAME,
40 		.dai_name = MAX_98373_CODEC_DAI,
41 	},
42 };
43 EXPORT_SYMBOL_NS(max_98373_components, SND_SOC_INTEL_SOF_MAXIM_COMMON);
44 
45 static int max_98373_hw_params(struct snd_pcm_substream *substream,
46 			       struct snd_pcm_hw_params *params)
47 {
48 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
49 	struct snd_soc_dai *codec_dai;
50 	int j;
51 
52 	for_each_rtd_codec_dais(rtd, j, codec_dai) {
53 		if (!strcmp(codec_dai->component->name, MAX_98373_DEV0_NAME)) {
54 			/* DEV0 tdm slot configuration */
55 			snd_soc_dai_set_tdm_slot(codec_dai, 0x03, 3, 8, 32);
56 		}
57 		if (!strcmp(codec_dai->component->name, MAX_98373_DEV1_NAME)) {
58 			/* DEV1 tdm slot configuration */
59 			snd_soc_dai_set_tdm_slot(codec_dai, 0x0C, 3, 8, 32);
60 		}
61 	}
62 	return 0;
63 }
64 
65 int max_98373_trigger(struct snd_pcm_substream *substream, int cmd)
66 {
67 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
68 	struct snd_soc_dai *codec_dai;
69 	struct snd_soc_dai *cpu_dai;
70 	int j;
71 	int ret = 0;
72 
73 	/* set spk pin by playback only */
74 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
75 		return 0;
76 
77 	cpu_dai = asoc_rtd_to_cpu(rtd, 0);
78 	for_each_rtd_codec_dais(rtd, j, codec_dai) {
79 		struct snd_soc_dapm_context *dapm =
80 				snd_soc_component_get_dapm(cpu_dai->component);
81 		char pin_name[MAX_98373_PIN_NAME];
82 
83 		snprintf(pin_name, ARRAY_SIZE(pin_name), "%s Spk",
84 			 codec_dai->component->name_prefix);
85 
86 		switch (cmd) {
87 		case SNDRV_PCM_TRIGGER_START:
88 		case SNDRV_PCM_TRIGGER_RESUME:
89 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
90 			ret = snd_soc_dapm_enable_pin(dapm, pin_name);
91 			if (!ret)
92 				snd_soc_dapm_sync(dapm);
93 			break;
94 		case SNDRV_PCM_TRIGGER_STOP:
95 		case SNDRV_PCM_TRIGGER_SUSPEND:
96 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
97 			ret = snd_soc_dapm_disable_pin(dapm, pin_name);
98 			if (!ret)
99 				snd_soc_dapm_sync(dapm);
100 			break;
101 		default:
102 			break;
103 		}
104 	}
105 
106 	return ret;
107 }
108 EXPORT_SYMBOL_NS(max_98373_trigger, SND_SOC_INTEL_SOF_MAXIM_COMMON);
109 
110 struct snd_soc_ops max_98373_ops = {
111 	.hw_params = max_98373_hw_params,
112 	.trigger = max_98373_trigger,
113 };
114 EXPORT_SYMBOL_NS(max_98373_ops, SND_SOC_INTEL_SOF_MAXIM_COMMON);
115 
116 int max_98373_spk_codec_init(struct snd_soc_pcm_runtime *rtd)
117 {
118 	struct snd_soc_card *card = rtd->card;
119 	int ret;
120 
121 	ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes,
122 				      ARRAY_SIZE(max_98373_dapm_routes));
123 	if (ret)
124 		dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
125 	return ret;
126 }
127 EXPORT_SYMBOL_NS(max_98373_spk_codec_init, SND_SOC_INTEL_SOF_MAXIM_COMMON);
128 
129 void max_98373_set_codec_conf(struct snd_soc_card *card)
130 {
131 	card->codec_conf = max_98373_codec_conf;
132 	card->num_configs = ARRAY_SIZE(max_98373_codec_conf);
133 }
134 EXPORT_SYMBOL_NS(max_98373_set_codec_conf, SND_SOC_INTEL_SOF_MAXIM_COMMON);
135 
136 /*
137  * Maxim MAX98357A/MAX98360A
138  */
139 static const struct snd_kcontrol_new max_98357a_kcontrols[] = {
140 	SOC_DAPM_PIN_SWITCH("Spk"),
141 };
142 
143 static const struct snd_soc_dapm_widget max_98357a_dapm_widgets[] = {
144 	SND_SOC_DAPM_SPK("Spk", NULL),
145 };
146 
147 static const struct snd_soc_dapm_route max_98357a_dapm_routes[] = {
148 	/* speaker */
149 	{"Spk", NULL, "Speaker"},
150 };
151 
152 static struct snd_soc_dai_link_component max_98357a_components[] = {
153 	{
154 		.name = MAX_98357A_DEV0_NAME,
155 		.dai_name = MAX_98357A_CODEC_DAI,
156 	}
157 };
158 
159 static struct snd_soc_dai_link_component max_98360a_components[] = {
160 	{
161 		.name = MAX_98360A_DEV0_NAME,
162 		.dai_name = MAX_98357A_CODEC_DAI,
163 	}
164 };
165 
166 static int max_98357a_init(struct snd_soc_pcm_runtime *rtd)
167 {
168 	struct snd_soc_card *card = rtd->card;
169 	int ret;
170 
171 	ret = snd_soc_dapm_new_controls(&card->dapm, max_98357a_dapm_widgets,
172 					ARRAY_SIZE(max_98357a_dapm_widgets));
173 	if (ret) {
174 		dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
175 		/* Don't need to add routes if widget addition failed */
176 		return ret;
177 	}
178 
179 	ret = snd_soc_add_card_controls(card, max_98357a_kcontrols,
180 					ARRAY_SIZE(max_98357a_kcontrols));
181 	if (ret) {
182 		dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
183 		return ret;
184 	}
185 
186 	ret = snd_soc_dapm_add_routes(&card->dapm, max_98357a_dapm_routes,
187 				      ARRAY_SIZE(max_98357a_dapm_routes));
188 
189 	if (ret)
190 		dev_err(rtd->dev, "unable to add dapm routes, ret %d\n", ret);
191 
192 	return ret;
193 }
194 
195 void max_98357a_dai_link(struct snd_soc_dai_link *link)
196 {
197 	link->codecs = max_98357a_components;
198 	link->num_codecs = ARRAY_SIZE(max_98357a_components);
199 	link->init = max_98357a_init;
200 }
201 EXPORT_SYMBOL_NS(max_98357a_dai_link, SND_SOC_INTEL_SOF_MAXIM_COMMON);
202 
203 void max_98360a_dai_link(struct snd_soc_dai_link *link)
204 {
205 	link->codecs = max_98360a_components;
206 	link->num_codecs = ARRAY_SIZE(max_98360a_components);
207 	link->init = max_98357a_init;
208 }
209 EXPORT_SYMBOL_NS(max_98360a_dai_link, SND_SOC_INTEL_SOF_MAXIM_COMMON);
210 
211 MODULE_DESCRIPTION("ASoC Intel SOF Maxim helpers");
212 MODULE_LICENSE("GPL");
213