xref: /linux/sound/soc/intel/avs/boards/rt5663.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2022-2023 Intel Corporation
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8 
9 #include <linux/clk.h>
10 #include <linux/input.h>
11 #include <linux/module.h>
12 #include <sound/jack.h>
13 #include <sound/pcm.h>
14 #include <sound/pcm_params.h>
15 #include <sound/soc.h>
16 #include <sound/soc-acpi.h>
17 #include "../../../codecs/rt5663.h"
18 #include "../utils.h"
19 
20 #define RT5663_CODEC_DAI	"rt5663-aif"
21 
22 struct rt5663_private {
23 	struct snd_soc_jack jack;
24 };
25 
26 static const struct snd_kcontrol_new card_controls[] = {
27 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
28 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
29 };
30 
31 static const struct snd_soc_dapm_widget card_widgets[] = {
32 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
33 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
34 };
35 
36 static const struct snd_soc_dapm_route card_routes[] = {
37 	/* HP jack connectors */
38 	{ "Headphone Jack", NULL, "HPOL" },
39 	{ "Headphone Jack", NULL, "HPOR" },
40 
41 	/* Mic jacks */
42 	{ "IN1P", NULL, "Headset Mic" },
43 	{ "IN1N", NULL, "Headset Mic" },
44 };
45 
46 static const struct snd_soc_jack_pin card_headset_pins[] = {
47 	{
48 		.pin = "Headphone Jack",
49 		.mask = SND_JACK_HEADPHONE,
50 	},
51 	{
52 		.pin = "Headset Mic",
53 		.mask = SND_JACK_MICROPHONE,
54 	},
55 };
56 
57 static int avs_rt5663_codec_init(struct snd_soc_pcm_runtime *runtime)
58 {
59 	struct snd_soc_card *card = runtime->card;
60 	struct rt5663_private *priv = snd_soc_card_get_drvdata(card);
61 	struct snd_soc_jack_pin *pins;
62 	struct snd_soc_jack *jack;
63 	int num_pins, ret;
64 
65 	jack = &priv->jack;
66 	num_pins = ARRAY_SIZE(card_headset_pins);
67 
68 	pins = devm_kmemdup(card->dev, card_headset_pins, sizeof(*pins) * num_pins, GFP_KERNEL);
69 	if (!pins)
70 		return -ENOMEM;
71 
72 	ret = snd_soc_card_jack_new_pins(card, "Headset Jack", SND_JACK_HEADSET | SND_JACK_BTN_0 |
73 					 SND_JACK_BTN_1 | SND_JACK_BTN_2 | SND_JACK_BTN_3, jack,
74 					 pins, num_pins);
75 	if (ret)
76 		return ret;
77 
78 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
79 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
80 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
81 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
82 
83 	snd_soc_component_set_jack(snd_soc_rtd_to_codec(runtime, 0)->component, jack, NULL);
84 
85 	return 0;
86 }
87 
88 static void avs_rt5663_codec_exit(struct snd_soc_pcm_runtime *runtime)
89 {
90 	snd_soc_component_set_jack(snd_soc_rtd_to_codec(runtime, 0)->component, NULL, NULL);
91 }
92 
93 static int
94 avs_rt5663_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params)
95 {
96 	struct snd_interval *rate, *channels;
97 	struct snd_mask *fmt;
98 
99 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
100 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
101 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
102 
103 	/* The ADSP will convert the FE rate to 48k, stereo */
104 	rate->min = rate->max = 48000;
105 	channels->min = channels->max = 2;
106 
107 	/* set SSPN to 24 bit */
108 	snd_mask_none(fmt);
109 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
110 
111 	return 0;
112 }
113 
114 static int avs_rt5663_hw_params(struct snd_pcm_substream *substream,
115 				struct snd_pcm_hw_params *params)
116 {
117 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
118 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
119 	int ret;
120 
121 	/* use ASRC for internal clocks, as PLL rate isn't multiple of BCLK */
122 	rt5663_sel_asrc_clk_src(codec_dai->component,
123 				RT5663_DA_STEREO_FILTER | RT5663_AD_STEREO_FILTER,
124 				RT5663_CLK_SEL_I2S1_ASRC);
125 
126 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5663_SCLK_S_MCLK, 24576000, SND_SOC_CLOCK_IN);
127 
128 	return ret;
129 }
130 
131 static const struct snd_soc_ops avs_rt5663_ops = {
132 	.hw_params = avs_rt5663_hw_params,
133 };
134 
135 
136 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
137 			       int tdm_slot, struct snd_soc_dai_link **dai_link)
138 {
139 	struct snd_soc_dai_link_component *platform;
140 	struct snd_soc_dai_link *dl;
141 
142 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
143 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
144 	if (!dl || !platform)
145 		return -ENOMEM;
146 
147 	platform->name = platform_name;
148 
149 	dl->name = devm_kasprintf(dev, GFP_KERNEL,
150 				  AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
151 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
152 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
153 	if (!dl->name || !dl->cpus || !dl->codecs)
154 		return -ENOMEM;
155 
156 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
157 					    AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
158 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-10EC5663:00");
159 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, RT5663_CODEC_DAI);
160 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
161 		return -ENOMEM;
162 
163 	dl->num_cpus = 1;
164 	dl->num_codecs = 1;
165 	dl->platforms = platform;
166 	dl->num_platforms = 1;
167 	dl->id = 0;
168 	dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
169 	dl->init = avs_rt5663_codec_init;
170 	dl->exit = avs_rt5663_codec_exit;
171 	dl->be_hw_params_fixup = avs_rt5663_be_fixup;
172 	dl->nonatomic = 1;
173 	dl->no_pcm = 1;
174 	dl->dpcm_capture = 1;
175 	dl->dpcm_playback = 1;
176 	dl->ops = &avs_rt5663_ops;
177 
178 	*dai_link = dl;
179 
180 	return 0;
181 }
182 
183 static int avs_card_suspend_pre(struct snd_soc_card *card)
184 {
185 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT5663_CODEC_DAI);
186 
187 	return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
188 }
189 
190 static int avs_card_resume_post(struct snd_soc_card *card)
191 {
192 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT5663_CODEC_DAI);
193 	struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
194 
195 	return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
196 }
197 
198 static int avs_rt5663_probe(struct platform_device *pdev)
199 {
200 	struct snd_soc_dai_link *dai_link;
201 	struct snd_soc_acpi_mach *mach;
202 	struct snd_soc_card *card;
203 	struct rt5663_private *priv;
204 	struct device *dev = &pdev->dev;
205 	const char *pname;
206 	int ssp_port, tdm_slot, ret;
207 
208 	mach = dev_get_platdata(dev);
209 	pname = mach->mach_params.platform;
210 
211 	ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
212 	if (ret)
213 		return ret;
214 
215 	ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
216 	if (ret) {
217 		dev_err(dev, "Failed to create dai link: %d", ret);
218 		return ret;
219 	}
220 
221 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
222 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
223 	if (!priv || !card)
224 		return -ENOMEM;
225 
226 	card->name = "avs_rt5663";
227 	card->dev = dev;
228 	card->owner = THIS_MODULE;
229 	card->suspend_pre = avs_card_suspend_pre;
230 	card->resume_post = avs_card_resume_post;
231 	card->dai_link = dai_link;
232 	card->num_links = 1;
233 	card->controls = card_controls;
234 	card->num_controls = ARRAY_SIZE(card_controls);
235 	card->dapm_widgets = card_widgets;
236 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
237 	card->dapm_routes = card_routes;
238 	card->num_dapm_routes = ARRAY_SIZE(card_routes);
239 	card->fully_routed = true;
240 	snd_soc_card_set_drvdata(card, priv);
241 
242 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
243 	if (ret)
244 		return ret;
245 
246 	return devm_snd_soc_register_card(dev, card);
247 }
248 
249 static const struct platform_device_id avs_rt5663_driver_ids[] = {
250 	{
251 		.name = "avs_rt5663",
252 	},
253 	{},
254 };
255 MODULE_DEVICE_TABLE(platform, avs_rt5663_driver_ids);
256 
257 static struct platform_driver avs_rt5663_driver = {
258 	.probe = avs_rt5663_probe,
259 	.driver = {
260 		.name = "avs_rt5663",
261 		.pm = &snd_soc_pm_ops,
262 	},
263 	.id_table = avs_rt5663_driver_ids,
264 };
265 
266 module_platform_driver(avs_rt5663_driver);
267 
268 MODULE_DESCRIPTION("Intel rt5663 machine driver");
269 MODULE_LICENSE("GPL");
270