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 
q6dma_set_channel_map(struct snd_soc_dai * dai,unsigned int tx_num,unsigned int * tx_ch_mask,unsigned int rx_num,unsigned int * rx_ch_mask)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 
q6dma_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)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 
q6apm_lpass_dai_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)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 
q6apm_lpass_dai_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)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 	/**
131 	 * It is recommend to load DSP with source graph first and then sink
132 	 * graph, so sequence for playback and capture will be different
133 	 */
134 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
135 		graph = q6apm_graph_open(dai->dev, NULL, dai->dev, graph_id);
136 		if (IS_ERR(graph)) {
137 			dev_err(dai->dev, "Failed to open graph (%d)\n", graph_id);
138 			rc = PTR_ERR(graph);
139 			return rc;
140 		}
141 		dai_data->graph[graph_id] = graph;
142 	}
143 
144 	cfg->direction = substream->stream;
145 	rc = q6apm_graph_media_format_pcm(dai_data->graph[dai->id], cfg);
146 
147 	if (rc) {
148 		dev_err(dai->dev, "Failed to set media format %d\n", rc);
149 		return rc;
150 	}
151 
152 	rc = q6apm_graph_prepare(dai_data->graph[dai->id]);
153 	if (rc) {
154 		dev_err(dai->dev, "Failed to prepare Graph %d\n", rc);
155 		return rc;
156 	}
157 
158 	rc = q6apm_graph_start(dai_data->graph[dai->id]);
159 	if (rc < 0) {
160 		dev_err(dai->dev, "fail to start APM port %x\n", dai->id);
161 		return rc;
162 	}
163 	dai_data->is_port_started[dai->id] = true;
164 
165 	return 0;
166 }
167 
q6apm_lpass_dai_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)168 static int q6apm_lpass_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
169 {
170 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
171 	struct q6apm_graph *graph;
172 	int graph_id = dai->id;
173 
174 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
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 			return PTR_ERR(graph);
179 		}
180 		dai_data->graph[graph_id] = graph;
181 	}
182 
183 	return 0;
184 }
185 
q6i2s_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)186 static int q6i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
187 {
188 	struct q6apm_lpass_dai_data *dai_data = dev_get_drvdata(dai->dev);
189 	struct audioreach_module_config *cfg = &dai_data->module_config[dai->id];
190 
191 	cfg->fmt = fmt;
192 
193 	return 0;
194 }
195 
196 static const struct snd_soc_dai_ops q6dma_ops = {
197 	.prepare	= q6apm_lpass_dai_prepare,
198 	.startup	= q6apm_lpass_dai_startup,
199 	.shutdown	= q6apm_lpass_dai_shutdown,
200 	.set_channel_map  = q6dma_set_channel_map,
201 	.hw_params        = q6dma_hw_params,
202 };
203 
204 static const struct snd_soc_dai_ops q6i2s_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 	.set_fmt	= q6i2s_set_fmt,
211 };
212 
213 static const struct snd_soc_component_driver q6apm_lpass_dai_component = {
214 	.name = "q6apm-be-dai-component",
215 	.of_xlate_dai_name = q6dsp_audio_ports_of_xlate_dai_name,
216 	.be_pcm_base = AUDIOREACH_BE_PCM_BASE,
217 	.use_dai_pcm_id = true,
218 };
219 
q6apm_lpass_dai_dev_probe(struct platform_device * pdev)220 static int q6apm_lpass_dai_dev_probe(struct platform_device *pdev)
221 {
222 	struct q6dsp_audio_port_dai_driver_config cfg;
223 	struct q6apm_lpass_dai_data *dai_data;
224 	struct snd_soc_dai_driver *dais;
225 	struct device *dev = &pdev->dev;
226 	int num_dais;
227 
228 	dai_data = devm_kzalloc(dev, sizeof(*dai_data), GFP_KERNEL);
229 	if (!dai_data)
230 		return -ENOMEM;
231 
232 	dev_set_drvdata(dev, dai_data);
233 
234 	memset(&cfg, 0, sizeof(cfg));
235 	cfg.q6i2s_ops = &q6i2s_ops;
236 	cfg.q6dma_ops = &q6dma_ops;
237 	dais = q6dsp_audio_ports_set_config(dev, &cfg, &num_dais);
238 
239 	return devm_snd_soc_register_component(dev, &q6apm_lpass_dai_component, dais, num_dais);
240 }
241 
242 #ifdef CONFIG_OF
243 static const struct of_device_id q6apm_lpass_dai_device_id[] = {
244 	{ .compatible = "qcom,q6apm-lpass-dais" },
245 	{},
246 };
247 MODULE_DEVICE_TABLE(of, q6apm_lpass_dai_device_id);
248 #endif
249 
250 static struct platform_driver q6apm_lpass_dai_platform_driver = {
251 	.driver = {
252 		.name = "q6apm-lpass-dais",
253 		.of_match_table = of_match_ptr(q6apm_lpass_dai_device_id),
254 	},
255 	.probe = q6apm_lpass_dai_dev_probe,
256 };
257 module_platform_driver(q6apm_lpass_dai_platform_driver);
258 
259 MODULE_DESCRIPTION("AUDIOREACH APM LPASS dai driver");
260 MODULE_LICENSE("GPL");
261