xref: /openbmc/linux/sound/soc/intel/avs/boards/max98927.c (revision ce6cc6f7)
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 MAX98927_DEV0_NAME	"i2c-MX98927:00"
17 #define MAX98927_DEV1_NAME	"i2c-MX98927:01"
18 #define MAX98927_CODEC_NAME	"max98927-aif1"
19 
20 static struct snd_soc_codec_conf card_codec_conf[] = {
21 	{
22 		.dlc = COMP_CODEC_CONF(MAX98927_DEV0_NAME),
23 		.name_prefix = "Right",
24 	},
25 	{
26 		.dlc = COMP_CODEC_CONF(MAX98927_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_max98927_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 convert 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_max98927_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 = 0;
72 	int i;
73 
74 	for_each_rtd_codec_dais(runtime, i, codec_dai) {
75 		if (!strcmp(codec_dai->component->name, MAX98927_DEV0_NAME))
76 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x30, 3, 8, 16);
77 		else if (!strcmp(codec_dai->component->name, MAX98927_DEV1_NAME))
78 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC0, 3, 8, 16);
79 
80 		if (ret < 0) {
81 			dev_err(runtime->dev, "hw_params for %s failed: %d\n",
82 				codec_dai->component->name, ret);
83 			return ret;
84 		}
85 	}
86 
87 	return 0;
88 }
89 
90 static const struct snd_soc_ops avs_max98927_ops = {
91 	.hw_params = avs_max98927_hw_params,
92 };
93 
94 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
95 			       struct snd_soc_dai_link **dai_link)
96 {
97 	struct snd_soc_dai_link_component *platform;
98 	struct snd_soc_dai_link *dl;
99 
100 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
101 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
102 	if (!dl || !platform)
103 		return -ENOMEM;
104 
105 	platform->name = platform_name;
106 
107 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
108 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
109 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
110 	if (!dl->name || !dl->cpus || !dl->codecs)
111 		return -ENOMEM;
112 
113 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
114 	dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_DEV0_NAME);
115 	dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_CODEC_NAME);
116 	dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_DEV1_NAME);
117 	dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, MAX98927_CODEC_NAME);
118 	if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
119 	    !dl->codecs[1].name || !dl->codecs[1].dai_name)
120 		return -ENOMEM;
121 
122 	dl->num_cpus = 1;
123 	dl->num_codecs = 2;
124 	dl->platforms = platform;
125 	dl->num_platforms = 1;
126 	dl->id = 0;
127 	dl->dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
128 	dl->be_hw_params_fixup = avs_max98927_be_fixup;
129 	dl->nonatomic = 1;
130 	dl->no_pcm = 1;
131 	dl->dpcm_capture = 1;
132 	dl->dpcm_playback = 1;
133 	dl->ignore_pmdown_time = 1;
134 	dl->ops = &avs_max98927_ops;
135 
136 	*dai_link = dl;
137 
138 	return 0;
139 }
140 
141 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
142 				  struct snd_soc_dapm_route **routes, int *num_routes)
143 {
144 	struct snd_soc_dapm_route *dr;
145 	const int num_base = ARRAY_SIZE(card_base_routes);
146 	const int num_dr = num_base + 2;
147 	int idx;
148 
149 	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
150 	if (!dr)
151 		return -ENOMEM;
152 
153 	memcpy(dr, card_base_routes, num_base * sizeof(*dr));
154 
155 	idx = num_base;
156 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Left HiFi Playback");
157 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
158 	if (!dr[idx].sink || !dr[idx].source)
159 		return -ENOMEM;
160 
161 	idx++;
162 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Right HiFi Playback");
163 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
164 	if (!dr[idx].sink || !dr[idx].source)
165 		return -ENOMEM;
166 
167 	*routes = dr;
168 	*num_routes = num_dr;
169 
170 	return 0;
171 }
172 
173 static int avs_max98927_probe(struct platform_device *pdev)
174 {
175 	struct snd_soc_dapm_route *routes;
176 	struct snd_soc_dai_link *dai_link;
177 	struct snd_soc_acpi_mach *mach;
178 	struct snd_soc_card *card;
179 	struct device *dev = &pdev->dev;
180 	const char *pname;
181 	int num_routes, ssp_port, ret;
182 
183 	mach = dev_get_platdata(dev);
184 	pname = mach->mach_params.platform;
185 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
186 
187 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
188 	if (ret) {
189 		dev_err(dev, "Failed to create dai link: %d", ret);
190 		return ret;
191 	}
192 
193 	ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
194 	if (ret) {
195 		dev_err(dev, "Failed to create dapm routes: %d", ret);
196 		return ret;
197 	}
198 
199 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
200 	if (!card)
201 		return -ENOMEM;
202 
203 	card->name = "avs_max98927";
204 	card->dev = dev;
205 	card->owner = THIS_MODULE;
206 	card->dai_link = dai_link;
207 	card->num_links = 1;
208 	card->codec_conf = card_codec_conf;
209 	card->num_configs = ARRAY_SIZE(card_codec_conf);
210 	card->controls = card_controls;
211 	card->num_controls = ARRAY_SIZE(card_controls);
212 	card->dapm_widgets = card_widgets;
213 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
214 	card->dapm_routes = routes;
215 	card->num_dapm_routes = num_routes;
216 	card->fully_routed = true;
217 
218 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
219 	if (ret)
220 		return ret;
221 
222 	return devm_snd_soc_register_card(dev, card);
223 }
224 
225 static struct platform_driver avs_max98927_driver = {
226 	.probe = avs_max98927_probe,
227 	.driver = {
228 		.name = "avs_max98927",
229 		.pm = &snd_soc_pm_ops,
230 	},
231 };
232 
233 module_platform_driver(avs_max98927_driver)
234 
235 MODULE_LICENSE("GPL");
236 MODULE_ALIAS("platform:avs_max98927");
237