xref: /linux/sound/soc/qcom/qdsp6/q6apm-lpass-dais.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
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 		return;
146 	rc = q6apm_graph_stop(dai_data->graph[dai->id]);
147 	if (rc < 0)
148 		dev_err(dai->dev, "fail to close APM port (%d)\n", rc);
149 
150 	q6apm_graph_close(dai_data->graph[dai->id]);
151 	dai_data->is_port_started[dai->id] = false;
152 }
153 
154 static int q6apm_lpass_dai_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
155 {
156 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
157 	struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
158 	struct q6apm_graph *graph;
159 	int graph_id = dai->id;
160 	int rc;
161 
162 	if (dai_data->is_port_started[dai->id]) {
163 		q6apm_graph_stop(dai_data->graph[dai->id]);
164 		dai_data->is_port_started[dai->id] = false;
165 
166 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
167 			q6apm_graph_close(dai_data->graph[dai->id]);
168 	}
169 
170 	/**
171 	 * It is recommend to load DSP with source graph first and then sink
172 	 * graph, so sequence for playback and capture will be different
173 	 */
174 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
175 		graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
176 		if (IS_ERR(graph)) {
177 			dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
178 			rc = PTR_ERR(graph);
179 			return rc;
180 		}
181 		dai_data->graph[graph_id] = graph;
182 	}
183 
184 	cfg->direction = substream->stream;
185 	rc = q6apm_graph_media_format_pcm(dai_data->graph[dai->id], cfg);
186 
187 	if (rc) {
188 		dev_err(dai->dev, "Failed to set media format %d\n", rc);
189 		return rc;
190 	}
191 
192 	rc = q6apm_graph_prepare(dai_data->graph[dai->id]);
193 	if (rc) {
194 		dev_err(dai->dev, "Failed to prepare Graph %d\n", rc);
195 		return rc;
196 	}
197 
198 	rc = q6apm_graph_start(dai_data->graph[dai->id]);
199 	if (rc < 0) {
200 		dev_err(dai->dev, "fail to start APM port %x\n", dai->id);
201 		return rc;
202 	}
203 	dai_data->is_port_started[dai->id] = true;
204 
205 	return 0;
206 }
207 
208 static int q6apm_lpass_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
209 {
210 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
211 	struct q6apm_graph *graph;
212 	int graph_id = dai->id;
213 
214 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
215 		graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
216 		if (IS_ERR(graph)) {
217 			dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
218 			return PTR_ERR(graph);
219 		}
220 		dai_data->graph[graph_id] = graph;
221 	}
222 
223 	return 0;
224 }
225 
226 static int q6i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
227 {
228 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
229 	struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
230 
231 	cfg->fmt = fmt;
232 
233 	return 0;
234 }
235 
236 static const struct snd_soc_dai_ops q6dma_ops = {
237 	.prepare	= q6apm_lpass_dai_prepare,
238 	.startup	= q6apm_lpass_dai_startup,
239 	.shutdown	= q6apm_lpass_dai_shutdown,
240 	.set_channel_map  = q6dma_set_channel_map,
241 	.hw_params        = q6dma_hw_params,
242 };
243 
244 static const struct snd_soc_dai_ops q6i2s_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 q6hdmi_ops = {
253 	.prepare	= q6apm_lpass_dai_prepare,
254 	.startup	= q6apm_lpass_dai_startup,
255 	.shutdown	= q6apm_lpass_dai_shutdown,
256 	.hw_params	= q6hdmi_hw_params,
257 	.set_fmt	= q6i2s_set_fmt,
258 };
259 
260 static const struct snd_soc_component_driver q6apm_lpass_dai_component = {
261 	.name = "q6apm-be-dai-component",
262 	.of_xlate_dai_name = q6dsp_audio_ports_of_xlate_dai_name,
263 	.be_pcm_base = AUDIOREACH_BE_PCM_BASE,
264 	.use_dai_pcm_id = true,
265 };
266 
267 static int q6apm_lpass_dai_dev_probe(struct platform_device *pdev)
268 {
269 	struct q6dsp_audio_port_dai_driver_config cfg;
270 	struct q6apm_lpass_dai_data *dai_data;
271 	struct snd_soc_dai_driver *dais;
272 	struct device *dev = &pdev->dev;
273 	int num_dais;
274 
275 	dai_data = devm_kzalloc(dev, sizeof(*dai_data), GFP_KERNEL);
276 	if (!dai_data)
277 		return -ENOMEM;
278 
279 	dev_set_drvdata(dev, dai_data);
280 
281 	memset(&cfg, 0, sizeof(cfg));
282 	cfg.q6i2s_ops = &q6i2s_ops;
283 	cfg.q6dma_ops = &q6dma_ops;
284 	cfg.q6hdmi_ops = &q6hdmi_ops;
285 	dais = q6dsp_audio_ports_set_config(dev, &cfg, &num_dais);
286 
287 	return devm_snd_soc_register_component(dev, &q6apm_lpass_dai_component, dais, num_dais);
288 }
289 
290 #ifdef CONFIG_OF
291 static const struct of_device_id q6apm_lpass_dai_device_id[] = {
292 	{ .compatible = "qcom,q6apm-lpass-dais" },
293 	{},
294 };
295 MODULE_DEVICE_TABLE(of, q6apm_lpass_dai_device_id);
296 #endif
297 
298 static struct platform_driver q6apm_lpass_dai_platform_driver = {
299 	.driver = {
300 		.name = "q6apm-lpass-dais",
301 		.of_match_table = of_match_ptr(q6apm_lpass_dai_device_id),
302 	},
303 	.probe = q6apm_lpass_dai_dev_probe,
304 };
305 module_platform_driver(q6apm_lpass_dai_platform_driver);
306 
307 MODULE_DESCRIPTION("AUDIOREACH APM LPASS dai driver");
308 MODULE_LICENSE("GPL");
309