xref: /linux/sound/soc/qcom/qdsp6/audioreach.c (revision 2aa680df68062e4e0c356ec2aa7100c13654907b)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020, Linaro Limited
3 
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/soc/qcom/apr.h>
7 #include <sound/soc.h>
8 #include <sound/soc-dai.h>
9 #include <sound/pcm.h>
10 #include <sound/pcm_params.h>
11 #include <dt-bindings/soc/qcom,gpr.h>
12 #include "q6apm.h"
13 #include "audioreach.h"
14 
15 /* SubGraph Config */
16 struct apm_sub_graph_data {
17 	struct apm_sub_graph_cfg sub_graph_cfg;
18 	struct apm_prop_data perf_data;
19 	struct apm_sg_prop_id_perf_mode perf;
20 	struct apm_prop_data dir_data;
21 	struct apm_sg_prop_id_direction dir;
22 	struct apm_prop_data sid_data;
23 	struct apm_sg_prop_id_scenario_id sid;
24 
25 } __packed;
26 
27 #define APM_SUB_GRAPH_CFG_NPROP	3
28 
29 struct apm_sub_graph_params  {
30 	struct apm_module_param_data param_data;
31 	uint32_t num_sub_graphs;
32 	struct apm_sub_graph_data sg_cfg[];
33 } __packed;
34 
35 #define APM_SUB_GRAPH_PSIZE(p, n) ALIGN(struct_size(p, sg_cfg, n), 8)
36 
37 /* container config */
38 struct apm_container_obj  {
39 	struct apm_container_cfg container_cfg;
40 	/* Capability ID list */
41 	struct apm_prop_data cap_data;
42 	uint32_t num_capability_id;
43 	uint32_t capability_id;
44 
45 	/* Container graph Position */
46 	struct apm_prop_data pos_data;
47 	struct apm_cont_prop_id_graph_pos pos;
48 
49 	/* Container Stack size */
50 	struct apm_prop_data stack_data;
51 	struct apm_cont_prop_id_stack_size stack;
52 
53 	/* Container proc domain id */
54 	struct apm_prop_data domain_data;
55 	struct apm_cont_prop_id_domain domain;
56 } __packed;
57 
58 struct apm_container_params  {
59 	struct apm_module_param_data param_data;
60 	uint32_t num_containers;
61 	struct apm_container_obj cont_obj[];
62 } __packed;
63 
64 #define APM_CONTAINER_PSIZE(p, n) ALIGN(struct_size(p, cont_obj, n), 8)
65 
66 /* Module List config */
67 struct apm_mod_list_obj {
68 	/* Modules list cfg */
69 	uint32_t sub_graph_id;
70 	uint32_t container_id;
71 	uint32_t num_modules;
72 	struct apm_module_obj mod_cfg[];
73 } __packed;
74 
75 #define APM_MOD_LIST_OBJ_PSIZE(p, n) struct_size(p, mod_cfg, n)
76 
77 struct apm_module_list_params {
78 	struct apm_module_param_data param_data;
79 	uint32_t num_modules_list;
80 	/* Module list config array */
81 	struct apm_mod_list_obj mod_list_obj[];
82 } __packed;
83 
84 
85 /* Module Properties */
86 struct apm_mod_prop_obj {
87 	u32 instance_id;
88 	u32 num_props;
89 	struct apm_prop_data prop_data_1;
90 	struct apm_module_prop_id_port_info prop_id_port;
91 } __packed;
92 
93 struct apm_prop_list_params {
94 	struct apm_module_param_data param_data;
95 	u32 num_modules_prop_cfg;
96 	struct apm_mod_prop_obj mod_prop_obj[];
97 
98 } __packed;
99 
100 #define APM_MOD_PROP_PSIZE(p, n) ALIGN(struct_size(p, mod_prop_obj, n), 8)
101 
102 /* Module Connections */
103 struct apm_mod_conn_list_params {
104 	struct apm_module_param_data param_data;
105 	u32 num_connections;
106 	struct apm_module_conn_obj conn_obj[];
107 
108 } __packed;
109 
110 #define APM_MOD_CONN_PSIZE(p, n) ALIGN(struct_size(p, conn_obj, n), 8)
111 
112 struct apm_graph_open_params {
113 	struct apm_cmd_header *cmd_header;
114 	struct apm_sub_graph_params *sg_data;
115 	struct apm_container_params *cont_data;
116 	struct apm_module_list_params *mod_list_data;
117 	struct apm_prop_list_params *mod_prop_data;
118 	struct apm_mod_conn_list_params *mod_conn_list_data;
119 } __packed;
120 
121 struct apm_pcm_module_media_fmt_cmd {
122 	struct apm_module_param_data param_data;
123 	struct param_id_pcm_output_format_cfg header;
124 	struct payload_pcm_output_format_cfg media_cfg;
125 } __packed;
126 
127 struct apm_rd_shmem_module_config_cmd {
128 	struct apm_module_param_data param_data;
129 	struct param_id_rd_sh_mem_cfg cfg;
130 } __packed;
131 
132 struct apm_sh_module_media_fmt_cmd {
133 	struct media_format header;
134 	struct payload_media_fmt_pcm cfg;
135 } __packed;
136 
137 #define APM_SHMEM_FMT_CFG_PSIZE(ch) ALIGN( \
138 				sizeof(struct apm_sh_module_media_fmt_cmd) + \
139 				ch * sizeof(uint8_t), 8)
140 
141 /* num of channels as argument */
142 #define APM_PCM_MODULE_FMT_CMD_PSIZE(ch) ALIGN( \
143 				sizeof(struct apm_pcm_module_media_fmt_cmd) + \
144 				ch * sizeof(uint8_t), 8)
145 
146 #define APM_PCM_OUT_FMT_CFG_PSIZE(p, n) ALIGN(struct_size(p, channel_mapping, n), 4)
147 
148 struct apm_i2s_module_intf_cfg {
149 	struct apm_module_param_data param_data;
150 	struct param_id_i2s_intf_cfg cfg;
151 } __packed;
152 
153 #define APM_I2S_INTF_CFG_PSIZE ALIGN(sizeof(struct apm_i2s_module_intf_cfg), 8)
154 
155 struct apm_module_hw_ep_mf_cfg {
156 	struct apm_module_param_data param_data;
157 	struct param_id_hw_ep_mf mf;
158 } __packed;
159 
160 #define APM_HW_EP_CFG_PSIZE ALIGN(sizeof(struct apm_module_hw_ep_mf_cfg), 8)
161 
162 #define APM_MFC_CFG_PSIZE(p, n) ALIGN(struct_size(p, channel_mapping, n), 4)
163 
164 struct apm_module_frame_size_factor_cfg {
165 	struct apm_module_param_data param_data;
166 	uint32_t frame_size_factor;
167 } __packed;
168 
169 #define APM_FS_CFG_PSIZE ALIGN(sizeof(struct apm_module_frame_size_factor_cfg), 8)
170 
171 struct apm_module_hw_ep_power_mode_cfg {
172 	struct apm_module_param_data param_data;
173 	struct param_id_hw_ep_power_mode_cfg power_mode;
174 } __packed;
175 
176 #define APM_HW_EP_PMODE_CFG_PSIZE ALIGN(sizeof(struct apm_module_hw_ep_power_mode_cfg),	8)
177 
178 struct apm_module_hw_ep_dma_data_align_cfg {
179 	struct apm_module_param_data param_data;
180 	struct param_id_hw_ep_dma_data_align align;
181 } __packed;
182 
183 #define APM_HW_EP_DALIGN_CFG_PSIZE ALIGN(sizeof(struct apm_module_hw_ep_dma_data_align_cfg), 8)
184 
185 struct apm_gain_module_cfg {
186 	struct apm_module_param_data param_data;
187 	struct param_id_gain_cfg gain_cfg;
188 } __packed;
189 
190 #define APM_GAIN_CFG_PSIZE ALIGN(sizeof(struct apm_gain_module_cfg), 8)
191 
192 struct apm_codec_dma_module_intf_cfg {
193 	struct apm_module_param_data param_data;
194 	struct param_id_codec_dma_intf_cfg cfg;
195 } __packed;
196 
197 #define APM_CDMA_INTF_CFG_PSIZE ALIGN(sizeof(struct apm_codec_dma_module_intf_cfg), 8)
198 
199 struct apm_display_port_module_intf_cfg {
200 	struct apm_module_param_data param_data;
201 	struct param_id_display_port_intf_cfg cfg;
202 } __packed;
203 #define APM_DP_INTF_CFG_PSIZE ALIGN(sizeof(struct apm_display_port_module_intf_cfg), 8)
204 
205 static void *__audioreach_alloc_pkt(int payload_size, uint32_t opcode, uint32_t token,
206 				    uint32_t src_port, uint32_t dest_port, bool has_cmd_hdr)
207 {
208 	struct gpr_pkt *pkt;
209 	void *p;
210 	int pkt_size = GPR_HDR_SIZE + payload_size;
211 
212 	if (has_cmd_hdr)
213 		pkt_size += APM_CMD_HDR_SIZE;
214 
215 	p = kzalloc(pkt_size, GFP_KERNEL);
216 	if (!p)
217 		return ERR_PTR(-ENOMEM);
218 
219 	pkt = p;
220 	pkt->hdr.version = GPR_PKT_VER;
221 	pkt->hdr.hdr_size = GPR_PKT_HEADER_WORD_SIZE;
222 	pkt->hdr.pkt_size = pkt_size;
223 	pkt->hdr.dest_port = dest_port;
224 	pkt->hdr.src_port = src_port;
225 
226 	pkt->hdr.dest_domain = GPR_DOMAIN_ID_ADSP;
227 	pkt->hdr.src_domain = GPR_DOMAIN_ID_APPS;
228 	pkt->hdr.token = token;
229 	pkt->hdr.opcode = opcode;
230 
231 	if (has_cmd_hdr) {
232 		struct apm_cmd_header *cmd_header;
233 
234 		p = p + GPR_HDR_SIZE;
235 		cmd_header = p;
236 		cmd_header->payload_size = payload_size;
237 	}
238 
239 	return pkt;
240 }
241 
242 void *audioreach_alloc_pkt(int payload_size, uint32_t opcode, uint32_t token,
243 			   uint32_t src_port, uint32_t dest_port)
244 {
245 	return __audioreach_alloc_pkt(payload_size, opcode, token, src_port, dest_port, false);
246 }
247 EXPORT_SYMBOL_GPL(audioreach_alloc_pkt);
248 
249 void *audioreach_alloc_apm_pkt(int pkt_size, uint32_t opcode, uint32_t token, uint32_t src_port)
250 {
251 	return __audioreach_alloc_pkt(pkt_size, opcode, token, src_port, APM_MODULE_INSTANCE_ID,
252 				      false);
253 }
254 EXPORT_SYMBOL_GPL(audioreach_alloc_apm_pkt);
255 
256 void *audioreach_alloc_cmd_pkt(int payload_size, uint32_t opcode, uint32_t token,
257 			       uint32_t src_port, uint32_t dest_port)
258 {
259 	return __audioreach_alloc_pkt(payload_size, opcode, token, src_port, dest_port, true);
260 }
261 EXPORT_SYMBOL_GPL(audioreach_alloc_cmd_pkt);
262 
263 void *audioreach_alloc_apm_cmd_pkt(int pkt_size, uint32_t opcode, uint32_t token)
264 {
265 	return __audioreach_alloc_pkt(pkt_size, opcode, token, GPR_APM_MODULE_IID,
266 				       APM_MODULE_INSTANCE_ID, true);
267 }
268 EXPORT_SYMBOL_GPL(audioreach_alloc_apm_cmd_pkt);
269 
270 void audioreach_set_default_channel_mapping(u8 *ch_map, int num_channels)
271 {
272 	if (num_channels == 1) {
273 		ch_map[0] =  PCM_CHANNEL_FL;
274 	} else if (num_channels == 2) {
275 		ch_map[0] =  PCM_CHANNEL_FL;
276 		ch_map[1] =  PCM_CHANNEL_FR;
277 	} else if (num_channels == 4) {
278 		ch_map[0] =  PCM_CHANNEL_FL;
279 		ch_map[1] =  PCM_CHANNEL_FR;
280 		ch_map[2] =  PCM_CHANNEL_LS;
281 		ch_map[3] =  PCM_CHANNEL_RS;
282 	}
283 }
284 EXPORT_SYMBOL_GPL(audioreach_set_default_channel_mapping);
285 
286 static void apm_populate_container_config(struct apm_container_obj *cfg,
287 					  struct audioreach_container *cont)
288 {
289 
290 	/* Container Config */
291 	cfg->container_cfg.container_id = cont->container_id;
292 	cfg->container_cfg.num_prop = 4;
293 
294 	/* Capability list */
295 	cfg->cap_data.prop_id = APM_CONTAINER_PROP_ID_CAPABILITY_LIST;
296 	cfg->cap_data.prop_size = APM_CONTAINER_PROP_ID_CAPABILITY_SIZE;
297 	cfg->num_capability_id = 1;
298 	cfg->capability_id = cont->capability_id;
299 
300 	/* Graph Position */
301 	cfg->pos_data.prop_id = APM_CONTAINER_PROP_ID_GRAPH_POS;
302 	cfg->pos_data.prop_size = sizeof(struct apm_cont_prop_id_graph_pos);
303 	cfg->pos.graph_pos = cont->graph_pos;
304 
305 	/* Stack size */
306 	cfg->stack_data.prop_id = APM_CONTAINER_PROP_ID_STACK_SIZE;
307 	cfg->stack_data.prop_size = sizeof(struct apm_cont_prop_id_stack_size);
308 	cfg->stack.stack_size = cont->stack_size;
309 
310 	/* Proc domain */
311 	cfg->domain_data.prop_id = APM_CONTAINER_PROP_ID_PROC_DOMAIN;
312 	cfg->domain_data.prop_size = sizeof(struct apm_cont_prop_id_domain);
313 	cfg->domain.proc_domain = cont->proc_domain;
314 }
315 
316 static void apm_populate_sub_graph_config(struct apm_sub_graph_data *cfg,
317 					  struct audioreach_sub_graph *sg)
318 {
319 	cfg->sub_graph_cfg.sub_graph_id = sg->sub_graph_id;
320 	cfg->sub_graph_cfg.num_sub_graph_prop = APM_SUB_GRAPH_CFG_NPROP;
321 
322 	/* Perf Mode */
323 	cfg->perf_data.prop_id = APM_SUB_GRAPH_PROP_ID_PERF_MODE;
324 	cfg->perf_data.prop_size = APM_SG_PROP_ID_PERF_MODE_SIZE;
325 	cfg->perf.perf_mode = sg->perf_mode;
326 
327 	/* Direction */
328 	cfg->dir_data.prop_id = APM_SUB_GRAPH_PROP_ID_DIRECTION;
329 	cfg->dir_data.prop_size = APM_SG_PROP_ID_DIR_SIZE;
330 	cfg->dir.direction = sg->direction;
331 
332 	/* Scenario ID */
333 	cfg->sid_data.prop_id = APM_SUB_GRAPH_PROP_ID_SCENARIO_ID;
334 	cfg->sid_data.prop_size = APM_SG_PROP_ID_SID_SIZE;
335 	cfg->sid.scenario_id = sg->scenario_id;
336 }
337 
338 static void apm_populate_module_prop_obj(struct apm_mod_prop_obj *obj,
339 					 struct audioreach_module *module)
340 {
341 
342 	obj->instance_id = module->instance_id;
343 	obj->num_props = 1;
344 	obj->prop_data_1.prop_id = APM_MODULE_PROP_ID_PORT_INFO;
345 	obj->prop_data_1.prop_size = APM_MODULE_PROP_ID_PORT_INFO_SZ;
346 	obj->prop_id_port.max_ip_port = module->max_ip_port;
347 	obj->prop_id_port.max_op_port = module->max_op_port;
348 }
349 
350 static void apm_populate_module_list_obj(struct apm_mod_list_obj *obj,
351 					 struct audioreach_container *container,
352 					 int sub_graph_id)
353 {
354 	struct audioreach_module *module;
355 	int i;
356 
357 	obj->sub_graph_id = sub_graph_id;
358 	obj->container_id = container->container_id;
359 	obj->num_modules = container->num_modules;
360 	i = 0;
361 	list_for_each_entry(module, &container->modules_list, node) {
362 		obj->mod_cfg[i].module_id = module->module_id;
363 		obj->mod_cfg[i].instance_id = module->instance_id;
364 		i++;
365 	}
366 }
367 
368 static void audioreach_populate_graph(struct q6apm *apm, struct audioreach_graph_info *info,
369 				      struct apm_graph_open_params *open,
370 				      struct list_head *sg_list,
371 				      int num_sub_graphs)
372 {
373 	struct apm_mod_conn_list_params *mc_data = open->mod_conn_list_data;
374 	struct apm_module_list_params *ml_data = open->mod_list_data;
375 	struct apm_prop_list_params *mp_data = open->mod_prop_data;
376 	struct apm_container_params *c_data = open->cont_data;
377 	struct apm_sub_graph_params *sg_data = open->sg_data;
378 	int ncontainer = 0, nmodule = 0, nconn = 0;
379 	struct apm_mod_prop_obj *module_prop_obj;
380 	struct audioreach_container *container;
381 	struct apm_module_conn_obj *conn_obj;
382 	struct audioreach_module *module;
383 	struct audioreach_sub_graph *sg;
384 	struct apm_container_obj *cobj;
385 	struct apm_mod_list_obj *mlobj;
386 	int i = 0;
387 
388 	mlobj = &ml_data->mod_list_obj[0];
389 
390 
391 	if (info->dst_mod_inst_id && info->src_mod_inst_id) {
392 		conn_obj = &mc_data->conn_obj[nconn];
393 		conn_obj->src_mod_inst_id = info->src_mod_inst_id;
394 		conn_obj->src_mod_op_port_id = info->src_mod_op_port_id;
395 		conn_obj->dst_mod_inst_id = info->dst_mod_inst_id;
396 		conn_obj->dst_mod_ip_port_id = info->dst_mod_ip_port_id;
397 		nconn++;
398 	}
399 
400 	list_for_each_entry(sg, sg_list, node) {
401 		struct apm_sub_graph_data *sg_cfg = &sg_data->sg_cfg[i++];
402 
403 		apm_populate_sub_graph_config(sg_cfg, sg);
404 
405 		list_for_each_entry(container, &sg->container_list, node) {
406 			cobj = &c_data->cont_obj[ncontainer];
407 
408 			apm_populate_container_config(cobj, container);
409 			apm_populate_module_list_obj(mlobj, container, sg->sub_graph_id);
410 
411 			list_for_each_entry(module, &container->modules_list, node) {
412 				int pn;
413 
414 				module_prop_obj = &mp_data->mod_prop_obj[nmodule++];
415 				apm_populate_module_prop_obj(module_prop_obj, module);
416 
417 				if (!module->max_op_port)
418 					continue;
419 
420 				for (pn = 0; pn < module->max_op_port; pn++) {
421 					if (module->dst_mod_inst_id[pn]) {
422 						conn_obj = &mc_data->conn_obj[nconn];
423 						conn_obj->src_mod_inst_id = module->instance_id;
424 						conn_obj->src_mod_op_port_id =
425 								module->src_mod_op_port_id[pn];
426 						conn_obj->dst_mod_inst_id =
427 								module->dst_mod_inst_id[pn];
428 						conn_obj->dst_mod_ip_port_id =
429 								module->dst_mod_ip_port_id[pn];
430 						nconn++;
431 					}
432 				}
433 			}
434 			mlobj = (void *) mlobj + APM_MOD_LIST_OBJ_PSIZE(mlobj,
435 									container->num_modules);
436 
437 			ncontainer++;
438 		}
439 	}
440 }
441 
442 void *audioreach_alloc_graph_pkt(struct q6apm *apm, struct audioreach_graph_info *info)
443 {
444 	int payload_size, sg_sz, cont_sz, ml_sz, mp_sz, mc_sz;
445 	struct apm_module_param_data  *param_data;
446 	struct apm_container_params *cont_params;
447 	struct audioreach_container *container;
448 	struct apm_sub_graph_params *sg_params;
449 	struct apm_mod_conn_list_params *mcon;
450 	struct apm_graph_open_params params;
451 	struct apm_prop_list_params *mprop;
452 	struct audioreach_module *module;
453 	struct audioreach_sub_graph *sgs;
454 	struct apm_mod_list_obj *mlobj;
455 	struct list_head *sg_list;
456 	int num_connections = 0;
457 	int num_containers = 0;
458 	int num_sub_graphs = 0;
459 	int num_modules = 0;
460 	int num_modules_list;
461 	struct gpr_pkt *pkt;
462 	void *p;
463 
464 	sg_list = &info->sg_list;
465 	ml_sz = 0;
466 
467 	/* add FE-BE connections */
468 	if (info->dst_mod_inst_id && info->src_mod_inst_id)
469 		num_connections++;
470 
471 	list_for_each_entry(sgs, sg_list, node) {
472 		num_sub_graphs++;
473 		list_for_each_entry(container, &sgs->container_list, node) {
474 			num_containers++;
475 			num_modules += container->num_modules;
476 			ml_sz = ml_sz + sizeof(struct apm_module_list_params) +
477 				APM_MOD_LIST_OBJ_PSIZE(mlobj, container->num_modules);
478 
479 			list_for_each_entry(module, &container->modules_list, node) {
480 				num_connections += module->num_connections;
481 			}
482 		}
483 	}
484 
485 	num_modules_list = num_containers;
486 	sg_sz = APM_SUB_GRAPH_PSIZE(sg_params, num_sub_graphs);
487 	cont_sz = APM_CONTAINER_PSIZE(cont_params, num_containers);
488 
489 	ml_sz = ALIGN(ml_sz, 8);
490 
491 	mp_sz = APM_MOD_PROP_PSIZE(mprop, num_modules);
492 	mc_sz =	APM_MOD_CONN_PSIZE(mcon, num_connections);
493 
494 	payload_size = sg_sz + cont_sz + ml_sz + mp_sz + mc_sz;
495 	pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_GRAPH_OPEN, 0);
496 	if (IS_ERR(pkt))
497 		return pkt;
498 
499 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
500 
501 	/* SubGraph */
502 	params.sg_data = p;
503 	param_data = &params.sg_data->param_data;
504 	param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
505 	param_data->param_id = APM_PARAM_ID_SUB_GRAPH_CONFIG;
506 	param_data->param_size = sg_sz - APM_MODULE_PARAM_DATA_SIZE;
507 	params.sg_data->num_sub_graphs = num_sub_graphs;
508 	p += sg_sz;
509 
510 	/* Container */
511 	params.cont_data = p;
512 	param_data = &params.cont_data->param_data;
513 	param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
514 	param_data->param_id = APM_PARAM_ID_CONTAINER_CONFIG;
515 	param_data->param_size = cont_sz - APM_MODULE_PARAM_DATA_SIZE;
516 	params.cont_data->num_containers = num_containers;
517 	p += cont_sz;
518 
519 	/* Module List*/
520 	params.mod_list_data = p;
521 	param_data = &params.mod_list_data->param_data;
522 	param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
523 	param_data->param_id = APM_PARAM_ID_MODULE_LIST;
524 	param_data->param_size = ml_sz - APM_MODULE_PARAM_DATA_SIZE;
525 	params.mod_list_data->num_modules_list = num_modules_list;
526 	p += ml_sz;
527 
528 	/* Module Properties */
529 	params.mod_prop_data = p;
530 	param_data = &params.mod_prop_data->param_data;
531 	param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
532 	param_data->param_id = APM_PARAM_ID_MODULE_PROP;
533 	param_data->param_size = mp_sz - APM_MODULE_PARAM_DATA_SIZE;
534 	params.mod_prop_data->num_modules_prop_cfg = num_modules;
535 	p += mp_sz;
536 
537 	/* Module Connections */
538 	params.mod_conn_list_data = p;
539 	param_data = &params.mod_conn_list_data->param_data;
540 	param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
541 	param_data->param_id = APM_PARAM_ID_MODULE_CONN;
542 	param_data->param_size = mc_sz - APM_MODULE_PARAM_DATA_SIZE;
543 	params.mod_conn_list_data->num_connections = num_connections;
544 	p += mc_sz;
545 
546 	audioreach_populate_graph(apm, info, &params, sg_list, num_sub_graphs);
547 
548 	return pkt;
549 }
550 EXPORT_SYMBOL_GPL(audioreach_alloc_graph_pkt);
551 
552 int audioreach_send_cmd_sync(struct device *dev, gpr_device_t *gdev,
553 			     struct gpr_ibasic_rsp_result_t *result, struct mutex *cmd_lock,
554 			     gpr_port_t *port, wait_queue_head_t *cmd_wait,
555 			     struct gpr_pkt *pkt, uint32_t rsp_opcode)
556 {
557 
558 	struct gpr_hdr *hdr = &pkt->hdr;
559 	int rc;
560 
561 	mutex_lock(cmd_lock);
562 	result->opcode = 0;
563 	result->status = 0;
564 
565 	if (port)
566 		rc = gpr_send_port_pkt(port, pkt);
567 	else if (gdev)
568 		rc = gpr_send_pkt(gdev, pkt);
569 	else
570 		rc = -EINVAL;
571 
572 	if (rc < 0)
573 		goto err;
574 
575 	if (rsp_opcode)
576 		rc = wait_event_timeout(*cmd_wait, (result->opcode == hdr->opcode) ||
577 					(result->opcode == rsp_opcode),	5 * HZ);
578 	else
579 		rc = wait_event_timeout(*cmd_wait, (result->opcode == hdr->opcode), 5 * HZ);
580 
581 	if (!rc) {
582 		dev_err(dev, "CMD timeout for [%x] opcode\n", hdr->opcode);
583 		rc = -ETIMEDOUT;
584 	} else if (result->status > 0) {
585 		dev_err(dev, "DSP returned error[%x] %x\n", hdr->opcode, result->status);
586 		rc = -EINVAL;
587 	} else {
588 		/* DSP successfully finished the command */
589 		rc = 0;
590 	}
591 
592 err:
593 	mutex_unlock(cmd_lock);
594 	return rc;
595 }
596 EXPORT_SYMBOL_GPL(audioreach_send_cmd_sync);
597 
598 int audioreach_graph_send_cmd_sync(struct q6apm_graph *graph, struct gpr_pkt *pkt,
599 				   uint32_t rsp_opcode)
600 {
601 
602 	return audioreach_send_cmd_sync(graph->dev, NULL,  &graph->result, &graph->lock,
603 					graph->port, &graph->cmd_wait, pkt, rsp_opcode);
604 }
605 EXPORT_SYMBOL_GPL(audioreach_graph_send_cmd_sync);
606 
607 static int audioreach_display_port_set_media_format(struct q6apm_graph *graph,
608 						    struct audioreach_module *module,
609 						    struct audioreach_module_config *cfg)
610 {
611 	struct apm_display_port_module_intf_cfg *intf_cfg;
612 	struct apm_module_frame_size_factor_cfg *fs_cfg;
613 	struct apm_module_param_data *param_data;
614 	struct apm_module_hw_ep_mf_cfg *hw_cfg;
615 	int ic_sz = APM_DP_INTF_CFG_PSIZE;
616 	int ep_sz = APM_HW_EP_CFG_PSIZE;
617 	int fs_sz = APM_FS_CFG_PSIZE;
618 	int size = ic_sz + ep_sz + fs_sz;
619 	void *p;
620 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
621 	if (IS_ERR(pkt))
622 		return PTR_ERR(pkt);
623 
624 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
625 
626 	hw_cfg = p;
627 	param_data = &hw_cfg->param_data;
628 	param_data->module_instance_id = module->instance_id;
629 	param_data->error_code = 0;
630 	param_data->param_id = PARAM_ID_HW_EP_MF_CFG;
631 	param_data->param_size = ep_sz - APM_MODULE_PARAM_DATA_SIZE;
632 
633 	hw_cfg->mf.sample_rate = cfg->sample_rate;
634 	hw_cfg->mf.bit_width = cfg->bit_width;
635 	hw_cfg->mf.num_channels = cfg->num_channels;
636 	hw_cfg->mf.data_format = module->data_format;
637 	p += ep_sz;
638 
639 	fs_cfg = p;
640 	param_data = &fs_cfg->param_data;
641 	param_data->module_instance_id = module->instance_id;
642 	param_data->error_code = 0;
643 	param_data->param_id = PARAM_ID_HW_EP_FRAME_SIZE_FACTOR;
644 	param_data->param_size = fs_sz - APM_MODULE_PARAM_DATA_SIZE;
645 	fs_cfg->frame_size_factor = 1;
646 	p += fs_sz;
647 
648 	intf_cfg = p;
649 	param_data = &intf_cfg->param_data;
650 	param_data->module_instance_id = module->instance_id;
651 	param_data->error_code = 0;
652 	param_data->param_id = PARAM_ID_DISPLAY_PORT_INTF_CFG;
653 	param_data->param_size = ic_sz - APM_MODULE_PARAM_DATA_SIZE;
654 
655 	intf_cfg->cfg.channel_allocation = cfg->channel_allocation;
656 	intf_cfg->cfg.mst_idx = 0;
657 	intf_cfg->cfg.dptx_idx = cfg->dp_idx;
658 
659 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
660 }
661 
662 /* LPASS Codec DMA port Module Media Format Setup */
663 static int audioreach_codec_dma_set_media_format(struct q6apm_graph *graph,
664 						 struct audioreach_module *module,
665 						 struct audioreach_module_config *cfg)
666 {
667 	struct apm_codec_dma_module_intf_cfg *intf_cfg;
668 	struct apm_module_frame_size_factor_cfg *fs_cfg;
669 	struct apm_module_hw_ep_power_mode_cfg *pm_cfg;
670 	struct apm_module_param_data *param_data;
671 	struct apm_module_hw_ep_mf_cfg *hw_cfg;
672 	int ic_sz = APM_CDMA_INTF_CFG_PSIZE;
673 	int ep_sz = APM_HW_EP_CFG_PSIZE;
674 	int fs_sz = APM_FS_CFG_PSIZE;
675 	int pm_sz = APM_HW_EP_PMODE_CFG_PSIZE;
676 	int size = ic_sz + ep_sz + fs_sz + pm_sz;
677 	void *p;
678 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
679 	if (IS_ERR(pkt))
680 		return PTR_ERR(pkt);
681 
682 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
683 
684 	hw_cfg = p;
685 	param_data = &hw_cfg->param_data;
686 	param_data->module_instance_id = module->instance_id;
687 	param_data->error_code = 0;
688 	param_data->param_id = PARAM_ID_HW_EP_MF_CFG;
689 	param_data->param_size = ep_sz - APM_MODULE_PARAM_DATA_SIZE;
690 
691 	hw_cfg->mf.sample_rate = cfg->sample_rate;
692 	hw_cfg->mf.bit_width = cfg->bit_width;
693 	hw_cfg->mf.num_channels = cfg->num_channels;
694 	hw_cfg->mf.data_format = module->data_format;
695 	p += ep_sz;
696 
697 	fs_cfg = p;
698 	param_data = &fs_cfg->param_data;
699 	param_data->module_instance_id = module->instance_id;
700 	param_data->error_code = 0;
701 	param_data->param_id = PARAM_ID_HW_EP_FRAME_SIZE_FACTOR;
702 	param_data->param_size = fs_sz - APM_MODULE_PARAM_DATA_SIZE;
703 	fs_cfg->frame_size_factor = 1;
704 	p += fs_sz;
705 
706 	intf_cfg = p;
707 	param_data = &intf_cfg->param_data;
708 	param_data->module_instance_id = module->instance_id;
709 	param_data->error_code = 0;
710 	param_data->param_id = PARAM_ID_CODEC_DMA_INTF_CFG;
711 	param_data->param_size = ic_sz - APM_MODULE_PARAM_DATA_SIZE;
712 
713 	intf_cfg->cfg.lpaif_type = module->hw_interface_type;
714 	intf_cfg->cfg.intf_index = module->hw_interface_idx;
715 	intf_cfg->cfg.active_channels_mask = (1 << cfg->num_channels) - 1;
716 	p += ic_sz;
717 
718 	pm_cfg = p;
719 	param_data = &pm_cfg->param_data;
720 	param_data->module_instance_id = module->instance_id;
721 	param_data->error_code = 0;
722 	param_data->param_id = PARAM_ID_HW_EP_POWER_MODE_CFG;
723 	param_data->param_size = pm_sz - APM_MODULE_PARAM_DATA_SIZE;
724 	pm_cfg->power_mode.power_mode = 0;
725 
726 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
727 }
728 
729 int audioreach_send_u32_param(struct q6apm_graph *graph, struct audioreach_module *module,
730 			      uint32_t param_id, uint32_t param_val)
731 {
732 	struct apm_module_param_data *param_data;
733 	struct gpr_pkt *pkt __free(kfree) = NULL;
734 	uint32_t *param;
735 	int payload_size = sizeof(uint32_t) + APM_MODULE_PARAM_DATA_SIZE;
736 	void *p = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
737 	if (IS_ERR(p))
738 		return -ENOMEM;
739 
740 	pkt = p;
741 	p = p + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
742 
743 	param_data = p;
744 	param_data->module_instance_id = module->instance_id;
745 	param_data->error_code = 0;
746 	param_data->param_id = param_id;
747 	param_data->param_size = sizeof(uint32_t);
748 
749 	p = p + APM_MODULE_PARAM_DATA_SIZE;
750 	param = p;
751 	*param = param_val;
752 
753 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
754 }
755 EXPORT_SYMBOL_GPL(audioreach_send_u32_param);
756 
757 static int audioreach_sal_limiter_enable(struct q6apm_graph *graph,
758 					 struct audioreach_module *module, bool enable)
759 {
760 	return audioreach_send_u32_param(graph, module, PARAM_ID_SAL_LIMITER_ENABLE, enable);
761 }
762 
763 static int audioreach_sal_set_media_format(struct q6apm_graph *graph,
764 					   struct audioreach_module *module,
765 					   struct audioreach_module_config *cfg)
766 {
767 	return audioreach_send_u32_param(graph, module, PARAM_ID_SAL_OUTPUT_CFG,  cfg->bit_width);
768 }
769 
770 static int audioreach_module_enable(struct q6apm_graph *graph,
771 				    struct audioreach_module *module,
772 				    bool enable)
773 {
774 	return audioreach_send_u32_param(graph, module, PARAM_ID_MODULE_ENABLE, enable);
775 }
776 
777 static int audioreach_gapless_set_media_format(struct q6apm_graph *graph,
778 					       struct audioreach_module *module,
779 					       struct audioreach_module_config *cfg)
780 {
781 	return audioreach_send_u32_param(graph, module, PARAM_ID_EARLY_EOS_DELAY,
782 					 EARLY_EOS_DELAY_MS);
783 }
784 
785 static int audioreach_set_module_config(struct q6apm_graph *graph,
786 					struct audioreach_module *module,
787 					struct audioreach_module_config *cfg)
788 {
789 	int size = le32_to_cpu(module->data->size);
790 	void *p;
791 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
792 	if (IS_ERR(pkt))
793 		return PTR_ERR(pkt);
794 
795 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
796 
797 	memcpy(p, module->data->data, size);
798 
799 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
800 }
801 
802 static int audioreach_mfc_set_media_format(struct q6apm_graph *graph,
803 					   struct audioreach_module *module,
804 					   struct audioreach_module_config *cfg)
805 {
806 	struct apm_module_param_data *param_data;
807 	struct param_id_mfc_media_format *media_format;
808 	uint32_t num_channels = cfg->num_channels;
809 	int payload_size = APM_MFC_CFG_PSIZE(media_format, num_channels) +
810 				APM_MODULE_PARAM_DATA_SIZE;
811 	int i;
812 	void *p;
813 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
814 	if (IS_ERR(pkt))
815 		return PTR_ERR(pkt);
816 
817 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
818 
819 	param_data = p;
820 	param_data->module_instance_id = module->instance_id;
821 	param_data->error_code = 0;
822 	param_data->param_id = PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
823 	param_data->param_size = APM_MFC_CFG_PSIZE(media_format, num_channels);
824 	p = p + APM_MODULE_PARAM_DATA_SIZE;
825 	media_format = p;
826 
827 	media_format->sample_rate = cfg->sample_rate;
828 	media_format->bit_width = cfg->bit_width;
829 	media_format->num_channels = cfg->num_channels;
830 	for (i = 0; i < num_channels; i++)
831 		media_format->channel_mapping[i] = cfg->channel_map[i];
832 
833 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
834 }
835 
836 static int audioreach_set_compr_media_format(struct media_format *media_fmt_hdr,
837 					     void *p, struct audioreach_module_config *mcfg)
838 {
839 	struct payload_media_fmt_aac_t *aac_cfg;
840 	struct payload_media_fmt_pcm *mp3_cfg;
841 	struct payload_media_fmt_flac_t *flac_cfg;
842 	struct payload_media_fmt_opus_t *opus_cfg;
843 
844 	switch (mcfg->fmt) {
845 	case SND_AUDIOCODEC_MP3:
846 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
847 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_MP3;
848 		media_fmt_hdr->payload_size = 0;
849 		p = p + sizeof(*media_fmt_hdr);
850 		mp3_cfg = p;
851 		mp3_cfg->sample_rate = mcfg->sample_rate;
852 		mp3_cfg->bit_width = mcfg->bit_width;
853 		mp3_cfg->alignment = PCM_LSB_ALIGNED;
854 		mp3_cfg->bits_per_sample = mcfg->bit_width;
855 		mp3_cfg->q_factor = mcfg->bit_width - 1;
856 		mp3_cfg->endianness = PCM_LITTLE_ENDIAN;
857 		mp3_cfg->num_channels = mcfg->num_channels;
858 		break;
859 	case SND_AUDIOCODEC_AAC:
860 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
861 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_AAC;
862 		media_fmt_hdr->payload_size = sizeof(struct payload_media_fmt_aac_t);
863 		p = p + sizeof(*media_fmt_hdr);
864 		aac_cfg = p;
865 		aac_cfg->aac_fmt_flag = 0;
866 		aac_cfg->audio_obj_type = 5;
867 		aac_cfg->num_channels = mcfg->num_channels;
868 		aac_cfg->total_size_of_PCE_bits = 0;
869 		aac_cfg->sample_rate = mcfg->sample_rate;
870 		break;
871 	case SND_AUDIOCODEC_FLAC:
872 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
873 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_FLAC;
874 		media_fmt_hdr->payload_size = sizeof(struct payload_media_fmt_flac_t);
875 		p = p + sizeof(*media_fmt_hdr);
876 		flac_cfg = p;
877 		flac_cfg->sample_size = mcfg->codec.options.flac_d.sample_size;
878 		flac_cfg->num_channels = mcfg->num_channels;
879 		flac_cfg->min_blk_size = mcfg->codec.options.flac_d.min_blk_size;
880 		flac_cfg->max_blk_size = mcfg->codec.options.flac_d.max_blk_size;
881 		flac_cfg->sample_rate = mcfg->sample_rate;
882 		flac_cfg->min_frame_size = mcfg->codec.options.flac_d.min_frame_size;
883 		flac_cfg->max_frame_size = mcfg->codec.options.flac_d.max_frame_size;
884 		break;
885 	case SND_AUDIOCODEC_OPUS_RAW:
886 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
887 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_OPUS;
888 		media_fmt_hdr->payload_size = sizeof(*opus_cfg);
889 		p = p + sizeof(*media_fmt_hdr);
890 		opus_cfg = p;
891 		/* raw opus packets prepended with 4 bytes of length */
892 		opus_cfg->bitstream_format = 1;
893 		/*
894 		 * payload_type:
895 		 * 0 -- read metadata from opus stream;
896 		 * 1 -- metadata is provided by filling in the struct here.
897 		 */
898 		opus_cfg->payload_type = 1;
899 		opus_cfg->version = mcfg->codec.options.opus_d.version;
900 		opus_cfg->num_channels = mcfg->codec.options.opus_d.num_channels;
901 		opus_cfg->pre_skip = mcfg->codec.options.opus_d.pre_skip;
902 		opus_cfg->sample_rate = mcfg->codec.options.opus_d.sample_rate;
903 		opus_cfg->output_gain = mcfg->codec.options.opus_d.output_gain;
904 		opus_cfg->mapping_family = mcfg->codec.options.opus_d.mapping_family;
905 		opus_cfg->stream_count = mcfg->codec.options.opus_d.chan_map.stream_count;
906 		opus_cfg->coupled_count = mcfg->codec.options.opus_d.chan_map.coupled_count;
907 		memcpy(opus_cfg->channel_mapping, mcfg->codec.options.opus_d.chan_map.channel_map,
908 		       sizeof(opus_cfg->channel_mapping));
909 		opus_cfg->reserved[0] = opus_cfg->reserved[1] = opus_cfg->reserved[2] = 0;
910 		break;
911 	default:
912 		return -EINVAL;
913 	}
914 
915 	return 0;
916 }
917 
918 int audioreach_compr_set_param(struct q6apm_graph *graph, struct audioreach_module_config *mcfg)
919 {
920 	struct media_format *header;
921 	int rc;
922 	void *p;
923 	int iid = q6apm_graph_get_rx_shmem_module_iid(graph);
924 	int payload_size = sizeof(struct apm_sh_module_media_fmt_cmd);
925 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_cmd_pkt(payload_size,
926 					DATA_CMD_WR_SH_MEM_EP_MEDIA_FORMAT,
927 					0, graph->port->id, iid);
928 	if (IS_ERR(pkt))
929 		return -ENOMEM;
930 
931 
932 	p = (void *)pkt + GPR_HDR_SIZE;
933 	header = p;
934 	rc = audioreach_set_compr_media_format(header, p, mcfg);
935 	if (rc)
936 		return rc;
937 
938 	return gpr_send_port_pkt(graph->port, pkt);
939 }
940 EXPORT_SYMBOL_GPL(audioreach_compr_set_param);
941 
942 static int audioreach_i2s_set_media_format(struct q6apm_graph *graph,
943 					   struct audioreach_module *module,
944 					   struct audioreach_module_config *cfg)
945 {
946 	struct apm_module_frame_size_factor_cfg *fs_cfg;
947 	struct apm_module_param_data *param_data;
948 	struct apm_i2s_module_intf_cfg *intf_cfg;
949 	struct apm_module_hw_ep_mf_cfg *hw_cfg;
950 	int ic_sz = APM_I2S_INTF_CFG_PSIZE;
951 	int ep_sz = APM_HW_EP_CFG_PSIZE;
952 	int fs_sz = APM_FS_CFG_PSIZE;
953 	int size = ic_sz + ep_sz + fs_sz;
954 	void *p;
955 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
956 	if (IS_ERR(pkt))
957 		return PTR_ERR(pkt);
958 
959 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
960 	intf_cfg = p;
961 
962 	param_data = &intf_cfg->param_data;
963 	param_data->module_instance_id = module->instance_id;
964 	param_data->error_code = 0;
965 	param_data->param_id = PARAM_ID_I2S_INTF_CFG;
966 	param_data->param_size = ic_sz - APM_MODULE_PARAM_DATA_SIZE;
967 
968 	intf_cfg->cfg.lpaif_type = module->hw_interface_type;
969 	intf_cfg->cfg.intf_idx = module->hw_interface_idx;
970 	intf_cfg->cfg.sd_line_idx = module->sd_line_idx;
971 
972 	switch (cfg->fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
973 	case SND_SOC_DAIFMT_BP_FP:
974 		intf_cfg->cfg.ws_src = CONFIG_I2S_WS_SRC_INTERNAL;
975 		break;
976 	case SND_SOC_DAIFMT_BC_FC:
977 		/* CPU is slave */
978 		intf_cfg->cfg.ws_src = CONFIG_I2S_WS_SRC_EXTERNAL;
979 		break;
980 	default:
981 		break;
982 	}
983 
984 	p += ic_sz;
985 	hw_cfg = p;
986 	param_data = &hw_cfg->param_data;
987 	param_data->module_instance_id = module->instance_id;
988 	param_data->error_code = 0;
989 	param_data->param_id = PARAM_ID_HW_EP_MF_CFG;
990 	param_data->param_size = ep_sz - APM_MODULE_PARAM_DATA_SIZE;
991 
992 	hw_cfg->mf.sample_rate = cfg->sample_rate;
993 	hw_cfg->mf.bit_width = cfg->bit_width;
994 	hw_cfg->mf.num_channels = cfg->num_channels;
995 	hw_cfg->mf.data_format = module->data_format;
996 
997 	p += ep_sz;
998 	fs_cfg = p;
999 	param_data = &fs_cfg->param_data;
1000 	param_data->module_instance_id = module->instance_id;
1001 	param_data->error_code = 0;
1002 	param_data->param_id = PARAM_ID_HW_EP_FRAME_SIZE_FACTOR;
1003 	param_data->param_size = fs_sz - APM_MODULE_PARAM_DATA_SIZE;
1004 	fs_cfg->frame_size_factor = 1;
1005 
1006 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1007 }
1008 
1009 static int audioreach_logging_set_media_format(struct q6apm_graph *graph,
1010 					       struct audioreach_module *module)
1011 {
1012 	struct apm_module_param_data *param_data;
1013 	struct data_logging_config *cfg;
1014 	int size = sizeof(*cfg) + APM_MODULE_PARAM_DATA_SIZE;
1015 	void *p;
1016 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1017 	if (IS_ERR(pkt))
1018 		return PTR_ERR(pkt);
1019 
1020 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1021 
1022 	param_data = p;
1023 	param_data->module_instance_id = module->instance_id;
1024 	param_data->error_code = 0;
1025 	param_data->param_id = PARAM_ID_DATA_LOGGING_CONFIG;
1026 	param_data->param_size = size - APM_MODULE_PARAM_DATA_SIZE;
1027 
1028 	p = p + APM_MODULE_PARAM_DATA_SIZE;
1029 	cfg = p;
1030 	cfg->log_code = module->log_code;
1031 	cfg->log_tap_point_id = module->log_tap_point_id;
1032 	cfg->mode = module->log_mode;
1033 
1034 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1035 }
1036 
1037 static int audioreach_pcm_set_media_format(struct q6apm_graph *graph,
1038 					   struct audioreach_module *module,
1039 					   struct audioreach_module_config *mcfg)
1040 {
1041 	struct payload_pcm_output_format_cfg *media_cfg;
1042 	uint32_t num_channels = mcfg->num_channels;
1043 	struct apm_pcm_module_media_fmt_cmd *cfg;
1044 	struct apm_module_param_data *param_data;
1045 	int payload_size;
1046 	struct gpr_pkt *pkt __free(kfree) = NULL;
1047 
1048 	if (num_channels > 4) {
1049 		dev_err(graph->dev, "Error: Invalid channels (%d)!\n", num_channels);
1050 		return -EINVAL;
1051 	}
1052 
1053 	payload_size = APM_PCM_MODULE_FMT_CMD_PSIZE(num_channels);
1054 
1055 	pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
1056 	if (IS_ERR(pkt))
1057 		return PTR_ERR(pkt);
1058 
1059 	cfg = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1060 
1061 	param_data = &cfg->param_data;
1062 	param_data->module_instance_id = module->instance_id;
1063 	param_data->error_code = 0;
1064 	param_data->param_id = PARAM_ID_PCM_OUTPUT_FORMAT_CFG;
1065 	param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
1066 
1067 	cfg->header.data_format = DATA_FORMAT_FIXED_POINT;
1068 	cfg->header.fmt_id = MEDIA_FMT_ID_PCM;
1069 	cfg->header.payload_size = APM_PCM_OUT_FMT_CFG_PSIZE(media_cfg, num_channels);
1070 
1071 	media_cfg = &cfg->media_cfg;
1072 	media_cfg->alignment = PCM_LSB_ALIGNED;
1073 	media_cfg->bit_width = mcfg->bit_width;
1074 	media_cfg->endianness = PCM_LITTLE_ENDIAN;
1075 	media_cfg->interleaved = module->interleave_type;
1076 	media_cfg->num_channels = mcfg->num_channels;
1077 	media_cfg->q_factor = mcfg->bit_width - 1;
1078 	media_cfg->bits_per_sample = mcfg->bit_width;
1079 	memcpy(media_cfg->channel_mapping, mcfg->channel_map, mcfg->num_channels);
1080 
1081 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1082 }
1083 
1084 static int audioreach_shmem_set_media_format(struct q6apm_graph *graph,
1085 					     struct audioreach_module *module,
1086 					     struct audioreach_module_config *mcfg)
1087 {
1088 	uint32_t num_channels = mcfg->num_channels;
1089 	struct apm_module_param_data *param_data;
1090 	struct payload_media_fmt_pcm *cfg;
1091 	struct media_format *header;
1092 	int rc, payload_size;
1093 	struct gpr_pkt *pkt __free(kfree) = NULL;
1094 	void *p;
1095 
1096 	if (num_channels > 4) {
1097 		dev_err(graph->dev, "Error: Invalid channels (%d)!\n", num_channels);
1098 		return -EINVAL;
1099 	}
1100 
1101 	payload_size = APM_SHMEM_FMT_CFG_PSIZE(num_channels) + APM_MODULE_PARAM_DATA_SIZE;
1102 
1103 	pkt = audioreach_alloc_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0,
1104 				     graph->port->id, module->instance_id);
1105 	if (IS_ERR(pkt))
1106 		return PTR_ERR(pkt);
1107 
1108 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1109 
1110 	param_data = p;
1111 	param_data->module_instance_id = module->instance_id;
1112 	param_data->error_code = 0;
1113 	param_data->param_id = PARAM_ID_MEDIA_FORMAT;
1114 	param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
1115 	p = p + APM_MODULE_PARAM_DATA_SIZE;
1116 
1117 	header = p;
1118 	if (mcfg->fmt == SND_AUDIOCODEC_PCM) {
1119 		header->data_format = DATA_FORMAT_FIXED_POINT;
1120 		header->fmt_id =  MEDIA_FMT_ID_PCM;
1121 		header->payload_size = payload_size - sizeof(*header);
1122 
1123 		p = p + sizeof(*header);
1124 		cfg = p;
1125 		cfg->sample_rate = mcfg->sample_rate;
1126 		cfg->bit_width = mcfg->bit_width;
1127 		cfg->alignment = PCM_LSB_ALIGNED;
1128 		cfg->bits_per_sample = mcfg->bit_width;
1129 		cfg->q_factor = mcfg->bit_width - 1;
1130 		cfg->endianness = PCM_LITTLE_ENDIAN;
1131 		cfg->num_channels = mcfg->num_channels;
1132 		memcpy(cfg->channel_mapping, mcfg->channel_map, mcfg->num_channels);
1133 	} else {
1134 		rc = audioreach_set_compr_media_format(header, p, mcfg);
1135 		if (rc)
1136 			return rc;
1137 	}
1138 
1139 	return audioreach_graph_send_cmd_sync(graph, pkt, 0);
1140 }
1141 
1142 int audioreach_gain_set_vol_ctrl(struct q6apm *apm, struct audioreach_module *module, int vol)
1143 {
1144 	struct param_id_vol_ctrl_master_gain *cfg;
1145 	struct apm_module_param_data *param_data;
1146 	int size = sizeof(*cfg) + APM_MODULE_PARAM_DATA_SIZE;
1147 	void *p;
1148 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1149 	if (IS_ERR(pkt))
1150 		return PTR_ERR(pkt);
1151 
1152 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1153 
1154 	param_data = p;
1155 	param_data->module_instance_id = module->instance_id;
1156 	param_data->error_code = 0;
1157 	param_data->param_id = PARAM_ID_VOL_CTRL_MASTER_GAIN;
1158 	param_data->param_size = size - APM_MODULE_PARAM_DATA_SIZE;
1159 
1160 	p = p + APM_MODULE_PARAM_DATA_SIZE;
1161 	cfg = p;
1162 	cfg->master_gain =  vol;
1163 	return q6apm_send_cmd_sync(apm, pkt, 0);
1164 }
1165 EXPORT_SYMBOL_GPL(audioreach_gain_set_vol_ctrl);
1166 
1167 static int audioreach_gain_set(struct q6apm_graph *graph, struct audioreach_module *module)
1168 {
1169 	struct apm_module_param_data *param_data;
1170 	struct apm_gain_module_cfg *cfg;
1171 	int size = APM_GAIN_CFG_PSIZE;
1172 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1173 	if (IS_ERR(pkt))
1174 		return PTR_ERR(pkt);
1175 
1176 	cfg = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1177 
1178 	param_data = &cfg->param_data;
1179 	param_data->module_instance_id = module->instance_id;
1180 	param_data->error_code = 0;
1181 	param_data->param_id = APM_PARAM_ID_GAIN;
1182 	param_data->param_size = size - APM_MODULE_PARAM_DATA_SIZE;
1183 
1184 	cfg->gain_cfg.gain = module->gain;
1185 
1186 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1187 }
1188 
1189 int audioreach_set_media_format(struct q6apm_graph *graph, struct audioreach_module *module,
1190 				struct audioreach_module_config *cfg)
1191 {
1192 	int rc;
1193 
1194 	switch (module->module_id) {
1195 	case MODULE_ID_DATA_LOGGING:
1196 		rc = audioreach_module_enable(graph, module, true);
1197 		if (!rc)
1198 			rc = audioreach_logging_set_media_format(graph, module);
1199 		break;
1200 	case MODULE_ID_PCM_DEC:
1201 	case MODULE_ID_PCM_ENC:
1202 	case MODULE_ID_PCM_CNV:
1203 	case MODULE_ID_PLACEHOLDER_DECODER:
1204 	case MODULE_ID_PLACEHOLDER_ENCODER:
1205 		rc = audioreach_pcm_set_media_format(graph, module, cfg);
1206 		break;
1207 	case MODULE_ID_DISPLAY_PORT_SINK:
1208 		rc = audioreach_display_port_set_media_format(graph, module, cfg);
1209 		break;
1210 	case  MODULE_ID_SMECNS_V2:
1211 		rc = audioreach_set_module_config(graph, module, cfg);
1212 		break;
1213 	case MODULE_ID_I2S_SOURCE:
1214 	case MODULE_ID_I2S_SINK:
1215 		rc = audioreach_i2s_set_media_format(graph, module, cfg);
1216 		break;
1217 	case MODULE_ID_WR_SHARED_MEM_EP:
1218 		rc = audioreach_shmem_set_media_format(graph, module, cfg);
1219 		break;
1220 	case MODULE_ID_GAIN:
1221 		rc = audioreach_gain_set(graph, module);
1222 		break;
1223 	case MODULE_ID_CODEC_DMA_SINK:
1224 	case MODULE_ID_CODEC_DMA_SOURCE:
1225 		rc = audioreach_codec_dma_set_media_format(graph, module, cfg);
1226 		break;
1227 	case MODULE_ID_SAL:
1228 		rc = audioreach_sal_set_media_format(graph, module, cfg);
1229 		if (!rc)
1230 			rc = audioreach_sal_limiter_enable(graph, module, true);
1231 		break;
1232 	case MODULE_ID_MFC:
1233 		rc = audioreach_mfc_set_media_format(graph, module, cfg);
1234 		break;
1235 	case MODULE_ID_GAPLESS:
1236 		rc = audioreach_gapless_set_media_format(graph, module, cfg);
1237 		break;
1238 	default:
1239 		rc = 0;
1240 	}
1241 
1242 	return rc;
1243 }
1244 EXPORT_SYMBOL_GPL(audioreach_set_media_format);
1245 
1246 void audioreach_graph_free_buf(struct q6apm_graph *graph)
1247 {
1248 	struct audioreach_graph_data *port;
1249 
1250 	mutex_lock(&graph->lock);
1251 	port = &graph->rx_data;
1252 	port->num_periods = 0;
1253 	kfree(port->buf);
1254 	port->buf = NULL;
1255 
1256 	port = &graph->tx_data;
1257 	port->num_periods = 0;
1258 	kfree(port->buf);
1259 	port->buf = NULL;
1260 	mutex_unlock(&graph->lock);
1261 }
1262 EXPORT_SYMBOL_GPL(audioreach_graph_free_buf);
1263 
1264 int audioreach_map_memory_regions(struct q6apm_graph *graph, unsigned int dir, size_t period_sz,
1265 				  unsigned int periods, bool is_contiguous)
1266 {
1267 	struct apm_shared_map_region_payload *mregions;
1268 	struct apm_cmd_shared_mem_map_regions *cmd;
1269 	uint32_t num_regions, buf_sz, payload_size;
1270 	struct audioreach_graph_data *data;
1271 	struct gpr_pkt *pkt __free(kfree) = NULL;
1272 	void *p;
1273 	int i;
1274 
1275 	if (dir == SNDRV_PCM_STREAM_PLAYBACK)
1276 		data = &graph->rx_data;
1277 	else
1278 		data = &graph->tx_data;
1279 
1280 	if (is_contiguous) {
1281 		num_regions = 1;
1282 		buf_sz = period_sz * periods;
1283 	} else {
1284 		buf_sz = period_sz;
1285 		num_regions = periods;
1286 	}
1287 
1288 	/* DSP expects size should be aligned to 4K */
1289 	buf_sz = ALIGN(buf_sz, 4096);
1290 
1291 	payload_size = sizeof(*cmd) + (sizeof(*mregions) * num_regions);
1292 
1293 	pkt = audioreach_alloc_apm_pkt(payload_size, APM_CMD_SHARED_MEM_MAP_REGIONS, dir,
1294 				     graph->port->id);
1295 	if (IS_ERR(pkt))
1296 		return PTR_ERR(pkt);
1297 
1298 	p = (void *)pkt + GPR_HDR_SIZE;
1299 	cmd = p;
1300 	cmd->mem_pool_id = APM_MEMORY_MAP_SHMEM8_4K_POOL;
1301 	cmd->num_regions = num_regions;
1302 
1303 	cmd->property_flag = 0x0;
1304 
1305 	mregions = p + sizeof(*cmd);
1306 
1307 	mutex_lock(&graph->lock);
1308 
1309 	for (i = 0; i < num_regions; i++) {
1310 		struct audio_buffer *ab;
1311 
1312 		ab = &data->buf[i];
1313 		mregions->shm_addr_lsw = lower_32_bits(ab->phys);
1314 		mregions->shm_addr_msw = upper_32_bits(ab->phys);
1315 		mregions->mem_size_bytes = buf_sz;
1316 		++mregions;
1317 	}
1318 	mutex_unlock(&graph->lock);
1319 
1320 	return audioreach_graph_send_cmd_sync(graph, pkt, APM_CMD_RSP_SHARED_MEM_MAP_REGIONS);
1321 }
1322 EXPORT_SYMBOL_GPL(audioreach_map_memory_regions);
1323 
1324 int audioreach_shared_memory_send_eos(struct q6apm_graph *graph)
1325 {
1326 	struct data_cmd_wr_sh_mem_ep_eos *eos;
1327 	int iid = q6apm_graph_get_rx_shmem_module_iid(graph);
1328 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_cmd_pkt(sizeof(*eos),
1329 					DATA_CMD_WR_SH_MEM_EP_EOS, 0, graph->port->id, iid);
1330 	if (IS_ERR(pkt))
1331 		return PTR_ERR(pkt);
1332 
1333 	eos = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1334 
1335 	eos->policy = WR_SH_MEM_EP_EOS_POLICY_LAST;
1336 
1337 	return gpr_send_port_pkt(graph->port, pkt);
1338 }
1339 EXPORT_SYMBOL_GPL(audioreach_shared_memory_send_eos);
1340