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
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 int max_98357a_init(struct snd_soc_pcm_runtime *rtd)
160 {
161 	struct snd_soc_card *card = rtd->card;
162 	int ret;
163 
164 	ret = snd_soc_dapm_new_controls(&card->dapm, max_98357a_dapm_widgets,
165 					ARRAY_SIZE(max_98357a_dapm_widgets));
166 	if (ret) {
167 		dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
168 		/* Don't need to add routes if widget addition failed */
169 		return ret;
170 	}
171 
172 	ret = snd_soc_add_card_controls(card, max_98357a_kcontrols,
173 					ARRAY_SIZE(max_98357a_kcontrols));
174 	if (ret) {
175 		dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
176 		return ret;
177 	}
178 
179 	ret = snd_soc_dapm_add_routes(&card->dapm, max_98357a_dapm_routes,
180 				      ARRAY_SIZE(max_98357a_dapm_routes));
181 
182 	if (ret)
183 		dev_err(rtd->dev, "unable to add dapm routes, ret %d\n", ret);
184 
185 	return ret;
186 }
187 
188 void max_98357a_dai_link(struct snd_soc_dai_link *link)
189 {
190 	link->codecs = max_98357a_components;
191 	link->num_codecs = ARRAY_SIZE(max_98357a_components);
192 	link->init = max_98357a_init;
193 }
194 EXPORT_SYMBOL_NS(max_98357a_dai_link, SND_SOC_INTEL_SOF_MAXIM_COMMON);
195 
196 MODULE_DESCRIPTION("ASoC Intel SOF Maxim helpers");
197 MODULE_LICENSE("GPL");
198