1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2021, Linaro Limited
5 #include <linux/init.h>
6 #include <linux/module.h>
7 #include <linux/device.h>
8 #include <linux/platform_device.h>
9 #include <linux/slab.h>
10 #include <sound/pcm.h>
11 #include <sound/soc.h>
12 #include <sound/pcm_params.h>
13 #include "q6dsp-lpass-ports.h"
14 #include "audioreach.h"
17 #define AUDIOREACH_BE_PCM_BASE 16
19 struct q6apm_lpass_dai_data {
20 struct q6apm_graph *graph[APM_PORT_MAX];
21 bool is_port_started[APM_PORT_MAX];
22 struct audioreach_module_config module_config[APM_PORT_MAX];
25 static int q6dma_set_channel_map(struct snd_soc_dai *dai,
26 unsigned int tx_num, unsigned int *tx_ch_mask,
27 unsigned int rx_num, unsigned int *rx_ch_mask)
30 struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
31 struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
35 case WSA_CODEC_DMA_TX_0:
36 case WSA_CODEC_DMA_TX_1:
37 case WSA_CODEC_DMA_TX_2:
38 case VA_CODEC_DMA_TX_0:
39 case VA_CODEC_DMA_TX_1:
40 case VA_CODEC_DMA_TX_2:
41 case TX_CODEC_DMA_TX_0:
42 case TX_CODEC_DMA_TX_1:
43 case TX_CODEC_DMA_TX_2:
44 case TX_CODEC_DMA_TX_3:
45 case TX_CODEC_DMA_TX_4:
46 case TX_CODEC_DMA_TX_5:
48 dev_err(dai->dev, "tx slot not found\n");
52 if (tx_num > AR_PCM_MAX_NUM_CHANNEL) {
53 dev_err(dai->dev, "invalid tx num %d\n",
57 ch_mask = *tx_ch_mask;
60 case WSA_CODEC_DMA_RX_0:
61 case WSA_CODEC_DMA_RX_1:
62 case RX_CODEC_DMA_RX_0:
63 case RX_CODEC_DMA_RX_1:
64 case RX_CODEC_DMA_RX_2:
65 case RX_CODEC_DMA_RX_3:
66 case RX_CODEC_DMA_RX_4:
67 case RX_CODEC_DMA_RX_5:
68 case RX_CODEC_DMA_RX_6:
69 case RX_CODEC_DMA_RX_7:
72 dev_err(dai->dev, "rx slot not found\n");
75 if (rx_num > APM_PORT_MAX_AUDIO_CHAN_CNT) {
76 dev_err(dai->dev, "invalid rx num %d\n",
80 ch_mask = *rx_ch_mask;
84 dev_err(dai->dev, "%s: invalid dai id 0x%x\n",
89 cfg->active_channels_mask = ch_mask;
94 static int q6dma_hw_params(struct snd_pcm_substream *substream,
95 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
97 struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
98 struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
100 cfg->bit_width = params_width(params);
101 cfg->sample_rate = params_rate(params);
102 cfg->num_channels = params_channels(params);
107 static void q6apm_lpass_dai_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
109 struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
112 if (!dai_data->is_port_started[dai->id])
114 rc = q6apm_graph_stop(dai_data->graph[dai->id]);
116 dev_err(dai->dev, "fail to close APM port (%d)\n", rc);
118 q6apm_graph_close(dai_data->graph[dai->id]);
119 dai_data->is_port_started[dai->id] = false;
122 static int q6apm_lpass_dai_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
124 struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
125 struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
126 struct q6apm_graph *graph;
127 int graph_id = dai->id;
130 if (dai_data->is_port_started[dai->id]) {
131 q6apm_graph_stop(dai_data->graph[dai->id]);
132 dai_data->is_port_started[dai->id] = false;
134 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
135 q6apm_graph_close(dai_data->graph[dai->id]);
139 * It is recommend to load DSP with source graph first and then sink
140 * graph, so sequence for playback and capture will be different
142 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
143 graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
145 dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
149 dai_data->graph[graph_id] = graph;
152 cfg->direction = substream->stream;
153 rc = q6apm_graph_media_format_pcm(dai_data->graph[dai->id], cfg);
156 dev_err(dai->dev, "Failed to set media format %d\n", rc);
160 rc = q6apm_graph_prepare(dai_data->graph[dai->id]);
162 dev_err(dai->dev, "Failed to prepare Graph %d\n", rc);
166 rc = q6apm_graph_start(dai_data->graph[dai->id]);
168 dev_err(dai->dev, "fail to start APM port %x\n", dai->id);
171 dai_data->is_port_started[dai->id] = true;
176 static int q6apm_lpass_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
178 struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
179 struct q6apm_graph *graph;
180 int graph_id = dai->id;
182 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
183 graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
185 dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
186 return PTR_ERR(graph);
188 dai_data->graph[graph_id] = graph;
194 static int q6i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
196 struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
197 struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
204 static const struct snd_soc_dai_ops q6dma_ops = {
205 .prepare = q6apm_lpass_dai_prepare,
206 .startup = q6apm_lpass_dai_startup,
207 .shutdown = q6apm_lpass_dai_shutdown,
208 .set_channel_map = q6dma_set_channel_map,
209 .hw_params = q6dma_hw_params,
212 static const struct snd_soc_dai_ops q6i2s_ops = {
213 .prepare = q6apm_lpass_dai_prepare,
214 .startup = q6apm_lpass_dai_startup,
215 .shutdown = q6apm_lpass_dai_shutdown,
216 .set_channel_map = q6dma_set_channel_map,
217 .hw_params = q6dma_hw_params,
218 .set_fmt = q6i2s_set_fmt,
221 static const struct snd_soc_component_driver q6apm_lpass_dai_component = {
222 .name = "q6apm-be-dai-component",
223 .of_xlate_dai_name = q6dsp_audio_ports_of_xlate_dai_name,
224 .be_pcm_base = AUDIOREACH_BE_PCM_BASE,
225 .use_dai_pcm_id = true,
228 static int q6apm_lpass_dai_dev_probe(struct platform_device *pdev)
230 struct q6dsp_audio_port_dai_driver_config cfg;
231 struct q6apm_lpass_dai_data *dai_data;
232 struct snd_soc_dai_driver *dais;
233 struct device *dev = &pdev->dev;
236 dai_data = devm_kzalloc(dev, sizeof(*dai_data), GFP_KERNEL);
240 dev_set_drvdata(dev, dai_data);
242 memset(&cfg, 0, sizeof(cfg));
243 cfg.q6i2s_ops = &q6i2s_ops;
244 cfg.q6dma_ops = &q6dma_ops;
245 dais = q6dsp_audio_ports_set_config(dev, &cfg, &num_dais);
247 return devm_snd_soc_register_component(dev, &q6apm_lpass_dai_component, dais, num_dais);
251 static const struct of_device_id q6apm_lpass_dai_device_id[] = {
252 { .compatible = "qcom,q6apm-lpass-dais" },
255 MODULE_DEVICE_TABLE(of, q6apm_lpass_dai_device_id);
258 static struct platform_driver q6apm_lpass_dai_platform_driver = {
260 .name = "q6apm-lpass-dais",
261 .of_match_table = of_match_ptr(q6apm_lpass_dai_device_id),
263 .probe = q6apm_lpass_dai_dev_probe,
265 module_platform_driver(q6apm_lpass_dai_platform_driver);
267 MODULE_DESCRIPTION("AUDIOREACH APM LPASS dai driver");
268 MODULE_LICENSE("GPL");