xref: /linux/sound/soc/qcom/qdsp6/q6apm-lpass-dais.c (revision d7f39aee79f04eeaa42085728423501b33ac5be5)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2021, Linaro Limited
3 
4 #include <dt-bindings/sound/qcom,q6dsp-lpass-ports.h>
5 #include <linux/err.h>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/device.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <sound/pcm.h>
12 #include <sound/soc.h>
13 #include <sound/pcm_params.h>
14 #include "q6dsp-lpass-ports.h"
15 #include "q6dsp-common.h"
16 #include "audioreach.h"
17 #include "q6apm.h"
18 
19 #define AUDIOREACH_BE_PCM_BASE	16
20 
21 struct q6apm_lpass_dai_data {
22 	struct q6apm_graph *graph[APM_PORT_MAX];
23 	bool is_port_started[APM_PORT_MAX];
24 	struct audioreach_module_config module_config[APM_PORT_MAX];
25 };
26 
27 static int q6dma_set_channel_map(struct snd_soc_dai *dai,
28 				 unsigned int tx_num, unsigned int *tx_ch_mask,
29 				 unsigned int rx_num, unsigned int *rx_ch_mask)
30 {
31 
32 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
33 	struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
34 	int ch_mask;
35 
36 	switch (dai->id) {
37 	case WSA_CODEC_DMA_TX_0:
38 	case WSA_CODEC_DMA_TX_1:
39 	case WSA_CODEC_DMA_TX_2:
40 	case VA_CODEC_DMA_TX_0:
41 	case VA_CODEC_DMA_TX_1:
42 	case VA_CODEC_DMA_TX_2:
43 	case TX_CODEC_DMA_TX_0:
44 	case TX_CODEC_DMA_TX_1:
45 	case TX_CODEC_DMA_TX_2:
46 	case TX_CODEC_DMA_TX_3:
47 	case TX_CODEC_DMA_TX_4:
48 	case TX_CODEC_DMA_TX_5:
49 		if (!tx_ch_mask) {
50 			dev_err(dai->dev, "tx slot not found\n");
51 			return -EINVAL;
52 		}
53 
54 		if (tx_num > AR_PCM_MAX_NUM_CHANNEL) {
55 			dev_err(dai->dev, "invalid tx num %d\n",
56 				tx_num);
57 			return -EINVAL;
58 		}
59 		ch_mask = *tx_ch_mask;
60 
61 		break;
62 	case WSA_CODEC_DMA_RX_0:
63 	case WSA_CODEC_DMA_RX_1:
64 	case RX_CODEC_DMA_RX_0:
65 	case RX_CODEC_DMA_RX_1:
66 	case RX_CODEC_DMA_RX_2:
67 	case RX_CODEC_DMA_RX_3:
68 	case RX_CODEC_DMA_RX_4:
69 	case RX_CODEC_DMA_RX_5:
70 	case RX_CODEC_DMA_RX_6:
71 	case RX_CODEC_DMA_RX_7:
72 		/* rx */
73 		if (!rx_ch_mask) {
74 			dev_err(dai->dev, "rx slot not found\n");
75 			return -EINVAL;
76 		}
77 		if (rx_num > APM_PORT_MAX_AUDIO_CHAN_CNT) {
78 			dev_err(dai->dev, "invalid rx num %d\n",
79 				rx_num);
80 			return -EINVAL;
81 		}
82 		ch_mask = *rx_ch_mask;
83 
84 		break;
85 	default:
86 		dev_err(dai->dev, "%s: invalid dai id 0x%x\n",
87 			__func__, dai->id);
88 		return -EINVAL;
89 	}
90 
91 	cfg->active_channels_mask = ch_mask;
92 
93 	return 0;
94 }
95 
96 static int q6hdmi_hw_params(struct snd_pcm_substream *substream,
97 			    struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
98 {
99 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
100 	struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
101 	int channels = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max;
102 	int ret;
103 
104 	cfg->bit_width = params_width(params);
105 	cfg->sample_rate = params_rate(params);
106 	cfg->num_channels = channels;
107 
108 	switch (dai->id) {
109 	case DISPLAY_PORT_RX_0:
110 		cfg->dp_idx = 0;
111 		break;
112 	case DISPLAY_PORT_RX_1 ... DISPLAY_PORT_RX_7:
113 		cfg->dp_idx = dai->id - DISPLAY_PORT_RX_1 + 1;
114 		break;
115 	}
116 
117 	ret = q6dsp_get_channel_allocation(channels);
118 	if (ret < 0)
119 		return ret;
120 
121 	cfg->channel_allocation = ret;
122 
123 	return 0;
124 }
125 
126 static int q6dma_hw_params(struct snd_pcm_substream *substream,
127 			   struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
128 {
129 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
130 	struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
131 
132 	cfg->bit_width = params_width(params);
133 	cfg->sample_rate = params_rate(params);
134 	cfg->num_channels = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max;
135 
136 	return 0;
137 }
138 
139 static void q6apm_lpass_dai_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
140 {
141 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
142 	int rc;
143 
144 	if (dai_data->is_port_started[dai->id]) {
145 		rc = q6apm_graph_stop(dai_data->graph[dai->id]);
146 		dai_data->is_port_started[dai->id] = false;
147 		if (rc < 0)
148 			dev_err(dai->dev, "fail to close APM port (%d)\n", rc);
149 	}
150 
151 	if (dai_data->graph[dai->id]) {
152 		q6apm_graph_close(dai_data->graph[dai->id]);
153 		dai_data->graph[dai->id] = NULL;
154 	}
155 }
156 
157 static int q6apm_lpass_dai_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
158 {
159 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
160 	struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
161 	struct q6apm_graph *graph;
162 	int graph_id = dai->id;
163 	int rc;
164 
165 	if (dai_data->is_port_started[dai->id]) {
166 		q6apm_graph_stop(dai_data->graph[dai->id]);
167 		dai_data->is_port_started[dai->id] = false;
168 
169 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
170 			q6apm_graph_close(dai_data->graph[dai->id]);
171 			dai_data->graph[dai->id] = NULL;
172 		}
173 	}
174 
175 	/**
176 	 * It is recommend to load DSP with source graph first and then sink
177 	 * graph, so sequence for playback and capture will be different
178 	 */
179 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
180 		graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
181 		if (IS_ERR(graph)) {
182 			dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
183 			rc = PTR_ERR(graph);
184 			return rc;
185 		}
186 		dai_data->graph[graph_id] = graph;
187 	}
188 
189 	cfg->direction = substream->stream;
190 	rc = q6apm_graph_media_format_pcm(dai_data->graph[dai->id], cfg);
191 	if (rc) {
192 		dev_err(dai->dev, "Failed to set media format %d\n", rc);
193 		goto err;
194 	}
195 
196 	rc = q6apm_graph_prepare(dai_data->graph[dai->id]);
197 	if (rc) {
198 		dev_err(dai->dev, "Failed to prepare Graph %d\n", rc);
199 		goto err;
200 	}
201 
202 	rc = q6apm_graph_start(dai_data->graph[dai->id]);
203 	if (rc < 0) {
204 		dev_err(dai->dev, "fail to start APM port %x\n", dai->id);
205 		goto err;
206 	}
207 	dai_data->is_port_started[dai->id] = true;
208 
209 	return 0;
210 err:
211 	q6apm_graph_close(dai_data->graph[dai->id]);
212 	dai_data->graph[dai->id] = NULL;
213 	return rc;
214 }
215 
216 static int q6apm_lpass_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
217 {
218 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
219 	struct q6apm_graph *graph;
220 	int graph_id = dai->id;
221 
222 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
223 		graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
224 		if (IS_ERR(graph)) {
225 			dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
226 			return PTR_ERR(graph);
227 		}
228 		dai_data->graph[graph_id] = graph;
229 	}
230 
231 	return 0;
232 }
233 
234 static int q6i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
235 {
236 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
237 	struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
238 
239 	cfg->fmt = fmt;
240 
241 	return 0;
242 }
243 
244 static const struct snd_soc_dai_ops q6dma_ops = {
245 	.prepare	= q6apm_lpass_dai_prepare,
246 	.startup	= q6apm_lpass_dai_startup,
247 	.shutdown	= q6apm_lpass_dai_shutdown,
248 	.set_channel_map  = q6dma_set_channel_map,
249 	.hw_params        = q6dma_hw_params,
250 };
251 
252 static const struct snd_soc_dai_ops q6i2s_ops = {
253 	.prepare	= q6apm_lpass_dai_prepare,
254 	.startup	= q6apm_lpass_dai_startup,
255 	.shutdown	= q6apm_lpass_dai_shutdown,
256 	.set_channel_map  = q6dma_set_channel_map,
257 	.hw_params        = q6dma_hw_params,
258 };
259 
260 static const struct snd_soc_dai_ops q6hdmi_ops = {
261 	.prepare	= q6apm_lpass_dai_prepare,
262 	.startup	= q6apm_lpass_dai_startup,
263 	.shutdown	= q6apm_lpass_dai_shutdown,
264 	.hw_params	= q6hdmi_hw_params,
265 	.set_fmt	= q6i2s_set_fmt,
266 };
267 
268 static const struct snd_soc_component_driver q6apm_lpass_dai_component = {
269 	.name = "q6apm-be-dai-component",
270 	.of_xlate_dai_name = q6dsp_audio_ports_of_xlate_dai_name,
271 	.be_pcm_base = AUDIOREACH_BE_PCM_BASE,
272 	.use_dai_pcm_id = true,
273 };
274 
275 static int q6apm_lpass_dai_dev_probe(struct platform_device *pdev)
276 {
277 	struct q6dsp_audio_port_dai_driver_config cfg;
278 	struct q6apm_lpass_dai_data *dai_data;
279 	struct snd_soc_dai_driver *dais;
280 	struct device *dev = &pdev->dev;
281 	int num_dais;
282 
283 	dai_data = devm_kzalloc(dev, sizeof(*dai_data), GFP_KERNEL);
284 	if (!dai_data)
285 		return -ENOMEM;
286 
287 	dev_set_drvdata(dev, dai_data);
288 
289 	memset(&cfg, 0, sizeof(cfg));
290 	cfg.q6i2s_ops = &q6i2s_ops;
291 	cfg.q6dma_ops = &q6dma_ops;
292 	cfg.q6hdmi_ops = &q6hdmi_ops;
293 	dais = q6dsp_audio_ports_set_config(dev, &cfg, &num_dais);
294 
295 	return devm_snd_soc_register_component(dev, &q6apm_lpass_dai_component, dais, num_dais);
296 }
297 
298 #ifdef CONFIG_OF
299 static const struct of_device_id q6apm_lpass_dai_device_id[] = {
300 	{ .compatible = "qcom,q6apm-lpass-dais" },
301 	{},
302 };
303 MODULE_DEVICE_TABLE(of, q6apm_lpass_dai_device_id);
304 #endif
305 
306 static struct platform_driver q6apm_lpass_dai_platform_driver = {
307 	.driver = {
308 		.name = "q6apm-lpass-dais",
309 		.of_match_table = of_match_ptr(q6apm_lpass_dai_device_id),
310 	},
311 	.probe = q6apm_lpass_dai_dev_probe,
312 };
313 module_platform_driver(q6apm_lpass_dai_platform_driver);
314 
315 MODULE_DESCRIPTION("AUDIOREACH APM LPASS dai driver");
316 MODULE_LICENSE("GPL");
317