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