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