xref: /openbmc/linux/sound/soc/intel/avs/boards/da7219.c (revision ecfb9f40)
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_data/x86/soc.h>
10 #include <linux/platform_device.h>
11 #include <sound/jack.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15 #include <sound/soc-acpi.h>
16 #include <sound/soc-dapm.h>
17 #include <uapi/linux/input-event-codes.h>
18 #include "../../../codecs/da7219.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_dai *codec_dai = asoc_rtd_to_codec(runtime, 0);
76 	struct snd_soc_component *component = codec_dai->component;
77 	struct snd_soc_card *card = runtime->card;
78 	struct snd_soc_jack *jack;
79 	int clk_freq;
80 	int ret;
81 
82 	jack = snd_soc_card_get_drvdata(card);
83 	if (soc_intel_is_apl())
84 		clk_freq = 19200000;
85 	else /* kbl */
86 		clk_freq = 24576000;
87 
88 	ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, clk_freq, SND_SOC_CLOCK_IN);
89 	if (ret) {
90 		dev_err(card->dev, "can't set codec sysclk configuration\n");
91 		return ret;
92 	}
93 
94 	/*
95 	 * Headset buttons map to the google Reference headset.
96 	 * These can be configured by userspace.
97 	 */
98 	ret = snd_soc_card_jack_new(card, "Headset Jack",
99 				    SND_JACK_HEADSET | SND_JACK_BTN_0 |
100 				    SND_JACK_BTN_1 | SND_JACK_BTN_2 |
101 				    SND_JACK_BTN_3 | SND_JACK_LINEOUT, jack);
102 	if (ret) {
103 		dev_err(card->dev, "Headset Jack creation failed: %d\n", ret);
104 		return ret;
105 	}
106 
107 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
108 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
109 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
110 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
111 
112 	return snd_soc_component_set_jack(component, jack, NULL);
113 }
114 
115 static void avs_da7219_codec_exit(struct snd_soc_pcm_runtime *rtd)
116 {
117 	snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component, NULL, NULL);
118 }
119 
120 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
121 			       struct snd_soc_dai_link **dai_link)
122 {
123 	struct snd_soc_dai_link_component *platform;
124 	struct snd_soc_dai_link *dl;
125 
126 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
127 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
128 	if (!dl || !platform)
129 		return -ENOMEM;
130 
131 	platform->name = platform_name;
132 
133 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
134 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
135 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
136 	if (!dl->name || !dl->cpus || !dl->codecs)
137 		return -ENOMEM;
138 
139 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
140 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-DLGS7219:00");
141 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, DA7219_DAI_NAME);
142 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
143 		return -ENOMEM;
144 
145 	dl->num_cpus = 1;
146 	dl->num_codecs = 1;
147 	dl->platforms = platform;
148 	dl->num_platforms = 1;
149 	dl->id = 0;
150 	dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
151 	dl->init = avs_da7219_codec_init;
152 	dl->exit = avs_da7219_codec_exit;
153 	dl->nonatomic = 1;
154 	dl->no_pcm = 1;
155 	dl->dpcm_capture = 1;
156 	dl->dpcm_playback = 1;
157 
158 	*dai_link = dl;
159 
160 	return 0;
161 }
162 
163 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
164 				  struct snd_soc_dapm_route **routes, int *num_routes)
165 {
166 	struct snd_soc_dapm_route *dr;
167 	const int num_base = ARRAY_SIZE(card_base_routes);
168 	const int num_dr = num_base + 2;
169 	int idx;
170 
171 	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
172 	if (!dr)
173 		return -ENOMEM;
174 
175 	memcpy(dr, card_base_routes, num_base * sizeof(*dr));
176 
177 	idx = num_base;
178 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Playback");
179 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
180 	if (!dr[idx].sink || !dr[idx].source)
181 		return -ENOMEM;
182 
183 	idx++;
184 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
185 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "Capture");
186 	if (!dr[idx].sink || !dr[idx].source)
187 		return -ENOMEM;
188 
189 	*routes = dr;
190 	*num_routes = num_dr;
191 
192 	return 0;
193 }
194 
195 static int avs_card_suspend_pre(struct snd_soc_card *card)
196 {
197 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, DA7219_DAI_NAME);
198 
199 	return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
200 }
201 
202 static int avs_card_resume_post(struct snd_soc_card *card)
203 {
204 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, DA7219_DAI_NAME);
205 	struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
206 
207 	return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
208 }
209 
210 static int avs_da7219_probe(struct platform_device *pdev)
211 {
212 	struct snd_soc_dapm_route *routes;
213 	struct snd_soc_dai_link *dai_link;
214 	struct snd_soc_acpi_mach *mach;
215 	struct snd_soc_card *card;
216 	struct snd_soc_jack *jack;
217 	struct device *dev = &pdev->dev;
218 	const char *pname;
219 	int num_routes, ssp_port, ret;
220 
221 	mach = dev_get_platdata(dev);
222 	pname = mach->mach_params.platform;
223 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
224 
225 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
226 	if (ret) {
227 		dev_err(dev, "Failed to create dai link: %d", ret);
228 		return ret;
229 	}
230 
231 	ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
232 	if (ret) {
233 		dev_err(dev, "Failed to create dapm routes: %d", ret);
234 		return ret;
235 	}
236 
237 	jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
238 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
239 	if (!jack || !card)
240 		return -ENOMEM;
241 
242 	card->name = "avs_da7219";
243 	card->dev = dev;
244 	card->owner = THIS_MODULE;
245 	card->suspend_pre = avs_card_suspend_pre;
246 	card->resume_post = avs_card_resume_post;
247 	card->dai_link = dai_link;
248 	card->num_links = 1;
249 	card->controls = card_controls;
250 	card->num_controls = ARRAY_SIZE(card_controls);
251 	card->dapm_widgets = card_widgets;
252 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
253 	card->dapm_routes = routes;
254 	card->num_dapm_routes = num_routes;
255 	card->fully_routed = true;
256 	snd_soc_card_set_drvdata(card, jack);
257 
258 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
259 	if (ret)
260 		return ret;
261 
262 	return devm_snd_soc_register_card(dev, card);
263 }
264 
265 static struct platform_driver avs_da7219_driver = {
266 	.probe = avs_da7219_probe,
267 	.driver = {
268 		.name = "avs_da7219",
269 		.pm = &snd_soc_pm_ops,
270 	},
271 };
272 
273 module_platform_driver(avs_da7219_driver);
274 
275 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
276 MODULE_LICENSE("GPL");
277 MODULE_ALIAS("platform:avs_da7219");
278