Merge tag 'objtool-core-2023-04-27' of git://git.kernel.org/pub/scm/linux/kernel...
[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                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
135                         q6apm_graph_close(dai_data->graph[dai->id]);
136         }
137
138         /**
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
141          */
142         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
143                 graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
144                 if (IS_ERR(graph)) {
145                         dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
146                         rc = PTR_ERR(graph);
147                         return rc;
148                 }
149                 dai_data->graph[graph_id] = graph;
150         }
151
152         cfg->direction = substream->stream;
153         rc = q6apm_graph_media_format_pcm(dai_data->graph[dai->id], cfg);
154
155         if (rc) {
156                 dev_err(dai->dev, "Failed to set media format %d\n", rc);
157                 return rc;
158         }
159
160         rc = q6apm_graph_prepare(dai_data->graph[dai->id]);
161         if (rc) {
162                 dev_err(dai->dev, "Failed to prepare Graph %d\n", rc);
163                 return rc;
164         }
165
166         rc = q6apm_graph_start(dai_data->graph[dai->id]);
167         if (rc < 0) {
168                 dev_err(dai->dev, "fail to start APM port %x\n", dai->id);
169                 return rc;
170         }
171         dai_data->is_port_started[dai->id] = true;
172
173         return 0;
174 }
175
176 static int q6apm_lpass_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
177 {
178         struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
179         struct q6apm_graph *graph;
180         int graph_id = dai->id;
181
182         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
183                 graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
184                 if (IS_ERR(graph)) {
185                         dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
186                         return PTR_ERR(graph);
187                 }
188                 dai_data->graph[graph_id] = graph;
189         }
190
191         return 0;
192 }
193
194 static int q6i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
195 {
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];
198
199         cfg->fmt = fmt;
200
201         return 0;
202 }
203
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,
210 };
211
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,
219 };
220
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,
226 };
227
228 static int q6apm_lpass_dai_dev_probe(struct platform_device *pdev)
229 {
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;
234         int num_dais;
235
236         dai_data = devm_kzalloc(dev, sizeof(*dai_data), GFP_KERNEL);
237         if (!dai_data)
238                 return -ENOMEM;
239
240         dev_set_drvdata(dev, dai_data);
241
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);
246
247         return devm_snd_soc_register_component(dev, &q6apm_lpass_dai_component, dais, num_dais);
248 }
249
250 #ifdef CONFIG_OF
251 static const struct of_device_id q6apm_lpass_dai_device_id[] = {
252         { .compatible = "qcom,q6apm-lpass-dais" },
253         {},
254 };
255 MODULE_DEVICE_TABLE(of, q6apm_lpass_dai_device_id);
256 #endif
257
258 static struct platform_driver q6apm_lpass_dai_platform_driver = {
259         .driver = {
260                 .name = "q6apm-lpass-dais",
261                 .of_match_table = of_match_ptr(q6apm_lpass_dai_device_id),
262         },
263         .probe = q6apm_lpass_dai_dev_probe,
264 };
265 module_platform_driver(q6apm_lpass_dai_platform_driver);
266
267 MODULE_DESCRIPTION("AUDIOREACH APM LPASS dai driver");
268 MODULE_LICENSE("GPL");