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