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;
136 * It is recommend to load DSP with source graph first and then sink
137 * graph, so sequence for playback and capture will be different
139 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
140 graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
142 dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
146 dai_data->graph[graph_id] = graph;
149 cfg->direction = substream->stream;
150 rc = q6apm_graph_media_format_pcm(dai_data->graph[dai->id], cfg);
153 dev_err(dai->dev, "Failed to set media format %d\n", rc);
157 rc = q6apm_graph_prepare(dai_data->graph[dai->id]);
159 dev_err(dai->dev, "Failed to prepare Graph %d\n", rc);
163 rc = q6apm_graph_start(dai_data->graph[dai->id]);
165 dev_err(dai->dev, "fail to start APM port %x\n", dai->id);
168 dai_data->is_port_started[dai->id] = true;
173 static int q6apm_lpass_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
175 struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
176 struct q6apm_graph *graph;
177 int graph_id = dai->id;
179 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
180 graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
182 dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
183 return PTR_ERR(graph);
185 dai_data->graph[graph_id] = graph;
191 static int q6i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
193 struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
194 struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
201 static const struct snd_soc_dai_ops q6dma_ops = {
202 .prepare = q6apm_lpass_dai_prepare,
203 .startup = q6apm_lpass_dai_startup,
204 .shutdown = q6apm_lpass_dai_shutdown,
205 .set_channel_map = q6dma_set_channel_map,
206 .hw_params = q6dma_hw_params,
209 static const struct snd_soc_dai_ops q6i2s_ops = {
210 .prepare = q6apm_lpass_dai_prepare,
211 .startup = q6apm_lpass_dai_startup,
212 .shutdown = q6apm_lpass_dai_shutdown,
213 .set_channel_map = q6dma_set_channel_map,
214 .hw_params = q6dma_hw_params,
215 .set_fmt = q6i2s_set_fmt,
218 static const struct snd_soc_component_driver q6apm_lpass_dai_component = {
219 .name = "q6apm-be-dai-component",
220 .of_xlate_dai_name = q6dsp_audio_ports_of_xlate_dai_name,
221 .be_pcm_base = AUDIOREACH_BE_PCM_BASE,
222 .use_dai_pcm_id = true,
225 static int q6apm_lpass_dai_dev_probe(struct platform_device *pdev)
227 struct q6dsp_audio_port_dai_driver_config cfg;
228 struct q6apm_lpass_dai_data *dai_data;
229 struct snd_soc_dai_driver *dais;
230 struct device *dev = &pdev->dev;
233 dai_data = devm_kzalloc(dev, sizeof(*dai_data), GFP_KERNEL);
237 dev_set_drvdata(dev, dai_data);
239 memset(&cfg, 0, sizeof(cfg));
240 cfg.q6i2s_ops = &q6i2s_ops;
241 cfg.q6dma_ops = &q6dma_ops;
242 dais = q6dsp_audio_ports_set_config(dev, &cfg, &num_dais);
244 return devm_snd_soc_register_component(dev, &q6apm_lpass_dai_component, dais, num_dais);
248 static const struct of_device_id q6apm_lpass_dai_device_id[] = {
249 { .compatible = "qcom,q6apm-lpass-dais" },
252 MODULE_DEVICE_TABLE(of, q6apm_lpass_dai_device_id);
255 static struct platform_driver q6apm_lpass_dai_platform_driver = {
257 .name = "q6apm-lpass-dais",
258 .of_match_table = of_match_ptr(q6apm_lpass_dai_device_id),
260 .probe = q6apm_lpass_dai_dev_probe,
262 module_platform_driver(q6apm_lpass_dai_platform_driver);
264 MODULE_DESCRIPTION("AUDIOREACH APM LPASS dai driver");
265 MODULE_LICENSE("GPL");