xref: /linux/sound/soc/intel/avs/boards/rt5663.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
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->ops = &avs_rt5663_ops;
175 
176 	*dai_link = dl;
177 
178 	return 0;
179 }
180 
181 static int avs_card_suspend_pre(struct snd_soc_card *card)
182 {
183 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT5663_CODEC_DAI);
184 
185 	return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
186 }
187 
188 static int avs_card_resume_post(struct snd_soc_card *card)
189 {
190 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT5663_CODEC_DAI);
191 	struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
192 
193 	return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
194 }
195 
196 static int avs_rt5663_probe(struct platform_device *pdev)
197 {
198 	struct snd_soc_dai_link *dai_link;
199 	struct snd_soc_acpi_mach *mach;
200 	struct snd_soc_card *card;
201 	struct rt5663_private *priv;
202 	struct device *dev = &pdev->dev;
203 	const char *pname;
204 	int ssp_port, tdm_slot, ret;
205 
206 	mach = dev_get_platdata(dev);
207 	pname = mach->mach_params.platform;
208 
209 	ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
210 	if (ret)
211 		return ret;
212 
213 	ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
214 	if (ret) {
215 		dev_err(dev, "Failed to create dai link: %d", ret);
216 		return ret;
217 	}
218 
219 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
220 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
221 	if (!priv || !card)
222 		return -ENOMEM;
223 
224 	card->name = "avs_rt5663";
225 	card->dev = dev;
226 	card->owner = THIS_MODULE;
227 	card->suspend_pre = avs_card_suspend_pre;
228 	card->resume_post = avs_card_resume_post;
229 	card->dai_link = dai_link;
230 	card->num_links = 1;
231 	card->controls = card_controls;
232 	card->num_controls = ARRAY_SIZE(card_controls);
233 	card->dapm_widgets = card_widgets;
234 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
235 	card->dapm_routes = card_routes;
236 	card->num_dapm_routes = ARRAY_SIZE(card_routes);
237 	card->fully_routed = true;
238 	snd_soc_card_set_drvdata(card, priv);
239 
240 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
241 	if (ret)
242 		return ret;
243 
244 	return devm_snd_soc_register_card(dev, card);
245 }
246 
247 static const struct platform_device_id avs_rt5663_driver_ids[] = {
248 	{
249 		.name = "avs_rt5663",
250 	},
251 	{},
252 };
253 MODULE_DEVICE_TABLE(platform, avs_rt5663_driver_ids);
254 
255 static struct platform_driver avs_rt5663_driver = {
256 	.probe = avs_rt5663_probe,
257 	.driver = {
258 		.name = "avs_rt5663",
259 		.pm = &snd_soc_pm_ops,
260 	},
261 	.id_table = avs_rt5663_driver_ids,
262 };
263 
264 module_platform_driver(avs_rt5663_driver);
265 
266 MODULE_DESCRIPTION("Intel rt5663 machine driver");
267 MODULE_LICENSE("GPL");
268