xref: /openbmc/linux/sound/soc/intel/avs/boards/ssm4567.c (revision 6f2bde9b)
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_SSM_CODEC_DAI	"ssm4567-hifi"
19 
20 static struct snd_soc_codec_conf card_codec_conf[] = {
21 	{
22 		.dlc = COMP_CODEC_CONF("i2c-INT343B:00"),
23 		.name_prefix = "Left",
24 	},
25 	{
26 		.dlc = COMP_CODEC_CONF("i2c-INT343B:01"),
27 		.name_prefix = "Right",
28 	},
29 };
30 
31 static const struct snd_kcontrol_new card_controls[] = {
32 	SOC_DAPM_PIN_SWITCH("Left Speaker"),
33 	SOC_DAPM_PIN_SWITCH("Right Speaker"),
34 };
35 
36 static const struct snd_soc_dapm_widget card_widgets[] = {
37 	SND_SOC_DAPM_SPK("Left Speaker", NULL),
38 	SND_SOC_DAPM_SPK("Right Speaker", NULL),
39 	SND_SOC_DAPM_SPK("DP1", NULL),
40 	SND_SOC_DAPM_SPK("DP2", NULL),
41 };
42 
43 static const struct snd_soc_dapm_route card_base_routes[] = {
44 	{"Left Speaker", NULL, "Left OUT"},
45 	{"Right Speaker", NULL, "Right OUT"},
46 };
47 
48 static int avs_ssm4567_codec_init(struct snd_soc_pcm_runtime *runtime)
49 {
50 	int ret;
51 
52 	/* Slot 1 for left */
53 	ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_codec(runtime, 0), 0x01, 0x01, 2, 48);
54 	if (ret < 0)
55 		return ret;
56 
57 	/* Slot 2 for right */
58 	ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_codec(runtime, 1), 0x02, 0x02, 2, 48);
59 	if (ret < 0)
60 		return ret;
61 
62 	return 0;
63 }
64 
65 static int
66 avs_ssm4567_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
67 {
68 	struct snd_interval *rate, *channels;
69 	struct snd_mask *fmt;
70 
71 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
72 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
73 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
74 
75 	/* The ADSP will convert the FE rate to 48k, stereo */
76 	rate->min = rate->max = 48000;
77 	channels->min = channels->max = 2;
78 
79 	/* set SSP0 to 24 bit */
80 	snd_mask_none(fmt);
81 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
82 	return 0;
83 }
84 
85 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
86 			       struct snd_soc_dai_link **dai_link)
87 {
88 	struct snd_soc_dai_link_component *platform;
89 	struct snd_soc_dai_link *dl;
90 
91 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
92 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
93 	if (!dl || !platform)
94 		return -ENOMEM;
95 
96 	platform->name = platform_name;
97 
98 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
99 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
100 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
101 	if (!dl->name || !dl->cpus || !dl->codecs)
102 		return -ENOMEM;
103 
104 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
105 	dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT343B:00");
106 	dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, "ssm4567-hifi");
107 	dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT343B:01");
108 	dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, "ssm4567-hifi");
109 	if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
110 	    !dl->codecs[1].name || !dl->codecs[1].dai_name)
111 		return -ENOMEM;
112 
113 	dl->num_cpus = 1;
114 	dl->num_codecs = 2;
115 	dl->platforms = platform;
116 	dl->num_platforms = 1;
117 	dl->id = 0;
118 	dl->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBS_CFS;
119 	dl->init = avs_ssm4567_codec_init;
120 	dl->be_hw_params_fixup = avs_ssm4567_be_fixup;
121 	dl->nonatomic = 1;
122 	dl->no_pcm = 1;
123 	dl->dpcm_capture = 1;
124 	dl->dpcm_playback = 1;
125 	dl->ignore_pmdown_time = 1;
126 
127 	*dai_link = dl;
128 
129 	return 0;
130 }
131 
132 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
133 				  struct snd_soc_dapm_route **routes, int *num_routes)
134 {
135 	struct snd_soc_dapm_route *dr;
136 	const int num_base = ARRAY_SIZE(card_base_routes);
137 	const int num_dr = num_base + 4;
138 	int idx;
139 
140 	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
141 	if (!dr)
142 		return -ENOMEM;
143 
144 	memcpy(dr, card_base_routes, num_base * sizeof(*dr));
145 
146 	idx = num_base;
147 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Left Playback");
148 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
149 	if (!dr[idx].sink || !dr[idx].source)
150 		return -ENOMEM;
151 
152 	idx++;
153 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Right Playback");
154 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
155 	if (!dr[idx].sink || !dr[idx].source)
156 		return -ENOMEM;
157 
158 	idx++;
159 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
160 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "Left Capture Sense");
161 	if (!dr[idx].sink || !dr[idx].source)
162 		return -ENOMEM;
163 
164 	idx++;
165 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
166 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "Right Capture Sense");
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_ssm4567_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_ssm4567-adi";
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 	card->disable_route_checks = true;
221 
222 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
223 	if (ret)
224 		return ret;
225 
226 	return devm_snd_soc_register_card(dev, card);
227 }
228 
229 static struct platform_driver avs_ssm4567_driver = {
230 	.probe = avs_ssm4567_probe,
231 	.driver = {
232 		.name = "avs_ssm4567",
233 		.pm = &snd_soc_pm_ops,
234 	},
235 };
236 
237 module_platform_driver(avs_ssm4567_driver)
238 
239 MODULE_LICENSE("GPL");
240 MODULE_ALIAS("platform:avs_ssm4567");
241