xref: /linux/sound/soc/intel/avs/boards/rt274.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
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 <sound/jack.h>
11 #include <sound/pcm.h>
12 #include <sound/pcm_params.h>
13 #include <sound/soc.h>
14 #include <sound/soc-acpi.h>
15 #include "../../../codecs/rt274.h"
16 #include "../utils.h"
17 
18 #define AVS_RT274_FREQ_OUT	24000000
19 #define AVS_RT274_BE_FIXUP_RATE	48000
20 #define RT274_CODEC_DAI		"rt274-aif1"
21 
22 static const struct snd_kcontrol_new card_controls[] = {
23 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
24 	SOC_DAPM_PIN_SWITCH("Mic Jack"),
25 };
26 
27 static int
28 avs_rt274_clock_control(struct snd_soc_dapm_widget *w, struct snd_kcontrol *control, int event)
29 {
30 	struct snd_soc_dapm_context *dapm = w->dapm;
31 	struct snd_soc_card *card = dapm->card;
32 	struct snd_soc_dai *codec_dai;
33 	int ret;
34 
35 	codec_dai = snd_soc_card_get_codec_dai(card, RT274_CODEC_DAI);
36 	if (!codec_dai)
37 		return -EINVAL;
38 
39 	/* Codec needs clock for Jack detection and button press */
40 	ret = snd_soc_dai_set_sysclk(codec_dai, RT274_SCLK_S_PLL2, AVS_RT274_FREQ_OUT,
41 				     SND_SOC_CLOCK_IN);
42 	if (ret < 0) {
43 		dev_err(codec_dai->dev, "set codec sysclk failed: %d\n", ret);
44 		return ret;
45 	}
46 
47 	if (SND_SOC_DAPM_EVENT_ON(event)) {
48 		int ratio = 100;
49 
50 		snd_soc_dai_set_bclk_ratio(codec_dai, ratio);
51 
52 		ret = snd_soc_dai_set_pll(codec_dai, 0, RT274_PLL2_S_BCLK,
53 					  AVS_RT274_BE_FIXUP_RATE * ratio, AVS_RT274_FREQ_OUT);
54 		if (ret) {
55 			dev_err(codec_dai->dev, "failed to enable PLL2: %d\n", ret);
56 			return ret;
57 		}
58 	}
59 
60 	return 0;
61 }
62 
63 static const struct snd_soc_dapm_widget card_widgets[] = {
64 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
65 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
66 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, avs_rt274_clock_control,
67 			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
68 };
69 
70 static const struct snd_soc_dapm_route card_base_routes[] = {
71 	{"Headphone Jack", NULL, "HPO Pin"},
72 	{"MIC", NULL, "Mic Jack"},
73 
74 	{"Headphone Jack", NULL, "Platform Clock"},
75 	{"MIC", NULL, "Platform Clock"},
76 };
77 
78 static struct snd_soc_jack_pin card_headset_pins[] = {
79 	{
80 		.pin = "Headphone Jack",
81 		.mask = SND_JACK_HEADPHONE,
82 	},
83 	{
84 		.pin = "Mic Jack",
85 		.mask = SND_JACK_MICROPHONE,
86 	},
87 };
88 
89 static int avs_rt274_codec_init(struct snd_soc_pcm_runtime *runtime)
90 {
91 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
92 	struct snd_soc_component *component = codec_dai->component;
93 	struct snd_soc_jack_pin *pins;
94 	struct snd_soc_jack *jack;
95 	struct snd_soc_card *card = runtime->card;
96 	int num_pins, ret;
97 
98 	jack = snd_soc_card_get_drvdata(card);
99 	num_pins = ARRAY_SIZE(card_headset_pins);
100 
101 	pins = devm_kmemdup(card->dev, card_headset_pins, sizeof(*pins) * num_pins, GFP_KERNEL);
102 	if (!pins)
103 		return -ENOMEM;
104 
105 	ret = snd_soc_card_jack_new_pins(card, "Headset", SND_JACK_HEADSET, jack, pins, num_pins);
106 	if (ret)
107 		return ret;
108 
109 	snd_soc_component_set_jack(component, jack, NULL);
110 
111 	/* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
112 	ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
113 	if (ret < 0) {
114 		dev_err(card->dev, "can't set codec pcm format %d\n", ret);
115 		return ret;
116 	}
117 
118 	card->dapm.idle_bias_off = true;
119 
120 	return 0;
121 }
122 
123 static void avs_rt274_codec_exit(struct snd_soc_pcm_runtime *rtd)
124 {
125 	snd_soc_component_set_jack(snd_soc_rtd_to_codec(rtd, 0)->component, NULL, NULL);
126 }
127 
128 static int avs_rt274_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params)
129 {
130 	struct snd_interval *rate, *channels;
131 	struct snd_mask *fmt;
132 
133 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
134 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
135 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
136 
137 	/* The ADSP will convert the FE rate to 48k, stereo */
138 	rate->min = rate->max = AVS_RT274_BE_FIXUP_RATE;
139 	channels->min = channels->max = 2;
140 
141 	/* set SSPN to 24 bit */
142 	snd_mask_none(fmt);
143 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
144 
145 	return 0;
146 }
147 
148 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
149 			       int tdm_slot, struct snd_soc_dai_link **dai_link)
150 {
151 	struct snd_soc_dai_link_component *platform;
152 	struct snd_soc_dai_link *dl;
153 
154 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
155 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
156 	if (!dl || !platform)
157 		return -ENOMEM;
158 
159 	platform->name = platform_name;
160 
161 	dl->name = devm_kasprintf(dev, GFP_KERNEL,
162 				  AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
163 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
164 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
165 	if (!dl->name || !dl->cpus || !dl->codecs)
166 		return -ENOMEM;
167 
168 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
169 					    AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
170 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT34C2:00");
171 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, RT274_CODEC_DAI);
172 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
173 		return -ENOMEM;
174 
175 	dl->num_cpus = 1;
176 	dl->num_codecs = 1;
177 	dl->platforms = platform;
178 	dl->num_platforms = 1;
179 	dl->id = 0;
180 	dl->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
181 	dl->init = avs_rt274_codec_init;
182 	dl->exit = avs_rt274_codec_exit;
183 	dl->be_hw_params_fixup = avs_rt274_be_fixup;
184 	dl->nonatomic = 1;
185 	dl->no_pcm = 1;
186 	dl->dpcm_capture = 1;
187 	dl->dpcm_playback = 1;
188 
189 	*dai_link = dl;
190 
191 	return 0;
192 }
193 
194 static int avs_card_suspend_pre(struct snd_soc_card *card)
195 {
196 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT274_CODEC_DAI);
197 
198 	return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
199 }
200 
201 static int avs_card_resume_post(struct snd_soc_card *card)
202 {
203 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT274_CODEC_DAI);
204 	struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
205 
206 	return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
207 }
208 
209 static int avs_rt274_probe(struct platform_device *pdev)
210 {
211 	struct snd_soc_dai_link *dai_link;
212 	struct snd_soc_acpi_mach *mach;
213 	struct snd_soc_card *card;
214 	struct snd_soc_jack *jack;
215 	struct device *dev = &pdev->dev;
216 	const char *pname;
217 	int ssp_port, tdm_slot, ret;
218 
219 	mach = dev_get_platdata(dev);
220 	pname = mach->mach_params.platform;
221 
222 	ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
223 	if (ret)
224 		return ret;
225 
226 	ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
227 	if (ret) {
228 		dev_err(dev, "Failed to create dai link: %d", ret);
229 		return ret;
230 	}
231 
232 	jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
233 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
234 	if (!jack || !card)
235 		return -ENOMEM;
236 
237 	card->name = "avs_rt274";
238 	card->dev = dev;
239 	card->owner = THIS_MODULE;
240 	card->suspend_pre = avs_card_suspend_pre;
241 	card->resume_post = avs_card_resume_post;
242 	card->dai_link = dai_link;
243 	card->num_links = 1;
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 = card_base_routes;
249 	card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
250 	card->fully_routed = true;
251 	snd_soc_card_set_drvdata(card, jack);
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 const struct platform_device_id avs_rt274_driver_ids[] = {
261 	{
262 		.name = "avs_rt274",
263 	},
264 	{},
265 };
266 MODULE_DEVICE_TABLE(platform, avs_rt274_driver_ids);
267 
268 static struct platform_driver avs_rt274_driver = {
269 	.probe = avs_rt274_probe,
270 	.driver = {
271 		.name = "avs_rt274",
272 		.pm = &snd_soc_pm_ops,
273 	},
274 	.id_table = avs_rt274_driver_ids,
275 };
276 
277 module_platform_driver(avs_rt274_driver);
278 
279 MODULE_LICENSE("GPL");
280