xref: /linux/sound/soc/intel/avs/boards/rt286.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
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/rt286.h"
16 #include "../utils.h"
17 
18 #define RT286_CODEC_DAI		"rt286-aif1"
19 
20 static const struct snd_kcontrol_new card_controls[] = {
21 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
22 	SOC_DAPM_PIN_SWITCH("Mic Jack"),
23 	SOC_DAPM_PIN_SWITCH("Speaker"),
24 };
25 
26 static const struct snd_soc_dapm_widget card_widgets[] = {
27 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
28 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
29 	SND_SOC_DAPM_SPK("Speaker", NULL),
30 };
31 
32 static const struct snd_soc_dapm_route card_base_routes[] = {
33 	/* HP jack connectors - unknown if we have jack detect */
34 	{"Headphone Jack", NULL, "HPO Pin"},
35 	{"MIC1", NULL, "Mic Jack"},
36 
37 	{"Speaker", NULL, "SPOR"},
38 	{"Speaker", NULL, "SPOL"},
39 };
40 
41 static struct snd_soc_jack_pin card_headset_pins[] = {
42 	{
43 		.pin = "Headphone Jack",
44 		.mask = SND_JACK_HEADPHONE,
45 	},
46 	{
47 		.pin = "Mic Jack",
48 		.mask = SND_JACK_MICROPHONE,
49 	},
50 };
51 
52 static int avs_rt286_codec_init(struct snd_soc_pcm_runtime *runtime)
53 {
54 	struct snd_soc_card *card = runtime->card;
55 	struct snd_soc_jack_pin *pins;
56 	struct snd_soc_jack *jack;
57 	int num_pins, ret;
58 
59 	jack = snd_soc_card_get_drvdata(card);
60 	num_pins = ARRAY_SIZE(card_headset_pins);
61 
62 	pins = devm_kmemdup(card->dev, card_headset_pins, sizeof(*pins) * num_pins, GFP_KERNEL);
63 	if (!pins)
64 		return -ENOMEM;
65 
66 	ret = snd_soc_card_jack_new_pins(card, "Headset", SND_JACK_HEADSET | SND_JACK_BTN_0, jack,
67 					 pins, num_pins);
68 	if (ret)
69 		return ret;
70 
71 	return snd_soc_component_set_jack(snd_soc_rtd_to_codec(runtime, 0)->component, jack, NULL);
72 }
73 
74 static void avs_rt286_codec_exit(struct snd_soc_pcm_runtime *rtd)
75 {
76 	snd_soc_component_set_jack(snd_soc_rtd_to_codec(rtd, 0)->component, NULL, NULL);
77 }
78 
79 static int avs_rt286_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params)
80 {
81 	struct snd_interval *rate, *channels;
82 	struct snd_mask *fmt;
83 
84 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
85 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
86 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
87 
88 	/* The ADSP will convert the FE rate to 48k, stereo */
89 	rate->min = rate->max = 48000;
90 	channels->min = channels->max = 2;
91 
92 	/* set SSP0 to 24 bit */
93 	snd_mask_none(fmt);
94 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
95 
96 	return 0;
97 }
98 
99 static int
100 avs_rt286_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
101 {
102 	struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream);
103 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
104 	int ret;
105 
106 	ret = snd_soc_dai_set_sysclk(codec_dai, RT286_SCLK_S_PLL, 24000000, SND_SOC_CLOCK_IN);
107 	if (ret < 0)
108 		dev_err(runtime->dev, "Set codec sysclk failed: %d\n", ret);
109 
110 	return ret;
111 }
112 
113 static const struct snd_soc_ops avs_rt286_ops = {
114 	.hw_params = avs_rt286_hw_params,
115 };
116 
117 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
118 			       int tdm_slot, struct snd_soc_dai_link **dai_link)
119 {
120 	struct snd_soc_dai_link_component *platform;
121 	struct snd_soc_dai_link *dl;
122 
123 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
124 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
125 	if (!dl || !platform)
126 		return -ENOMEM;
127 
128 	platform->name = platform_name;
129 
130 	dl->name = devm_kasprintf(dev, GFP_KERNEL,
131 				  AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
132 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
133 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
134 	if (!dl->name || !dl->cpus || !dl->codecs)
135 		return -ENOMEM;
136 
137 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
138 					    AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
139 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT343A:00");
140 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, RT286_CODEC_DAI);
141 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
142 		return -ENOMEM;
143 
144 	dl->num_cpus = 1;
145 	dl->num_codecs = 1;
146 	dl->platforms = platform;
147 	dl->num_platforms = 1;
148 	dl->id = 0;
149 	dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
150 	dl->init = avs_rt286_codec_init;
151 	dl->exit = avs_rt286_codec_exit;
152 	dl->be_hw_params_fixup = avs_rt286_be_fixup;
153 	dl->ops = &avs_rt286_ops;
154 	dl->nonatomic = 1;
155 	dl->no_pcm = 1;
156 	dl->dpcm_capture = 1;
157 	dl->dpcm_playback = 1;
158 
159 	*dai_link = dl;
160 
161 	return 0;
162 }
163 
164 static int avs_card_suspend_pre(struct snd_soc_card *card)
165 {
166 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT286_CODEC_DAI);
167 
168 	return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
169 }
170 
171 static int avs_card_resume_post(struct snd_soc_card *card)
172 {
173 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT286_CODEC_DAI);
174 	struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
175 
176 	return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
177 }
178 
179 static int avs_rt286_probe(struct platform_device *pdev)
180 {
181 	struct snd_soc_dai_link *dai_link;
182 	struct snd_soc_acpi_mach *mach;
183 	struct snd_soc_card *card;
184 	struct snd_soc_jack *jack;
185 	struct device *dev = &pdev->dev;
186 	const char *pname;
187 	int ssp_port, tdm_slot, ret;
188 
189 	mach = dev_get_platdata(dev);
190 	pname = mach->mach_params.platform;
191 
192 	ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
193 	if (ret)
194 		return ret;
195 
196 	ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
197 
198 	if (ret) {
199 		dev_err(dev, "Failed to create dai link: %d", ret);
200 		return ret;
201 	}
202 
203 	jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
204 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
205 	if (!jack || !card)
206 		return -ENOMEM;
207 
208 	card->name = "avs_rt286";
209 	card->dev = dev;
210 	card->owner = THIS_MODULE;
211 	card->suspend_pre = avs_card_suspend_pre;
212 	card->resume_post = avs_card_resume_post;
213 	card->dai_link = dai_link;
214 	card->num_links = 1;
215 	card->controls = card_controls;
216 	card->num_controls = ARRAY_SIZE(card_controls);
217 	card->dapm_widgets = card_widgets;
218 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
219 	card->dapm_routes = card_base_routes;
220 	card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
221 	card->fully_routed = true;
222 	snd_soc_card_set_drvdata(card, jack);
223 
224 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
225 	if (ret)
226 		return ret;
227 
228 	return devm_snd_soc_register_card(dev, card);
229 }
230 
231 static const struct platform_device_id avs_rt286_driver_ids[] = {
232 	{
233 		.name = "avs_rt286",
234 	},
235 	{},
236 };
237 MODULE_DEVICE_TABLE(platform, avs_rt286_driver_ids);
238 
239 static struct platform_driver avs_rt286_driver = {
240 	.probe = avs_rt286_probe,
241 	.driver = {
242 		.name = "avs_rt286",
243 		.pm = &snd_soc_pm_ops,
244 	},
245 	.id_table = avs_rt286_driver_ids,
246 };
247 
248 module_platform_driver(avs_rt286_driver);
249 
250 MODULE_LICENSE("GPL");
251