xref: /openbmc/linux/sound/soc/qcom/sc7280.c (revision fe4d0d5d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
4 //
5 // ALSA SoC Machine driver for sc7280
6 
7 #include <linux/input.h>
8 #include <linux/module.h>
9 #include <linux/of_device.h>
10 #include <linux/platform_device.h>
11 #include <sound/core.h>
12 #include <sound/jack.h>
13 #include <sound/pcm.h>
14 #include <sound/soc.h>
15 #include <linux/soundwire/sdw.h>
16 
17 #include "common.h"
18 #include "lpass.h"
19 
20 struct sc7280_snd_data {
21 	struct snd_soc_card card;
22 	struct sdw_stream_runtime *sruntime[LPASS_MAX_PORTS];
23 	struct snd_soc_jack hs_jack;
24 	struct snd_soc_jack hdmi_jack;
25 	bool jack_setup;
26 	bool stream_prepared[LPASS_MAX_PORTS];
27 };
28 
29 static void sc7280_jack_free(struct snd_jack *jack)
30 {
31 	struct snd_soc_component *component = jack->private_data;
32 
33 	snd_soc_component_set_jack(component, NULL, NULL);
34 }
35 
36 static int sc7280_headset_init(struct snd_soc_pcm_runtime *rtd)
37 {
38 	struct snd_soc_card *card = rtd->card;
39 	struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(card);
40 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
41 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
42 	struct snd_soc_component *component = codec_dai->component;
43 	struct snd_jack *jack;
44 	int rval, i;
45 
46 	if (!pdata->jack_setup) {
47 		rval = snd_soc_card_jack_new(card, "Headset Jack",
48 					     SND_JACK_HEADSET | SND_JACK_LINEOUT |
49 					     SND_JACK_MECHANICAL |
50 					     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
51 					     SND_JACK_BTN_2 | SND_JACK_BTN_3 |
52 					     SND_JACK_BTN_4 | SND_JACK_BTN_5,
53 					     &pdata->hs_jack, NULL, 0);
54 
55 		if (rval < 0) {
56 			dev_err(card->dev, "Unable to add Headset Jack\n");
57 			return rval;
58 		}
59 
60 		jack = pdata->hs_jack.jack;
61 
62 		snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
63 		snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
64 		snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
65 		snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
66 
67 		jack->private_data = component;
68 		jack->private_free = sc7280_jack_free;
69 		pdata->jack_setup = true;
70 	}
71 	switch (cpu_dai->id) {
72 	case LPASS_CDC_DMA_RX0:
73 	case LPASS_CDC_DMA_TX3:
74 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
75 			rval = snd_soc_component_set_jack(component, &pdata->hs_jack, NULL);
76 			if (rval != 0 && rval != -ENOTSUPP) {
77 				dev_err(card->dev, "Failed to set jack: %d\n", rval);
78 				return rval;
79 			}
80 		}
81 		break;
82 	default:
83 		break;
84 	}
85 
86 	return 0;
87 }
88 
89 static int sc7280_hdmi_init(struct snd_soc_pcm_runtime *rtd)
90 {
91 	struct snd_soc_card *card = rtd->card;
92 	struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(card);
93 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
94 	struct snd_soc_component *component = codec_dai->component;
95 	struct snd_jack *jack;
96 	int rval;
97 
98 	rval = snd_soc_card_jack_new(card, "HDMI Jack",	SND_JACK_LINEOUT,
99 				     &pdata->hdmi_jack, NULL, 0);
100 
101 	if (rval < 0) {
102 		dev_err(card->dev, "Unable to add HDMI Jack\n");
103 		return rval;
104 	}
105 
106 	jack = pdata->hdmi_jack.jack;
107 	jack->private_data = component;
108 	jack->private_free = sc7280_jack_free;
109 
110 	return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL);
111 }
112 
113 static int sc7280_init(struct snd_soc_pcm_runtime *rtd)
114 {
115 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
116 
117 	switch (cpu_dai->id) {
118 	case LPASS_CDC_DMA_TX3:
119 		return sc7280_headset_init(rtd);
120 	case LPASS_CDC_DMA_RX0:
121 	case LPASS_CDC_DMA_VA_TX0:
122 	case MI2S_SECONDARY:
123 		return 0;
124 	case LPASS_DP_RX:
125 		return sc7280_hdmi_init(rtd);
126 	default:
127 		dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__, cpu_dai->id);
128 	}
129 
130 	return -EINVAL;
131 }
132 
133 static int sc7280_snd_hw_params(struct snd_pcm_substream *substream,
134 				struct snd_pcm_hw_params *params)
135 {
136 	struct snd_pcm_runtime *runtime = substream->runtime;
137 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
138 	struct snd_soc_dai *codec_dai;
139 	const struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
140 	struct sc7280_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
141 	struct sdw_stream_runtime *sruntime;
142 	int i;
143 
144 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
145 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, 48000, 48000);
146 
147 	switch (cpu_dai->id) {
148 	case LPASS_CDC_DMA_TX3:
149 	case LPASS_CDC_DMA_RX0:
150 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
151 			sruntime = snd_soc_dai_get_stream(codec_dai, substream->stream);
152 			if (sruntime != ERR_PTR(-ENOTSUPP))
153 				pdata->sruntime[cpu_dai->id] = sruntime;
154 		}
155 		break;
156 	}
157 
158 	return 0;
159 }
160 
161 static int sc7280_snd_swr_prepare(struct snd_pcm_substream *substream)
162 {
163 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
164 	const struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
165 	struct sc7280_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
166 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
167 	int ret;
168 
169 	if (!sruntime)
170 		return 0;
171 
172 	if (data->stream_prepared[cpu_dai->id]) {
173 		sdw_disable_stream(sruntime);
174 		sdw_deprepare_stream(sruntime);
175 		data->stream_prepared[cpu_dai->id] = false;
176 	}
177 
178 	ret = sdw_prepare_stream(sruntime);
179 	if (ret)
180 		return ret;
181 
182 	ret = sdw_enable_stream(sruntime);
183 	if (ret) {
184 		sdw_deprepare_stream(sruntime);
185 		return ret;
186 	}
187 	data->stream_prepared[cpu_dai->id] = true;
188 
189 	return ret;
190 }
191 
192 static int sc7280_snd_prepare(struct snd_pcm_substream *substream)
193 {
194 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
195 	const struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
196 
197 	switch (cpu_dai->id) {
198 	case LPASS_CDC_DMA_RX0:
199 	case LPASS_CDC_DMA_TX3:
200 		return sc7280_snd_swr_prepare(substream);
201 	default:
202 		break;
203 	}
204 
205 	return 0;
206 }
207 
208 static int sc7280_snd_hw_free(struct snd_pcm_substream *substream)
209 {
210 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
211 	struct sc7280_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
212 	const struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
213 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
214 
215 	switch (cpu_dai->id) {
216 	case LPASS_CDC_DMA_RX0:
217 	case LPASS_CDC_DMA_TX3:
218 		if (sruntime && data->stream_prepared[cpu_dai->id]) {
219 			sdw_disable_stream(sruntime);
220 			sdw_deprepare_stream(sruntime);
221 			data->stream_prepared[cpu_dai->id] = false;
222 		}
223 		break;
224 	default:
225 		break;
226 	}
227 	return 0;
228 }
229 
230 static const struct snd_soc_ops sc7280_ops = {
231 	.hw_params = sc7280_snd_hw_params,
232 	.hw_free = sc7280_snd_hw_free,
233 	.prepare = sc7280_snd_prepare,
234 };
235 
236 static int sc7280_snd_platform_probe(struct platform_device *pdev)
237 {
238 	struct snd_soc_card *card;
239 	struct sc7280_snd_data *data;
240 	struct device *dev = &pdev->dev;
241 	struct snd_soc_dai_link *link;
242 	int ret, i;
243 
244 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
245 	if (!data)
246 		return -ENOMEM;
247 
248 	card = &data->card;
249 	snd_soc_card_set_drvdata(card, data);
250 
251 	card->owner = THIS_MODULE;
252 	card->driver_name = "SC7280";
253 	card->dev = dev;
254 
255 	ret = qcom_snd_parse_of(card);
256 	if (ret)
257 		return ret;
258 
259 	for_each_card_prelinks(card, i, link) {
260 		link->init = sc7280_init;
261 		link->ops = &sc7280_ops;
262 	}
263 
264 	return devm_snd_soc_register_card(dev, card);
265 }
266 
267 static const struct of_device_id sc7280_snd_device_id[]  = {
268 	{ .compatible = "google,sc7280-herobrine" },
269 	{}
270 };
271 MODULE_DEVICE_TABLE(of, sc7280_snd_device_id);
272 
273 static struct platform_driver sc7280_snd_driver = {
274 	.probe = sc7280_snd_platform_probe,
275 	.driver = {
276 		.name = "msm-snd-sc7280",
277 		.of_match_table = sc7280_snd_device_id,
278 		.pm = &snd_soc_pm_ops,
279 	},
280 };
281 module_platform_driver(sc7280_snd_driver);
282 
283 MODULE_DESCRIPTION("sc7280 ASoC Machine Driver");
284 MODULE_LICENSE("GPL");
285