xref: /openbmc/linux/sound/soc/intel/avs/boards/max98373.c (revision 7cc39531)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2022 Intel Corporation. All rights reserved.
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8 
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc.h>
13 #include <sound/soc-acpi.h>
14 #include <sound/soc-dapm.h>
15 
16 #define MAX98373_DEV0_NAME	"i2c-MX98373:00"
17 #define MAX98373_DEV1_NAME	"i2c-MX98373:01"
18 #define MAX98373_CODEC_NAME	"max98373-aif1"
19 
20 static struct snd_soc_codec_conf card_codec_conf[] = {
21 	{
22 		.dlc = COMP_CODEC_CONF(MAX98373_DEV0_NAME),
23 		.name_prefix = "Right",
24 	},
25 	{
26 		.dlc = COMP_CODEC_CONF(MAX98373_DEV1_NAME),
27 		.name_prefix = "Left",
28 	},
29 };
30 
31 static const struct snd_kcontrol_new card_controls[] = {
32 	SOC_DAPM_PIN_SWITCH("Left Spk"),
33 	SOC_DAPM_PIN_SWITCH("Right Spk"),
34 };
35 
36 static const struct snd_soc_dapm_widget card_widgets[] = {
37 	SND_SOC_DAPM_SPK("Left Spk", NULL),
38 	SND_SOC_DAPM_SPK("Right Spk", NULL),
39 };
40 
41 static const struct snd_soc_dapm_route card_base_routes[] = {
42 	{ "Left Spk", NULL, "Left BE_OUT" },
43 	{ "Right Spk", NULL, "Right BE_OUT" },
44 };
45 
46 static int
47 avs_max98373_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
48 {
49 	struct snd_interval *rate, *channels;
50 	struct snd_mask *fmt;
51 
52 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
53 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
54 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
55 
56 	/* The ADSP will covert the FE rate to 48k, stereo */
57 	rate->min = rate->max = 48000;
58 	channels->min = channels->max = 2;
59 
60 	/* set SSP0 to 16 bit */
61 	snd_mask_none(fmt);
62 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
63 	return 0;
64 }
65 
66 static int avs_max98373_hw_params(struct snd_pcm_substream *substream,
67 				  struct snd_pcm_hw_params *params)
68 {
69 	struct snd_soc_pcm_runtime *runtime = asoc_substream_to_rtd(substream);
70 	struct snd_soc_dai *codec_dai;
71 	int ret, i;
72 
73 	for_each_rtd_codec_dais(runtime, i, codec_dai) {
74 		if (!strcmp(codec_dai->component->name, MAX98373_DEV0_NAME)) {
75 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x30, 3, 8, 16);
76 			if (ret < 0) {
77 				dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret);
78 				return ret;
79 			}
80 		}
81 		if (!strcmp(codec_dai->component->name, MAX98373_DEV1_NAME)) {
82 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC0, 3, 8, 16);
83 			if (ret < 0) {
84 				dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret);
85 				return ret;
86 			}
87 		}
88 	}
89 
90 	return 0;
91 }
92 
93 static const struct snd_soc_ops avs_max98373_ops = {
94 	.hw_params = avs_max98373_hw_params,
95 };
96 
97 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
98 			       struct snd_soc_dai_link **dai_link)
99 {
100 	struct snd_soc_dai_link_component *platform;
101 	struct snd_soc_dai_link *dl;
102 
103 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
104 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
105 	if (!dl || !platform)
106 		return -ENOMEM;
107 
108 	platform->name = platform_name;
109 
110 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
111 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
112 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
113 	if (!dl->name || !dl->cpus || !dl->codecs)
114 		return -ENOMEM;
115 
116 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
117 	dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_DEV0_NAME);
118 	dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_CODEC_NAME);
119 	dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_DEV1_NAME);
120 	dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98373_CODEC_NAME);
121 	if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
122 	    !dl->codecs[1].name || !dl->codecs[1].dai_name)
123 		return -ENOMEM;
124 
125 	dl->num_cpus = 1;
126 	dl->num_codecs = 2;
127 	dl->platforms = platform;
128 	dl->num_platforms = 1;
129 	dl->id = 0;
130 	dl->dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
131 	dl->be_hw_params_fixup = avs_max98373_be_fixup;
132 	dl->nonatomic = 1;
133 	dl->no_pcm = 1;
134 	dl->dpcm_capture = 1;
135 	dl->dpcm_playback = 1;
136 	dl->ignore_pmdown_time = 1;
137 	dl->ops = &avs_max98373_ops;
138 
139 	*dai_link = dl;
140 
141 	return 0;
142 }
143 
144 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
145 				  struct snd_soc_dapm_route **routes, int *num_routes)
146 {
147 	struct snd_soc_dapm_route *dr;
148 	const int num_base = ARRAY_SIZE(card_base_routes);
149 	const int num_dr = num_base + 2;
150 	int idx;
151 
152 	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
153 	if (!dr)
154 		return -ENOMEM;
155 
156 	memcpy(dr, card_base_routes, num_base * sizeof(*dr));
157 
158 	idx = num_base;
159 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Left HiFi Playback");
160 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
161 	if (!dr[idx].sink || !dr[idx].source)
162 		return -ENOMEM;
163 
164 	idx++;
165 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Right HiFi Playback");
166 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
167 	if (!dr[idx].sink || !dr[idx].source)
168 		return -ENOMEM;
169 
170 	*routes = dr;
171 	*num_routes = num_dr;
172 
173 	return 0;
174 }
175 
176 static int avs_max98373_probe(struct platform_device *pdev)
177 {
178 	struct snd_soc_dapm_route *routes;
179 	struct snd_soc_dai_link *dai_link;
180 	struct snd_soc_acpi_mach *mach;
181 	struct snd_soc_card *card;
182 	struct device *dev = &pdev->dev;
183 	const char *pname;
184 	int num_routes, ssp_port, ret;
185 
186 	mach = dev_get_platdata(dev);
187 	pname = mach->mach_params.platform;
188 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
189 
190 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
191 	if (ret) {
192 		dev_err(dev, "Failed to create dai link: %d", ret);
193 		return ret;
194 	}
195 
196 	ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
197 	if (ret) {
198 		dev_err(dev, "Failed to create dapm routes: %d", ret);
199 		return ret;
200 	}
201 
202 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
203 	if (!card)
204 		return -ENOMEM;
205 
206 	card->name = "avs_max98373";
207 	card->dev = dev;
208 	card->owner = THIS_MODULE;
209 	card->dai_link = dai_link;
210 	card->num_links = 1;
211 	card->codec_conf = card_codec_conf;
212 	card->num_configs = ARRAY_SIZE(card_codec_conf);
213 	card->controls = card_controls;
214 	card->num_controls = ARRAY_SIZE(card_controls);
215 	card->dapm_widgets = card_widgets;
216 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
217 	card->dapm_routes = routes;
218 	card->num_dapm_routes = num_routes;
219 	card->fully_routed = true;
220 
221 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
222 	if (ret)
223 		return ret;
224 
225 	return devm_snd_soc_register_card(dev, card);
226 }
227 
228 static struct platform_driver avs_max98373_driver = {
229 	.probe = avs_max98373_probe,
230 	.driver = {
231 		.name = "avs_max98373",
232 		.pm = &snd_soc_pm_ops,
233 	},
234 };
235 
236 module_platform_driver(avs_max98373_driver)
237 
238 MODULE_LICENSE("GPL");
239 MODULE_ALIAS("platform:avs_max98373");
240