Merge tag 'drm-msm-fixes-2023-03-09' of https://gitlab.freedesktop.org/drm/msm into...
[platform/kernel/linux-starfive.git] / sound / soc / qcom / qdsp6 / q6apm-lpass-dais.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2021, Linaro Limited
3
4 #include <linux/err.h>
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"
15 #include "q6apm.h"
16
17 #define AUDIOREACH_BE_PCM_BASE  16
18
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];
23 };
24
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)
28 {
29
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];
32         int ch_mask;
33
34         switch (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:
47                 if (!tx_ch_mask) {
48                         dev_err(dai->dev, "tx slot not found\n");
49                         return -EINVAL;
50                 }
51
52                 if (tx_num > AR_PCM_MAX_NUM_CHANNEL) {
53                         dev_err(dai->dev, "invalid tx num %d\n",
54                                 tx_num);
55                         return -EINVAL;
56                 }
57                 ch_mask = *tx_ch_mask;
58
59                 break;
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:
70                 /* rx */
71                 if (!rx_ch_mask) {
72                         dev_err(dai->dev, "rx slot not found\n");
73                         return -EINVAL;
74                 }
75                 if (rx_num > APM_PORT_MAX_AUDIO_CHAN_CNT) {
76                         dev_err(dai->dev, "invalid rx num %d\n",
77                                 rx_num);
78                         return -EINVAL;
79                 }
80                 ch_mask = *rx_ch_mask;
81
82                 break;
83         default:
84                 dev_err(dai->dev, "%s: invalid dai id 0x%x\n",
85                         __func__, dai->id);
86                 return -EINVAL;
87         }
88
89         cfg->active_channels_mask = ch_mask;
90
91         return 0;
92 }
93
94 static int q6dma_hw_params(struct snd_pcm_substream *substream,
95                            struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
96 {
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];
99
100         cfg->bit_width = params_width(params);
101         cfg->sample_rate = params_rate(params);
102         cfg->num_channels = params_channels(params);
103
104         return 0;
105 }
106
107 static void q6apm_lpass_dai_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
108 {
109         struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
110         int rc;
111
112         if (!dai_data->is_port_started[dai->id])
113                 return;
114         rc = q6apm_graph_stop(dai_data->graph[dai->id]);
115         if (rc < 0)
116                 dev_err(dai->dev, "fail to close APM port (%d)\n", rc);
117
118         q6apm_graph_close(dai_data->graph[dai->id]);
119         dai_data->is_port_started[dai->id] = false;
120 }
121
122 static int q6apm_lpass_dai_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
123 {
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;
128         int rc;
129
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;
133         }
134
135         /**
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
138          */
139         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
140                 graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
141                 if (IS_ERR(graph)) {
142                         dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
143                         rc = PTR_ERR(graph);
144                         return rc;
145                 }
146                 dai_data->graph[graph_id] = graph;
147         }
148
149         cfg->direction = substream->stream;
150         rc = q6apm_graph_media_format_pcm(dai_data->graph[dai->id], cfg);
151
152         if (rc) {
153                 dev_err(dai->dev, "Failed to set media format %d\n", rc);
154                 return rc;
155         }
156
157         rc = q6apm_graph_prepare(dai_data->graph[dai->id]);
158         if (rc) {
159                 dev_err(dai->dev, "Failed to prepare Graph %d\n", rc);
160                 return rc;
161         }
162
163         rc = q6apm_graph_start(dai_data->graph[dai->id]);
164         if (rc < 0) {
165                 dev_err(dai->dev, "fail to start APM port %x\n", dai->id);
166                 return rc;
167         }
168         dai_data->is_port_started[dai->id] = true;
169
170         return 0;
171 }
172
173 static int q6apm_lpass_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
174 {
175         struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
176         struct q6apm_graph *graph;
177         int graph_id = dai->id;
178
179         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
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                         return PTR_ERR(graph);
184                 }
185                 dai_data->graph[graph_id] = graph;
186         }
187
188         return 0;
189 }
190
191 static int q6i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
192 {
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];
195
196         cfg->fmt = fmt;
197
198         return 0;
199 }
200
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,
207 };
208
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,
216 };
217
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,
223 };
224
225 static int q6apm_lpass_dai_dev_probe(struct platform_device *pdev)
226 {
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;
231         int num_dais;
232
233         dai_data = devm_kzalloc(dev, sizeof(*dai_data), GFP_KERNEL);
234         if (!dai_data)
235                 return -ENOMEM;
236
237         dev_set_drvdata(dev, dai_data);
238
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);
243
244         return devm_snd_soc_register_component(dev, &q6apm_lpass_dai_component, dais, num_dais);
245 }
246
247 #ifdef CONFIG_OF
248 static const struct of_device_id q6apm_lpass_dai_device_id[] = {
249         { .compatible = "qcom,q6apm-lpass-dais" },
250         {},
251 };
252 MODULE_DEVICE_TABLE(of, q6apm_lpass_dai_device_id);
253 #endif
254
255 static struct platform_driver q6apm_lpass_dai_platform_driver = {
256         .driver = {
257                 .name = "q6apm-lpass-dais",
258                 .of_match_table = of_match_ptr(q6apm_lpass_dai_device_id),
259         },
260         .probe = q6apm_lpass_dai_dev_probe,
261 };
262 module_platform_driver(q6apm_lpass_dai_platform_driver);
263
264 MODULE_DESCRIPTION("AUDIOREACH APM LPASS dai driver");
265 MODULE_LICENSE("GPL");