xref: /openbmc/linux/sound/soc/intel/avs/boards/ssm4567.c (revision 6c8c1406)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-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/core.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15 #include <sound/soc-acpi.h>
16 #include "../../../codecs/nau8825.h"
17 
18 #define SKL_NUVOTON_CODEC_DAI	"nau8825-hifi"
19 #define SKL_SSM_CODEC_DAI	"ssm4567-hifi"
20 
21 static struct snd_soc_codec_conf card_codec_conf[] = {
22 	{
23 		.dlc = COMP_CODEC_CONF("i2c-INT343B:00"),
24 		.name_prefix = "Left",
25 	},
26 	{
27 		.dlc = COMP_CODEC_CONF("i2c-INT343B:01"),
28 		.name_prefix = "Right",
29 	},
30 };
31 
32 static const struct snd_kcontrol_new card_controls[] = {
33 	SOC_DAPM_PIN_SWITCH("Left Speaker"),
34 	SOC_DAPM_PIN_SWITCH("Right Speaker"),
35 };
36 
37 static int
38 platform_clock_control(struct snd_soc_dapm_widget *w, struct snd_kcontrol *control, int event)
39 {
40 	struct snd_soc_dapm_context *dapm = w->dapm;
41 	struct snd_soc_card *card = dapm->card;
42 	struct snd_soc_dai *codec_dai;
43 	int ret;
44 
45 	codec_dai = snd_soc_card_get_codec_dai(card, SKL_NUVOTON_CODEC_DAI);
46 	if (!codec_dai) {
47 		dev_err(card->dev, "Codec dai not found\n");
48 		return -EINVAL;
49 	}
50 
51 	if (SND_SOC_DAPM_EVENT_ON(event)) {
52 		ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_MCLK, 24000000,
53 					     SND_SOC_CLOCK_IN);
54 		if (ret < 0)
55 			dev_err(card->dev, "set sysclk err = %d\n", ret);
56 	} else {
57 		ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_INTERNAL, 0, SND_SOC_CLOCK_IN);
58 		if (ret < 0)
59 			dev_err(card->dev, "set sysclk err = %d\n", ret);
60 	}
61 
62 	return ret;
63 }
64 
65 static const struct snd_soc_dapm_widget card_widgets[] = {
66 	SND_SOC_DAPM_SPK("Left Speaker", NULL),
67 	SND_SOC_DAPM_SPK("Right Speaker", NULL),
68 	SND_SOC_DAPM_SPK("DP1", NULL),
69 	SND_SOC_DAPM_SPK("DP2", NULL),
70 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, platform_clock_control,
71 			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
72 };
73 
74 static const struct snd_soc_dapm_route card_base_routes[] = {
75 	{"Left Speaker", NULL, "Left OUT"},
76 	{"Right Speaker", NULL, "Right OUT"},
77 };
78 
79 static int avs_ssm4567_codec_init(struct snd_soc_pcm_runtime *runtime)
80 {
81 	int ret;
82 
83 	/* Slot 1 for left */
84 	ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_codec(runtime, 0), 0x01, 0x01, 2, 48);
85 	if (ret < 0)
86 		return ret;
87 
88 	/* Slot 2 for right */
89 	ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_codec(runtime, 1), 0x02, 0x02, 2, 48);
90 	if (ret < 0)
91 		return ret;
92 
93 	return 0;
94 }
95 
96 static int
97 avs_ssm4567_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
98 {
99 	struct snd_interval *rate, *channels;
100 	struct snd_mask *fmt;
101 
102 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
103 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
104 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
105 
106 	/* The ADSP will covert the FE rate to 48k, stereo */
107 	rate->min = rate->max = 48000;
108 	channels->min = channels->max = 2;
109 
110 	/* set SSP0 to 24 bit */
111 	snd_mask_none(fmt);
112 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
113 	return 0;
114 }
115 
116 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
117 			       struct snd_soc_dai_link **dai_link)
118 {
119 	struct snd_soc_dai_link_component *platform;
120 	struct snd_soc_dai_link *dl;
121 
122 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
123 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
124 	if (!dl || !platform)
125 		return -ENOMEM;
126 
127 	platform->name = platform_name;
128 
129 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
130 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
131 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
132 	if (!dl->name || !dl->cpus || !dl->codecs)
133 		return -ENOMEM;
134 
135 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
136 	dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT343B:00");
137 	dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, "ssm4567-hifi");
138 	dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT343B:01");
139 	dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, "ssm4567-hifi");
140 	if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
141 	    !dl->codecs[1].name || !dl->codecs[1].dai_name)
142 		return -ENOMEM;
143 
144 	dl->num_cpus = 1;
145 	dl->num_codecs = 2;
146 	dl->platforms = platform;
147 	dl->num_platforms = 1;
148 	dl->id = 0;
149 	dl->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBS_CFS;
150 	dl->init = avs_ssm4567_codec_init;
151 	dl->be_hw_params_fixup = avs_ssm4567_be_fixup;
152 	dl->nonatomic = 1;
153 	dl->no_pcm = 1;
154 	dl->dpcm_capture = 1;
155 	dl->dpcm_playback = 1;
156 	dl->ignore_pmdown_time = 1;
157 
158 	*dai_link = dl;
159 
160 	return 0;
161 }
162 
163 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
164 				  struct snd_soc_dapm_route **routes, int *num_routes)
165 {
166 	struct snd_soc_dapm_route *dr;
167 	const int num_base = ARRAY_SIZE(card_base_routes);
168 	const int num_dr = num_base + 4;
169 	int idx;
170 
171 	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
172 	if (!dr)
173 		return -ENOMEM;
174 
175 	memcpy(dr, card_base_routes, num_base * sizeof(*dr));
176 
177 	idx = num_base;
178 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Left Playback");
179 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
180 	if (!dr[idx].sink || !dr[idx].source)
181 		return -ENOMEM;
182 
183 	idx++;
184 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Right Playback");
185 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
186 	if (!dr[idx].sink || !dr[idx].source)
187 		return -ENOMEM;
188 
189 	idx++;
190 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
191 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "Left Capture Sense");
192 	if (!dr[idx].sink || !dr[idx].source)
193 		return -ENOMEM;
194 
195 	idx++;
196 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
197 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "Right Capture Sense");
198 	if (!dr[idx].sink || !dr[idx].source)
199 		return -ENOMEM;
200 
201 	*routes = dr;
202 	*num_routes = num_dr;
203 
204 	return 0;
205 }
206 
207 static int avs_ssm4567_probe(struct platform_device *pdev)
208 {
209 	struct snd_soc_dapm_route *routes;
210 	struct snd_soc_dai_link *dai_link;
211 	struct snd_soc_acpi_mach *mach;
212 	struct snd_soc_card *card;
213 	struct device *dev = &pdev->dev;
214 	const char *pname;
215 	int num_routes, ssp_port, ret;
216 
217 	mach = dev_get_platdata(dev);
218 	pname = mach->mach_params.platform;
219 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
220 
221 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
222 	if (ret) {
223 		dev_err(dev, "Failed to create dai link: %d", ret);
224 		return ret;
225 	}
226 
227 	ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
228 	if (ret) {
229 		dev_err(dev, "Failed to create dapm routes: %d", ret);
230 		return ret;
231 	}
232 
233 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
234 	if (!card)
235 		return -ENOMEM;
236 
237 	card->name = "avs_ssm4567-adi";
238 	card->dev = dev;
239 	card->owner = THIS_MODULE;
240 	card->dai_link = dai_link;
241 	card->num_links = 1;
242 	card->codec_conf = card_codec_conf;
243 	card->num_configs = ARRAY_SIZE(card_codec_conf);
244 	card->controls = card_controls;
245 	card->num_controls = ARRAY_SIZE(card_controls);
246 	card->dapm_widgets = card_widgets;
247 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
248 	card->dapm_routes = routes;
249 	card->num_dapm_routes = num_routes;
250 	card->fully_routed = true;
251 	card->disable_route_checks = true;
252 
253 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
254 	if (ret)
255 		return ret;
256 
257 	return devm_snd_soc_register_card(dev, card);
258 }
259 
260 static struct platform_driver avs_ssm4567_driver = {
261 	.probe = avs_ssm4567_probe,
262 	.driver = {
263 		.name = "avs_ssm4567",
264 		.pm = &snd_soc_pm_ops,
265 	},
266 };
267 
268 module_platform_driver(avs_ssm4567_driver)
269 
270 MODULE_LICENSE("GPL");
271 MODULE_ALIAS("platform:avs_ssm4567");
272