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