xref: /linux/sound/soc/qcom/x1e80100.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2023, Linaro Limited
3 
4 #include <dt-bindings/sound/qcom,q6afe.h>
5 #include <linux/module.h>
6 #include <linux/platform_device.h>
7 #include <linux/soundwire/sdw.h>
8 #include <sound/pcm.h>
9 #include <sound/jack.h>
10 #include <sound/soc.h>
11 #include <sound/soc-dapm.h>
12 
13 #include "common.h"
14 #include "qdsp6/q6afe.h"
15 #include "qdsp6/q6dsp-common.h"
16 #include "sdw.h"
17 
18 struct x1e80100_snd_data {
19 	bool stream_prepared[AFE_PORT_MAX];
20 	struct snd_soc_card *card;
21 	struct sdw_stream_runtime *sruntime[AFE_PORT_MAX];
22 	struct snd_soc_jack jack;
23 	struct snd_soc_jack dp_jack[8];
24 	bool jack_setup;
25 };
26 
27 static int x1e80100_snd_init(struct snd_soc_pcm_runtime *rtd)
28 {
29 	struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
30 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
31 	struct snd_soc_jack *dp_jack = NULL;
32 	int dp_pcm_id = 0;
33 
34 	switch (cpu_dai->id) {
35 	case DISPLAY_PORT_RX_0:
36 		dp_pcm_id = 0;
37 		dp_jack = &data->dp_jack[dp_pcm_id];
38 		break;
39 	case DISPLAY_PORT_RX_1 ... DISPLAY_PORT_RX_7:
40 		dp_pcm_id = cpu_dai->id - DISPLAY_PORT_RX_1 + 1;
41 		dp_jack = &data->dp_jack[dp_pcm_id];
42 		break;
43 	default:
44 		break;
45 	}
46 
47 	if (dp_jack)
48 		return qcom_snd_dp_jack_setup(rtd, dp_jack, dp_pcm_id);
49 
50 	return qcom_snd_wcd_jack_setup(rtd, &data->jack, &data->jack_setup);
51 }
52 
53 static void x1e80100_snd_shutdown(struct snd_pcm_substream *substream)
54 {
55 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
56 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
57 	struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
58 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
59 
60 	data->sruntime[cpu_dai->id] = NULL;
61 	sdw_release_stream(sruntime);
62 }
63 
64 static int x1e80100_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
65 				     struct snd_pcm_hw_params *params)
66 {
67 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
68 	struct snd_interval *rate = hw_param_interval(params,
69 						      SNDRV_PCM_HW_PARAM_RATE);
70 	struct snd_interval *channels = hw_param_interval(params,
71 							  SNDRV_PCM_HW_PARAM_CHANNELS);
72 
73 	rate->min = rate->max = 48000;
74 	switch (cpu_dai->id) {
75 	case TX_CODEC_DMA_TX_0:
76 	case TX_CODEC_DMA_TX_1:
77 	case TX_CODEC_DMA_TX_2:
78 	case TX_CODEC_DMA_TX_3:
79 		channels->min = 1;
80 		break;
81 	default:
82 		break;
83 	}
84 
85 	return 0;
86 }
87 
88 static int x1e80100_snd_hw_params(struct snd_pcm_substream *substream,
89 				struct snd_pcm_hw_params *params)
90 {
91 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
92 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
93 	struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
94 
95 	return qcom_snd_sdw_hw_params(substream, params, &data->sruntime[cpu_dai->id]);
96 }
97 
98 static int x1e80100_snd_hw_map_channels(unsigned int *ch_map, int num)
99 {
100 	switch (num) {
101 	case 1:
102 		ch_map[0] = PCM_CHANNEL_FC;
103 		break;
104 	case 2:
105 		ch_map[0] = PCM_CHANNEL_FL;
106 		ch_map[1] = PCM_CHANNEL_FR;
107 		break;
108 	case 3:
109 		ch_map[0] = PCM_CHANNEL_FL;
110 		ch_map[1] = PCM_CHANNEL_FR;
111 		ch_map[2] = PCM_CHANNEL_FC;
112 		break;
113 	case 4:
114 		ch_map[0] = PCM_CHANNEL_FL;
115 		ch_map[1] = PCM_CHANNEL_LB;
116 		ch_map[2] = PCM_CHANNEL_FR;
117 		ch_map[3] = PCM_CHANNEL_RB;
118 		break;
119 	default:
120 		return -EINVAL;
121 	}
122 
123 	return 0;
124 }
125 
126 static int x1e80100_snd_prepare(struct snd_pcm_substream *substream)
127 {
128 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
129 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
130 	struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
131 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
132 	unsigned int channels = substream->runtime->channels;
133 	unsigned int rx_slot[4];
134 	int ret;
135 
136 	switch (cpu_dai->id) {
137 	case WSA_CODEC_DMA_RX_0:
138 	case WSA_CODEC_DMA_RX_1:
139 		ret = x1e80100_snd_hw_map_channels(rx_slot, channels);
140 		if (ret)
141 			return ret;
142 
143 		ret = snd_soc_dai_set_channel_map(cpu_dai, 0, NULL,
144 						  channels, rx_slot);
145 		if (ret)
146 			return ret;
147 		break;
148 	default:
149 		break;
150 	}
151 
152 	return qcom_snd_sdw_prepare(substream, sruntime,
153 				    &data->stream_prepared[cpu_dai->id]);
154 }
155 
156 static int x1e80100_snd_hw_free(struct snd_pcm_substream *substream)
157 {
158 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
159 	struct x1e80100_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
160 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
161 	struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
162 
163 	return qcom_snd_sdw_hw_free(substream, sruntime,
164 				    &data->stream_prepared[cpu_dai->id]);
165 }
166 
167 static const struct snd_soc_ops x1e80100_be_ops = {
168 	.startup = qcom_snd_sdw_startup,
169 	.shutdown = x1e80100_snd_shutdown,
170 	.hw_params = x1e80100_snd_hw_params,
171 	.hw_free = x1e80100_snd_hw_free,
172 	.prepare = x1e80100_snd_prepare,
173 };
174 
175 static void x1e80100_add_be_ops(struct snd_soc_card *card)
176 {
177 	struct snd_soc_dai_link *link;
178 	int i;
179 
180 	for_each_card_prelinks(card, i, link) {
181 		if (link->no_pcm == 1) {
182 			link->init = x1e80100_snd_init;
183 			link->be_hw_params_fixup = x1e80100_be_hw_params_fixup;
184 			link->ops = &x1e80100_be_ops;
185 		}
186 	}
187 }
188 
189 static int x1e80100_platform_probe(struct platform_device *pdev)
190 {
191 	struct snd_soc_card *card;
192 	struct x1e80100_snd_data *data;
193 	struct device *dev = &pdev->dev;
194 	int ret;
195 
196 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
197 	if (!card)
198 		return -ENOMEM;
199 	/* Allocate the private data */
200 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
201 	if (!data)
202 		return -ENOMEM;
203 
204 	card->owner = THIS_MODULE;
205 	card->dev = dev;
206 	dev_set_drvdata(dev, card);
207 	snd_soc_card_set_drvdata(card, data);
208 
209 	ret = qcom_snd_parse_of(card);
210 	if (ret)
211 		return ret;
212 
213 	card->driver_name = "x1e80100";
214 	x1e80100_add_be_ops(card);
215 
216 	return devm_snd_soc_register_card(dev, card);
217 }
218 
219 static const struct of_device_id snd_x1e80100_dt_match[] = {
220 	{ .compatible = "qcom,x1e80100-sndcard", },
221 	{}
222 };
223 MODULE_DEVICE_TABLE(of, snd_x1e80100_dt_match);
224 
225 static struct platform_driver snd_x1e80100_driver = {
226 	.probe  = x1e80100_platform_probe,
227 	.driver = {
228 		.name = "snd-x1e80100",
229 		.of_match_table = snd_x1e80100_dt_match,
230 	},
231 };
232 module_platform_driver(snd_x1e80100_driver);
233 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
234 MODULE_AUTHOR("Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>");
235 MODULE_DESCRIPTION("Qualcomm X1E80100 ASoC Machine Driver");
236 MODULE_LICENSE("GPL");
237