xref: /openbmc/linux/sound/soc/intel/avs/boards/da7219.c (revision dff03381)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4 //
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
6 //
7 
8 #include <linux/module.h>
9 #include <linux/platform_device.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 <sound/soc-dapm.h>
16 #include <uapi/linux/input-event-codes.h>
17 #include "../../../codecs/da7219.h"
18 #include "../../../codecs/da7219-aad.h"
19 
20 #define DA7219_DAI_NAME		"da7219-hifi"
21 
22 static const struct snd_kcontrol_new card_controls[] = {
23 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
24 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
25 };
26 
27 static int platform_clock_control(struct snd_soc_dapm_widget *w,
28 				  struct snd_kcontrol *k, 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 = 0;
34 
35 	codec_dai = snd_soc_card_get_codec_dai(card, DA7219_DAI_NAME);
36 	if (!codec_dai) {
37 		dev_err(card->dev, "Codec dai not found. Unable to set/unset codec pll\n");
38 		return -EIO;
39 	}
40 
41 	if (SND_SOC_DAPM_EVENT_OFF(event)) {
42 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
43 		if (ret)
44 			dev_err(card->dev, "failed to stop PLL: %d\n", ret);
45 	} else if (SND_SOC_DAPM_EVENT_ON(event)) {
46 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL_SRM,
47 					  0, DA7219_PLL_FREQ_OUT_98304);
48 		if (ret)
49 			dev_err(card->dev, "failed to start PLL: %d\n", ret);
50 	}
51 
52 	return ret;
53 }
54 
55 static const struct snd_soc_dapm_widget card_widgets[] = {
56 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
57 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
58 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, platform_clock_control,
59 			    SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_PRE_PMU),
60 };
61 
62 static const struct snd_soc_dapm_route card_base_routes[] = {
63 	/* HP jack connectors - unknown if we have jack detection */
64 	{"Headphone Jack", NULL, "HPL"},
65 	{"Headphone Jack", NULL, "HPR"},
66 
67 	{"MIC", NULL, "Headset Mic"},
68 
69 	{ "Headphone Jack", NULL, "Platform Clock" },
70 	{ "Headset Mic", NULL, "Platform Clock" },
71 };
72 
73 static int avs_da7219_codec_init(struct snd_soc_pcm_runtime *runtime)
74 {
75 	struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
76 	struct snd_soc_card *card = runtime->card;
77 	struct snd_soc_jack *jack;
78 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(runtime, 0);
79 	int clk_freq;
80 	int ret;
81 
82 	jack = snd_soc_card_get_drvdata(card);
83 	clk_freq = 19200000;
84 
85 	ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, clk_freq, SND_SOC_CLOCK_IN);
86 	if (ret) {
87 		dev_err(card->dev, "can't set codec sysclk configuration\n");
88 		return ret;
89 	}
90 
91 	/*
92 	 * Headset buttons map to the google Reference headset.
93 	 * These can be configured by userspace.
94 	 */
95 	ret = snd_soc_card_jack_new(card, "Headset Jack",
96 				    SND_JACK_HEADSET | SND_JACK_BTN_0 |
97 				    SND_JACK_BTN_1 | SND_JACK_BTN_2 |
98 				    SND_JACK_BTN_3 | SND_JACK_LINEOUT, jack);
99 	if (ret) {
100 		dev_err(card->dev, "Headset Jack creation failed: %d\n", ret);
101 		return ret;
102 	}
103 
104 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
105 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
106 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
107 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
108 
109 	da7219_aad_jack_det(component, jack);
110 
111 	return 0;
112 }
113 
114 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
115 			       struct snd_soc_dai_link **dai_link)
116 {
117 	struct snd_soc_dai_link_component *platform;
118 	struct snd_soc_dai_link *dl;
119 
120 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
121 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
122 	if (!dl || !platform)
123 		return -ENOMEM;
124 
125 	platform->name = platform_name;
126 
127 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
128 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
129 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
130 	if (!dl->name || !dl->cpus || !dl->codecs)
131 		return -ENOMEM;
132 
133 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
134 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-DLGS7219:00");
135 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, DA7219_DAI_NAME);
136 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
137 		return -ENOMEM;
138 
139 	dl->num_cpus = 1;
140 	dl->num_codecs = 1;
141 	dl->platforms = platform;
142 	dl->num_platforms = 1;
143 	dl->id = 0;
144 	dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
145 	dl->init = avs_da7219_codec_init;
146 	dl->nonatomic = 1;
147 	dl->no_pcm = 1;
148 	dl->dpcm_capture = 1;
149 	dl->dpcm_playback = 1;
150 
151 	*dai_link = dl;
152 
153 	return 0;
154 }
155 
156 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
157 				  struct snd_soc_dapm_route **routes, int *num_routes)
158 {
159 	struct snd_soc_dapm_route *dr;
160 	const int num_base = ARRAY_SIZE(card_base_routes);
161 	const int num_dr = num_base + 2;
162 	int idx;
163 
164 	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
165 	if (!dr)
166 		return -ENOMEM;
167 
168 	memcpy(dr, card_base_routes, num_base * sizeof(*dr));
169 
170 	idx = num_base;
171 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Playback");
172 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
173 	if (!dr[idx].sink || !dr[idx].source)
174 		return -ENOMEM;
175 
176 	idx++;
177 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
178 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "Capture");
179 	if (!dr[idx].sink || !dr[idx].source)
180 		return -ENOMEM;
181 
182 	*routes = dr;
183 	*num_routes = num_dr;
184 
185 	return 0;
186 }
187 
188 static int avs_card_set_jack(struct snd_soc_card *card, struct snd_soc_jack *jack)
189 {
190 	struct snd_soc_component *component;
191 
192 	for_each_card_components(card, component)
193 		snd_soc_component_set_jack(component, jack, NULL);
194 	return 0;
195 }
196 
197 static int avs_card_remove(struct snd_soc_card *card)
198 {
199 	return avs_card_set_jack(card, NULL);
200 }
201 
202 static int avs_card_suspend_pre(struct snd_soc_card *card)
203 {
204 	return avs_card_set_jack(card, NULL);
205 }
206 
207 static int avs_card_resume_post(struct snd_soc_card *card)
208 {
209 	struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
210 
211 	return avs_card_set_jack(card, jack);
212 }
213 
214 static int avs_da7219_probe(struct platform_device *pdev)
215 {
216 	struct snd_soc_dapm_route *routes;
217 	struct snd_soc_dai_link *dai_link;
218 	struct snd_soc_acpi_mach *mach;
219 	struct snd_soc_card *card;
220 	struct snd_soc_jack *jack;
221 	struct device *dev = &pdev->dev;
222 	const char *pname;
223 	int num_routes, ssp_port, ret;
224 
225 	mach = dev_get_platdata(dev);
226 	pname = mach->mach_params.platform;
227 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
228 
229 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
230 	if (ret) {
231 		dev_err(dev, "Failed to create dai link: %d", ret);
232 		return ret;
233 	}
234 
235 	ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
236 	if (ret) {
237 		dev_err(dev, "Failed to create dapm routes: %d", ret);
238 		return ret;
239 	}
240 
241 	jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
242 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
243 	if (!jack || !card)
244 		return -ENOMEM;
245 
246 	card->name = "avs_da7219";
247 	card->dev = dev;
248 	card->owner = THIS_MODULE;
249 	card->remove = avs_card_remove;
250 	card->suspend_pre = avs_card_suspend_pre;
251 	card->resume_post = avs_card_resume_post;
252 	card->dai_link = dai_link;
253 	card->num_links = 1;
254 	card->controls = card_controls;
255 	card->num_controls = ARRAY_SIZE(card_controls);
256 	card->dapm_widgets = card_widgets;
257 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
258 	card->dapm_routes = routes;
259 	card->num_dapm_routes = num_routes;
260 	card->fully_routed = true;
261 	snd_soc_card_set_drvdata(card, jack);
262 
263 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
264 	if (ret)
265 		return ret;
266 
267 	return devm_snd_soc_register_card(dev, card);
268 }
269 
270 static struct platform_driver avs_da7219_driver = {
271 	.probe = avs_da7219_probe,
272 	.driver = {
273 		.name = "avs_da7219",
274 		.pm = &snd_soc_pm_ops,
275 	},
276 };
277 
278 module_platform_driver(avs_da7219_driver);
279 
280 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
281 MODULE_LICENSE("GPL");
282 MODULE_ALIAS("platform:avs_da7219");
283