xref: /openbmc/linux/sound/soc/qcom/sc7180.c (revision dff03381)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright (c) 2020, The Linux Foundation. All rights reserved.
4 //
5 // sc7180.c -- ALSA SoC Machine driver for SC7180
6 
7 #include <dt-bindings/sound/sc7180-lpass.h>
8 #include <linux/gpio.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <sound/core.h>
14 #include <sound/jack.h>
15 #include <sound/pcm.h>
16 #include <sound/soc.h>
17 #include <uapi/linux/input-event-codes.h>
18 
19 #include "../codecs/rt5682.h"
20 #include "../codecs/rt5682s.h"
21 #include "common.h"
22 #include "lpass.h"
23 
24 #define DEFAULT_MCLK_RATE		19200000
25 #define RT5682_PLL1_FREQ (48000 * 512)
26 
27 #define DRIVER_NAME "SC7180"
28 
29 struct sc7180_snd_data {
30 	struct snd_soc_card card;
31 	u32 pri_mi2s_clk_count;
32 	struct snd_soc_jack hs_jack;
33 	struct snd_soc_jack hdmi_jack;
34 	struct gpio_desc *dmic_sel;
35 	int dmic_switch;
36 };
37 
38 static void sc7180_jack_free(struct snd_jack *jack)
39 {
40 	struct snd_soc_component *component = jack->private_data;
41 
42 	snd_soc_component_set_jack(component, NULL, NULL);
43 }
44 
45 static int sc7180_headset_init(struct snd_soc_pcm_runtime *rtd)
46 {
47 	struct snd_soc_card *card = rtd->card;
48 	struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card);
49 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
50 	struct snd_soc_component *component = codec_dai->component;
51 	struct snd_jack *jack;
52 	int rval;
53 
54 	rval = snd_soc_card_jack_new(
55 			card, "Headset Jack",
56 			SND_JACK_HEADSET |
57 			SND_JACK_HEADPHONE |
58 			SND_JACK_BTN_0 | SND_JACK_BTN_1 |
59 			SND_JACK_BTN_2 | SND_JACK_BTN_3,
60 			&pdata->hs_jack);
61 
62 	if (rval < 0) {
63 		dev_err(card->dev, "Unable to add Headset Jack\n");
64 		return rval;
65 	}
66 
67 	jack = pdata->hs_jack.jack;
68 
69 	snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
70 	snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
71 	snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
72 	snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
73 
74 	jack->private_data = component;
75 	jack->private_free = sc7180_jack_free;
76 
77 	return snd_soc_component_set_jack(component, &pdata->hs_jack, NULL);
78 }
79 
80 static int sc7180_hdmi_init(struct snd_soc_pcm_runtime *rtd)
81 {
82 	struct snd_soc_card *card = rtd->card;
83 	struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card);
84 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
85 	struct snd_soc_component *component = codec_dai->component;
86 	struct snd_jack *jack;
87 	int rval;
88 
89 	rval = snd_soc_card_jack_new(
90 			card, "HDMI Jack",
91 			SND_JACK_LINEOUT,
92 			&pdata->hdmi_jack);
93 
94 	if (rval < 0) {
95 		dev_err(card->dev, "Unable to add HDMI Jack\n");
96 		return rval;
97 	}
98 
99 	jack = pdata->hdmi_jack.jack;
100 	jack->private_data = component;
101 	jack->private_free = sc7180_jack_free;
102 
103 	return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL);
104 }
105 
106 static int sc7180_init(struct snd_soc_pcm_runtime *rtd)
107 {
108 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
109 
110 	switch (cpu_dai->id) {
111 	case MI2S_PRIMARY:
112 		return sc7180_headset_init(rtd);
113 	case MI2S_SECONDARY:
114 		return 0;
115 	case LPASS_DP_RX:
116 		return sc7180_hdmi_init(rtd);
117 	default:
118 		dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
119 			cpu_dai->id);
120 		return -EINVAL;
121 	}
122 	return 0;
123 }
124 
125 static int sc7180_snd_startup(struct snd_pcm_substream *substream)
126 {
127 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
128 	struct snd_soc_card *card = rtd->card;
129 	struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card);
130 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
131 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
132 	int pll_id, pll_source, pll_in, pll_out, clk_id, ret;
133 
134 	if (!strcmp(codec_dai->name, "rt5682-aif1")) {
135 		pll_source = RT5682_PLL1_S_MCLK;
136 		pll_id = 0;
137 		clk_id = RT5682_SCLK_S_PLL1;
138 		pll_out = RT5682_PLL1_FREQ;
139 		pll_in = DEFAULT_MCLK_RATE;
140 	} else if (!strcmp(codec_dai->name, "rt5682s-aif1")) {
141 		pll_source = RT5682S_PLL_S_MCLK;
142 		pll_id = RT5682S_PLL2;
143 		clk_id = RT5682S_SCLK_S_PLL2;
144 		pll_out = RT5682_PLL1_FREQ;
145 		pll_in = DEFAULT_MCLK_RATE;
146 	}
147 
148 	switch (cpu_dai->id) {
149 	case MI2S_PRIMARY:
150 		if (++data->pri_mi2s_clk_count == 1) {
151 			snd_soc_dai_set_sysclk(cpu_dai,
152 					       LPASS_MCLK0,
153 					       DEFAULT_MCLK_RATE,
154 					       SNDRV_PCM_STREAM_PLAYBACK);
155 		}
156 
157 		snd_soc_dai_set_fmt(codec_dai,
158 				    SND_SOC_DAIFMT_BC_FC |
159 				    SND_SOC_DAIFMT_NB_NF |
160 				    SND_SOC_DAIFMT_I2S);
161 
162 		/* Configure PLL1 for codec */
163 		ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source,
164 					  pll_in, pll_out);
165 		if (ret) {
166 			dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
167 			return ret;
168 		}
169 
170 		/* Configure sysclk for codec */
171 		ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, pll_out,
172 					     SND_SOC_CLOCK_IN);
173 		if (ret)
174 			dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n",
175 				ret);
176 
177 		break;
178 	case MI2S_SECONDARY:
179 		break;
180 	case LPASS_DP_RX:
181 		break;
182 	default:
183 		dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
184 			cpu_dai->id);
185 		return -EINVAL;
186 	}
187 	return 0;
188 }
189 
190 static int dmic_get(struct snd_kcontrol *kcontrol,
191 		    struct snd_ctl_elem_value *ucontrol)
192 {
193 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
194 	struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card);
195 
196 	ucontrol->value.integer.value[0] = data->dmic_switch;
197 	return 0;
198 }
199 
200 static int dmic_set(struct snd_kcontrol *kcontrol,
201 		    struct snd_ctl_elem_value *ucontrol)
202 {
203 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
204 	struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card);
205 
206 	data->dmic_switch = ucontrol->value.integer.value[0];
207 	gpiod_set_value(data->dmic_sel, data->dmic_switch);
208 	return 0;
209 }
210 
211 static void sc7180_snd_shutdown(struct snd_pcm_substream *substream)
212 {
213 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
214 	struct snd_soc_card *card = rtd->card;
215 	struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card);
216 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
217 
218 	switch (cpu_dai->id) {
219 	case MI2S_PRIMARY:
220 		if (--data->pri_mi2s_clk_count == 0) {
221 			snd_soc_dai_set_sysclk(cpu_dai,
222 					       LPASS_MCLK0,
223 					       0,
224 					       SNDRV_PCM_STREAM_PLAYBACK);
225 		}
226 		break;
227 	case MI2S_SECONDARY:
228 		break;
229 	case LPASS_DP_RX:
230 		break;
231 	default:
232 		dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
233 			cpu_dai->id);
234 		break;
235 	}
236 }
237 
238 static int sc7180_adau7002_init(struct snd_soc_pcm_runtime *rtd)
239 {
240 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
241 
242 	switch (cpu_dai->id) {
243 	case MI2S_PRIMARY:
244 		return 0;
245 	case MI2S_SECONDARY:
246 		return 0;
247 	case LPASS_DP_RX:
248 		return sc7180_hdmi_init(rtd);
249 	default:
250 		dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
251 			cpu_dai->id);
252 		return -EINVAL;
253 	}
254 	return 0;
255 }
256 
257 static int sc7180_adau7002_snd_startup(struct snd_pcm_substream *substream)
258 {
259 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
260 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
261 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
262 	struct snd_pcm_runtime *runtime = substream->runtime;
263 
264 	switch (cpu_dai->id) {
265 	case MI2S_PRIMARY:
266 		snd_soc_dai_set_fmt(codec_dai,
267 				    SND_SOC_DAIFMT_CBS_CFS |
268 				    SND_SOC_DAIFMT_NB_NF |
269 				    SND_SOC_DAIFMT_I2S);
270 		runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
271 		snd_pcm_hw_constraint_msbits(runtime, 0, 32, 32);
272 
273 		break;
274 	case MI2S_SECONDARY:
275 		break;
276 	case LPASS_DP_RX:
277 		break;
278 	default:
279 		dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
280 			cpu_dai->id);
281 		return -EINVAL;
282 	}
283 	return 0;
284 }
285 
286 static const struct snd_soc_ops sc7180_ops = {
287 	.startup = sc7180_snd_startup,
288 	.shutdown = sc7180_snd_shutdown,
289 };
290 
291 static const struct snd_soc_ops sc7180_adau7002_ops = {
292 	.startup = sc7180_adau7002_snd_startup,
293 };
294 
295 static const struct snd_soc_dapm_widget sc7180_snd_widgets[] = {
296 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
297 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
298 };
299 
300 static const struct snd_soc_dapm_widget sc7180_adau7002_snd_widgets[] = {
301 	SND_SOC_DAPM_MIC("DMIC", NULL),
302 };
303 
304 static const char * const dmic_mux_text[] = {
305 	"Front Mic",
306 	"Rear Mic",
307 };
308 
309 static SOC_ENUM_SINGLE_DECL(sc7180_dmic_enum,
310 			    SND_SOC_NOPM, 0, dmic_mux_text);
311 
312 static const struct snd_kcontrol_new sc7180_dmic_mux_control =
313 	SOC_DAPM_ENUM_EXT("DMIC Select Mux", sc7180_dmic_enum,
314 			  dmic_get, dmic_set);
315 
316 static const struct snd_soc_dapm_widget sc7180_snd_dual_mic_widgets[] = {
317 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
318 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
319 	SND_SOC_DAPM_MIC("DMIC", NULL),
320 	SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0, &sc7180_dmic_mux_control),
321 };
322 
323 static const struct snd_soc_dapm_route sc7180_snd_dual_mic_audio_route[] = {
324 	{"Dmic Mux", "Front Mic", "DMIC"},
325 	{"Dmic Mux", "Rear Mic", "DMIC"},
326 };
327 
328 static int sc7180_snd_platform_probe(struct platform_device *pdev)
329 {
330 	struct snd_soc_card *card;
331 	struct sc7180_snd_data *data;
332 	struct device *dev = &pdev->dev;
333 	struct snd_soc_dai_link *link;
334 	int ret;
335 	int i;
336 	bool no_headphone = false;
337 
338 	/* Allocate the private data */
339 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
340 	if (!data)
341 		return -ENOMEM;
342 
343 	card = &data->card;
344 	snd_soc_card_set_drvdata(card, data);
345 
346 	card->owner = THIS_MODULE;
347 	card->driver_name = DRIVER_NAME;
348 	card->dev = dev;
349 	card->dapm_widgets = sc7180_snd_widgets;
350 	card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_widgets);
351 
352 	if (of_property_read_bool(dev->of_node, "dmic-gpios")) {
353 		card->dapm_widgets = sc7180_snd_dual_mic_widgets,
354 		card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_dual_mic_widgets),
355 		card->dapm_routes = sc7180_snd_dual_mic_audio_route,
356 		card->num_dapm_routes = ARRAY_SIZE(sc7180_snd_dual_mic_audio_route),
357 		data->dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW);
358 		if (IS_ERR(data->dmic_sel)) {
359 			dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n", PTR_ERR(data->dmic_sel));
360 			return PTR_ERR(data->dmic_sel);
361 		}
362 	}
363 
364 	if (of_device_is_compatible(dev->of_node, "google,sc7180-coachz")) {
365 		no_headphone = true;
366 		card->dapm_widgets = sc7180_adau7002_snd_widgets;
367 		card->num_dapm_widgets = ARRAY_SIZE(sc7180_adau7002_snd_widgets);
368 	}
369 
370 	ret = qcom_snd_parse_of(card);
371 	if (ret)
372 		return ret;
373 
374 	for_each_card_prelinks(card, i, link) {
375 		if (no_headphone) {
376 			link->ops = &sc7180_adau7002_ops;
377 			link->init = sc7180_adau7002_init;
378 		} else {
379 			link->ops = &sc7180_ops;
380 			link->init = sc7180_init;
381 		}
382 	}
383 
384 	return devm_snd_soc_register_card(dev, card);
385 }
386 
387 static const struct of_device_id sc7180_snd_device_id[]  = {
388 	{.compatible = "google,sc7180-trogdor"},
389 	{.compatible = "google,sc7180-coachz"},
390 	{},
391 };
392 MODULE_DEVICE_TABLE(of, sc7180_snd_device_id);
393 
394 static struct platform_driver sc7180_snd_driver = {
395 	.probe = sc7180_snd_platform_probe,
396 	.driver = {
397 		.name = "msm-snd-sc7180",
398 		.of_match_table = sc7180_snd_device_id,
399 		.pm = &snd_soc_pm_ops,
400 	},
401 };
402 module_platform_driver(sc7180_snd_driver);
403 
404 MODULE_DESCRIPTION("sc7180 ASoC Machine Driver");
405 MODULE_LICENSE("GPL v2");
406