xref: /linux/sound/soc/qcom/qdsp6/audioreach.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
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_audio_if_module_intf_cfg {
156 	struct apm_module_param_data param_data;
157 	struct param_id_audio_if_intf_cfg cfg;
158 } __packed;
159 
160 #define APM_AUDIO_IF_INTF_CFG_PSIZE ALIGN(sizeof(struct apm_audio_if_module_intf_cfg), 8)
161 
162 struct apm_module_hw_ep_mf_cfg {
163 	struct apm_module_param_data param_data;
164 	struct param_id_hw_ep_mf mf;
165 } __packed;
166 
167 #define APM_HW_EP_CFG_PSIZE ALIGN(sizeof(struct apm_module_hw_ep_mf_cfg), 8)
168 
169 #define APM_MFC_CFG_PSIZE(p, n) ALIGN(struct_size(p, channel_mapping, n), 4)
170 
171 struct apm_module_frame_size_factor_cfg {
172 	struct apm_module_param_data param_data;
173 	uint32_t frame_size_factor;
174 } __packed;
175 
176 #define APM_FS_CFG_PSIZE ALIGN(sizeof(struct apm_module_frame_size_factor_cfg), 8)
177 
178 struct apm_module_hw_ep_frame_duration_cfg {
179 	struct apm_module_param_data param_data;
180 	struct param_id_hw_ep_frame_duration frame_duration;
181 } __packed;
182 
183 #define APM_HW_EP_FRAME_DURATION_PSIZE ALIGN(sizeof(struct apm_module_hw_ep_frame_duration_cfg), 8)
184 
185 struct apm_module_hw_ep_power_mode_cfg {
186 	struct apm_module_param_data param_data;
187 	struct param_id_hw_ep_power_mode_cfg power_mode;
188 } __packed;
189 
190 #define APM_HW_EP_PMODE_CFG_PSIZE ALIGN(sizeof(struct apm_module_hw_ep_power_mode_cfg),	8)
191 
192 struct apm_module_hw_ep_dma_data_align_cfg {
193 	struct apm_module_param_data param_data;
194 	struct param_id_hw_ep_dma_data_align align;
195 } __packed;
196 
197 #define APM_HW_EP_DALIGN_CFG_PSIZE ALIGN(sizeof(struct apm_module_hw_ep_dma_data_align_cfg), 8)
198 
199 struct apm_gain_module_cfg {
200 	struct apm_module_param_data param_data;
201 	struct param_id_gain_cfg gain_cfg;
202 } __packed;
203 
204 #define APM_GAIN_CFG_PSIZE ALIGN(sizeof(struct apm_gain_module_cfg), 8)
205 
206 struct apm_codec_dma_module_intf_cfg {
207 	struct apm_module_param_data param_data;
208 	struct param_id_codec_dma_intf_cfg cfg;
209 } __packed;
210 
211 #define APM_CDMA_INTF_CFG_PSIZE ALIGN(sizeof(struct apm_codec_dma_module_intf_cfg), 8)
212 
213 struct apm_display_port_module_intf_cfg {
214 	struct apm_module_param_data param_data;
215 	struct param_id_display_port_intf_cfg cfg;
216 } __packed;
217 #define APM_DP_INTF_CFG_PSIZE ALIGN(sizeof(struct apm_display_port_module_intf_cfg), 8)
218 
219 struct apm_module_sp_vi_op_mode_cfg {
220 	struct apm_module_param_data param_data;
221 	struct param_id_sp_vi_op_mode_cfg cfg;
222 } __packed;
223 
224 #define APM_SP_VI_OP_MODE_CFG_PSIZE(ch) ALIGN( \
225 				sizeof(struct apm_module_sp_vi_op_mode_cfg) + \
226 				(ch) * sizeof(uint32_t), 8)
227 
228 struct apm_module_sp_vi_ex_mode_cfg {
229 	struct apm_module_param_data param_data;
230 	struct param_id_sp_vi_ex_mode_cfg cfg;
231 } __packed;
232 
233 #define APM_SP_VI_EX_MODE_CFG_PSIZE ALIGN(sizeof(struct apm_module_sp_vi_ex_mode_cfg), 8)
234 
235 struct apm_module_sp_vi_channel_map_cfg {
236 	struct apm_module_param_data param_data;
237 	struct param_id_sp_vi_channel_map_cfg cfg;
238 } __packed;
239 
240 #define APM_SP_VI_CH_MAP_CFG_PSIZE(ch) ALIGN( \
241 				sizeof(struct apm_module_sp_vi_channel_map_cfg) + \
242 				(ch) * sizeof(uint32_t), 8)
243 
244 static void *__audioreach_alloc_pkt(int payload_size, uint32_t opcode, uint32_t token,
245 				    uint32_t src_port, uint32_t dest_port, bool has_cmd_hdr)
246 {
247 	struct gpr_pkt *pkt;
248 	void *p;
249 	int pkt_size = GPR_HDR_SIZE + payload_size;
250 
251 	if (has_cmd_hdr)
252 		pkt_size += APM_CMD_HDR_SIZE;
253 
254 	p = kzalloc(pkt_size, GFP_KERNEL);
255 	if (!p)
256 		return ERR_PTR(-ENOMEM);
257 
258 	pkt = p;
259 	pkt->hdr.version = GPR_PKT_VER;
260 	pkt->hdr.hdr_size = GPR_PKT_HEADER_WORD_SIZE;
261 	pkt->hdr.pkt_size = pkt_size;
262 	pkt->hdr.dest_port = dest_port;
263 	pkt->hdr.src_port = src_port;
264 
265 	pkt->hdr.dest_domain = GPR_DOMAIN_ID_ADSP;
266 	pkt->hdr.src_domain = GPR_DOMAIN_ID_APPS;
267 	pkt->hdr.token = token;
268 	pkt->hdr.opcode = opcode;
269 
270 	if (has_cmd_hdr) {
271 		struct apm_cmd_header *cmd_header;
272 
273 		p = p + GPR_HDR_SIZE;
274 		cmd_header = p;
275 		cmd_header->payload_size = payload_size;
276 	}
277 
278 	return pkt;
279 }
280 
281 void *audioreach_alloc_pkt(int payload_size, uint32_t opcode, uint32_t token,
282 			   uint32_t src_port, uint32_t dest_port)
283 {
284 	return __audioreach_alloc_pkt(payload_size, opcode, token, src_port, dest_port, false);
285 }
286 EXPORT_SYMBOL_GPL(audioreach_alloc_pkt);
287 
288 void *audioreach_alloc_apm_pkt(int pkt_size, uint32_t opcode, uint32_t token, uint32_t src_port)
289 {
290 	return __audioreach_alloc_pkt(pkt_size, opcode, token, src_port, APM_MODULE_INSTANCE_ID,
291 				      false);
292 }
293 EXPORT_SYMBOL_GPL(audioreach_alloc_apm_pkt);
294 
295 void *audioreach_alloc_cmd_pkt(int payload_size, uint32_t opcode, uint32_t token,
296 			       uint32_t src_port, uint32_t dest_port)
297 {
298 	return __audioreach_alloc_pkt(payload_size, opcode, token, src_port, dest_port, true);
299 }
300 EXPORT_SYMBOL_GPL(audioreach_alloc_cmd_pkt);
301 
302 void *audioreach_alloc_apm_cmd_pkt(int pkt_size, uint32_t opcode, uint32_t token)
303 {
304 	return __audioreach_alloc_pkt(pkt_size, opcode, token, GPR_APM_MODULE_IID,
305 				       APM_MODULE_INSTANCE_ID, true);
306 }
307 EXPORT_SYMBOL_GPL(audioreach_alloc_apm_cmd_pkt);
308 
309 void audioreach_set_default_channel_mapping(u8 *ch_map, int num_channels)
310 {
311 	if (num_channels == 1) {
312 		ch_map[0] =  PCM_CHANNEL_FL;
313 	} else if (num_channels == 2) {
314 		ch_map[0] =  PCM_CHANNEL_FL;
315 		ch_map[1] =  PCM_CHANNEL_FR;
316 	} else if (num_channels == 4) {
317 		ch_map[0] =  PCM_CHANNEL_FL;
318 		ch_map[1] =  PCM_CHANNEL_FR;
319 		ch_map[2] =  PCM_CHANNEL_LS;
320 		ch_map[3] =  PCM_CHANNEL_RS;
321 	}
322 }
323 EXPORT_SYMBOL_GPL(audioreach_set_default_channel_mapping);
324 
325 static void apm_populate_container_config(struct apm_container_obj *cfg,
326 					  const struct audioreach_container *cont)
327 {
328 
329 	/* Container Config */
330 	cfg->container_cfg.container_id = cont->container_id;
331 	cfg->container_cfg.num_prop = 4;
332 
333 	/* Capability list */
334 	cfg->cap_data.prop_id = APM_CONTAINER_PROP_ID_CAPABILITY_LIST;
335 	cfg->cap_data.prop_size = APM_CONTAINER_PROP_ID_CAPABILITY_SIZE;
336 	cfg->num_capability_id = 1;
337 	cfg->capability_id = cont->capability_id;
338 
339 	/* Graph Position */
340 	cfg->pos_data.prop_id = APM_CONTAINER_PROP_ID_GRAPH_POS;
341 	cfg->pos_data.prop_size = sizeof(struct apm_cont_prop_id_graph_pos);
342 	cfg->pos.graph_pos = cont->graph_pos;
343 
344 	/* Stack size */
345 	cfg->stack_data.prop_id = APM_CONTAINER_PROP_ID_STACK_SIZE;
346 	cfg->stack_data.prop_size = sizeof(struct apm_cont_prop_id_stack_size);
347 	cfg->stack.stack_size = cont->stack_size;
348 
349 	/* Proc domain */
350 	cfg->domain_data.prop_id = APM_CONTAINER_PROP_ID_PROC_DOMAIN;
351 	cfg->domain_data.prop_size = sizeof(struct apm_cont_prop_id_domain);
352 	cfg->domain.proc_domain = cont->proc_domain;
353 }
354 
355 static void apm_populate_sub_graph_config(struct apm_sub_graph_data *cfg,
356 					  const struct audioreach_sub_graph *sg)
357 {
358 	cfg->sub_graph_cfg.sub_graph_id = sg->sub_graph_id;
359 	cfg->sub_graph_cfg.num_sub_graph_prop = APM_SUB_GRAPH_CFG_NPROP;
360 
361 	/* Perf Mode */
362 	cfg->perf_data.prop_id = APM_SUB_GRAPH_PROP_ID_PERF_MODE;
363 	cfg->perf_data.prop_size = APM_SG_PROP_ID_PERF_MODE_SIZE;
364 	cfg->perf.perf_mode = sg->perf_mode;
365 
366 	/* Direction */
367 	cfg->dir_data.prop_id = APM_SUB_GRAPH_PROP_ID_DIRECTION;
368 	cfg->dir_data.prop_size = APM_SG_PROP_ID_DIR_SIZE;
369 	cfg->dir.direction = sg->direction;
370 
371 	/* Scenario ID */
372 	cfg->sid_data.prop_id = APM_SUB_GRAPH_PROP_ID_SCENARIO_ID;
373 	cfg->sid_data.prop_size = APM_SG_PROP_ID_SID_SIZE;
374 	cfg->sid.scenario_id = sg->scenario_id;
375 }
376 
377 static void apm_populate_module_prop_obj(struct apm_mod_prop_obj *obj,
378 					 const struct audioreach_module *module)
379 {
380 
381 	obj->instance_id = module->instance_id;
382 	obj->num_props = 1;
383 	obj->prop_data_1.prop_id = APM_MODULE_PROP_ID_PORT_INFO;
384 	obj->prop_data_1.prop_size = APM_MODULE_PROP_ID_PORT_INFO_SZ;
385 	obj->prop_id_port.max_ip_port = module->max_ip_port;
386 	obj->prop_id_port.max_op_port = module->max_op_port;
387 }
388 
389 static void apm_populate_module_list_obj(struct apm_mod_list_obj *obj,
390 					 const struct audioreach_container *container,
391 					 int sub_graph_id)
392 {
393 	struct audioreach_module *module;
394 	int i;
395 
396 	obj->sub_graph_id = sub_graph_id;
397 	obj->container_id = container->container_id;
398 	obj->num_modules = container->num_modules;
399 	i = 0;
400 	list_for_each_entry(module, &container->modules_list, node) {
401 		obj->mod_cfg[i].module_id = module->module_id;
402 		obj->mod_cfg[i].instance_id = module->instance_id;
403 		i++;
404 	}
405 }
406 
407 static void audioreach_populate_graph(struct q6apm *apm,
408 				      const struct audioreach_graph_info *info,
409 				      struct apm_graph_open_params *open,
410 				      const struct list_head *sg_list,
411 				      int num_sub_graphs)
412 {
413 	struct apm_mod_conn_list_params *mc_data = open->mod_conn_list_data;
414 	struct apm_module_list_params *ml_data = open->mod_list_data;
415 	struct apm_prop_list_params *mp_data = open->mod_prop_data;
416 	struct apm_container_params *c_data = open->cont_data;
417 	struct apm_sub_graph_params *sg_data = open->sg_data;
418 	int ncontainer = 0, nmodule = 0, nconn = 0;
419 	struct apm_mod_prop_obj *module_prop_obj;
420 	struct audioreach_container *container;
421 	struct apm_module_conn_obj *conn_obj;
422 	struct audioreach_module *module;
423 	struct audioreach_sub_graph *sg;
424 	struct apm_container_obj *cobj;
425 	struct apm_mod_list_obj *mlobj;
426 	int i = 0;
427 
428 	mlobj = &ml_data->mod_list_obj[0];
429 
430 
431 	if (info->dst_mod_inst_id && info->src_mod_inst_id) {
432 		conn_obj = &mc_data->conn_obj[nconn];
433 		conn_obj->src_mod_inst_id = info->src_mod_inst_id;
434 		conn_obj->src_mod_op_port_id = info->src_mod_op_port_id;
435 		conn_obj->dst_mod_inst_id = info->dst_mod_inst_id;
436 		conn_obj->dst_mod_ip_port_id = info->dst_mod_ip_port_id;
437 		nconn++;
438 	}
439 
440 	list_for_each_entry(sg, sg_list, node) {
441 		struct apm_sub_graph_data *sg_cfg = &sg_data->sg_cfg[i++];
442 
443 		apm_populate_sub_graph_config(sg_cfg, sg);
444 
445 		list_for_each_entry(container, &sg->container_list, node) {
446 			cobj = &c_data->cont_obj[ncontainer];
447 
448 			apm_populate_container_config(cobj, container);
449 			apm_populate_module_list_obj(mlobj, container, sg->sub_graph_id);
450 
451 			list_for_each_entry(module, &container->modules_list, node) {
452 				int pn;
453 
454 				module_prop_obj = &mp_data->mod_prop_obj[nmodule++];
455 				apm_populate_module_prop_obj(module_prop_obj, module);
456 
457 				if (!module->max_op_port)
458 					continue;
459 
460 				for (pn = 0; pn < module->max_op_port; pn++) {
461 					if (module->dst_mod_inst_id[pn]) {
462 						conn_obj = &mc_data->conn_obj[nconn];
463 						conn_obj->src_mod_inst_id = module->instance_id;
464 						conn_obj->src_mod_op_port_id =
465 								module->src_mod_op_port_id[pn];
466 						conn_obj->dst_mod_inst_id =
467 								module->dst_mod_inst_id[pn];
468 						conn_obj->dst_mod_ip_port_id =
469 								module->dst_mod_ip_port_id[pn];
470 						nconn++;
471 					}
472 				}
473 			}
474 			mlobj = (void *) mlobj + APM_MOD_LIST_OBJ_PSIZE(mlobj,
475 									container->num_modules);
476 
477 			ncontainer++;
478 		}
479 	}
480 }
481 
482 void *audioreach_alloc_graph_pkt(struct q6apm *apm,
483 				 const struct audioreach_graph_info *info)
484 {
485 	int payload_size, sg_sz, cont_sz, ml_sz, mp_sz, mc_sz;
486 	struct apm_module_param_data  *param_data;
487 	struct apm_container_params *cont_params;
488 	struct audioreach_container *container;
489 	struct apm_sub_graph_params *sg_params;
490 	struct apm_mod_conn_list_params *mcon;
491 	struct apm_graph_open_params params;
492 	struct apm_prop_list_params *mprop;
493 	struct audioreach_module *module;
494 	struct audioreach_sub_graph *sgs;
495 	struct apm_mod_list_obj *mlobj;
496 	const struct list_head *sg_list;
497 	int num_connections = 0;
498 	int num_containers = 0;
499 	int num_sub_graphs = 0;
500 	int num_modules = 0;
501 	int num_modules_list;
502 	struct gpr_pkt *pkt;
503 	void *p;
504 
505 	sg_list = &info->sg_list;
506 	ml_sz = 0;
507 
508 	/* add FE-BE connections */
509 	if (info->dst_mod_inst_id && info->src_mod_inst_id)
510 		num_connections++;
511 
512 	list_for_each_entry(sgs, sg_list, node) {
513 		num_sub_graphs++;
514 		list_for_each_entry(container, &sgs->container_list, node) {
515 			num_containers++;
516 			num_modules += container->num_modules;
517 			ml_sz = ml_sz + sizeof(struct apm_module_list_params) +
518 				APM_MOD_LIST_OBJ_PSIZE(mlobj, container->num_modules);
519 
520 			list_for_each_entry(module, &container->modules_list, node) {
521 				num_connections += module->num_connections;
522 			}
523 		}
524 	}
525 
526 	num_modules_list = num_containers;
527 	sg_sz = APM_SUB_GRAPH_PSIZE(sg_params, num_sub_graphs);
528 	cont_sz = APM_CONTAINER_PSIZE(cont_params, num_containers);
529 
530 	ml_sz = ALIGN(ml_sz, 8);
531 
532 	mp_sz = APM_MOD_PROP_PSIZE(mprop, num_modules);
533 	mc_sz =	APM_MOD_CONN_PSIZE(mcon, num_connections);
534 
535 	payload_size = sg_sz + cont_sz + ml_sz + mp_sz + mc_sz;
536 	pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_GRAPH_OPEN, 0);
537 	if (IS_ERR(pkt))
538 		return pkt;
539 
540 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
541 
542 	/* SubGraph */
543 	params.sg_data = p;
544 	param_data = &params.sg_data->param_data;
545 	param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
546 	param_data->param_id = APM_PARAM_ID_SUB_GRAPH_CONFIG;
547 	param_data->param_size = sg_sz - APM_MODULE_PARAM_DATA_SIZE;
548 	params.sg_data->num_sub_graphs = num_sub_graphs;
549 	p += sg_sz;
550 
551 	/* Container */
552 	params.cont_data = p;
553 	param_data = &params.cont_data->param_data;
554 	param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
555 	param_data->param_id = APM_PARAM_ID_CONTAINER_CONFIG;
556 	param_data->param_size = cont_sz - APM_MODULE_PARAM_DATA_SIZE;
557 	params.cont_data->num_containers = num_containers;
558 	p += cont_sz;
559 
560 	/* Module List*/
561 	params.mod_list_data = p;
562 	param_data = &params.mod_list_data->param_data;
563 	param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
564 	param_data->param_id = APM_PARAM_ID_MODULE_LIST;
565 	param_data->param_size = ml_sz - APM_MODULE_PARAM_DATA_SIZE;
566 	params.mod_list_data->num_modules_list = num_modules_list;
567 	p += ml_sz;
568 
569 	/* Module Properties */
570 	params.mod_prop_data = p;
571 	param_data = &params.mod_prop_data->param_data;
572 	param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
573 	param_data->param_id = APM_PARAM_ID_MODULE_PROP;
574 	param_data->param_size = mp_sz - APM_MODULE_PARAM_DATA_SIZE;
575 	params.mod_prop_data->num_modules_prop_cfg = num_modules;
576 	p += mp_sz;
577 
578 	/* Module Connections */
579 	params.mod_conn_list_data = p;
580 	param_data = &params.mod_conn_list_data->param_data;
581 	param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
582 	param_data->param_id = APM_PARAM_ID_MODULE_CONN;
583 	param_data->param_size = mc_sz - APM_MODULE_PARAM_DATA_SIZE;
584 	params.mod_conn_list_data->num_connections = num_connections;
585 	p += mc_sz;
586 
587 	audioreach_populate_graph(apm, info, &params, sg_list, num_sub_graphs);
588 
589 	return pkt;
590 }
591 EXPORT_SYMBOL_GPL(audioreach_alloc_graph_pkt);
592 
593 int audioreach_send_cmd_sync(struct device *dev, gpr_device_t *gdev,
594 			     struct gpr_ibasic_rsp_result_t *result, struct mutex *cmd_lock,
595 			     gpr_port_t *port, wait_queue_head_t *cmd_wait,
596 			     const struct gpr_pkt *pkt, uint32_t rsp_opcode)
597 {
598 
599 	const struct gpr_hdr *hdr = &pkt->hdr;
600 	int rc;
601 
602 	mutex_lock(cmd_lock);
603 	result->opcode = 0;
604 	result->status = 0;
605 
606 	if (port)
607 		rc = gpr_send_port_pkt(port, pkt);
608 	else if (gdev)
609 		rc = gpr_send_pkt(gdev, pkt);
610 	else
611 		rc = -EINVAL;
612 
613 	if (rc < 0)
614 		goto err;
615 
616 	if (rsp_opcode)
617 		rc = wait_event_timeout(*cmd_wait, (result->opcode == hdr->opcode) ||
618 					(result->opcode == rsp_opcode),	5 * HZ);
619 	else
620 		rc = wait_event_timeout(*cmd_wait, (result->opcode == hdr->opcode), 5 * HZ);
621 
622 	if (!rc) {
623 		dev_err(dev, "CMD timeout for [%x] opcode\n", hdr->opcode);
624 		rc = -ETIMEDOUT;
625 	} else if (result->status > 0) {
626 		dev_err(dev, "DSP returned error[%x] %x\n", hdr->opcode, result->status);
627 		rc = -EINVAL;
628 	} else {
629 		/* DSP successfully finished the command */
630 		rc = 0;
631 	}
632 
633 err:
634 	mutex_unlock(cmd_lock);
635 	return rc;
636 }
637 EXPORT_SYMBOL_GPL(audioreach_send_cmd_sync);
638 
639 int audioreach_graph_send_cmd_sync(struct q6apm_graph *graph, const struct gpr_pkt *pkt,
640 				   uint32_t rsp_opcode)
641 {
642 
643 	return audioreach_send_cmd_sync(graph->dev, NULL,  &graph->result, &graph->lock,
644 					graph->port, &graph->cmd_wait, pkt, rsp_opcode);
645 }
646 EXPORT_SYMBOL_GPL(audioreach_graph_send_cmd_sync);
647 
648 static int audioreach_display_port_set_media_format(struct q6apm_graph *graph,
649 						    const struct audioreach_module *module,
650 						    const struct audioreach_module_config *cfg)
651 {
652 	struct apm_display_port_module_intf_cfg *intf_cfg;
653 	struct apm_module_frame_size_factor_cfg *fs_cfg;
654 	struct apm_module_param_data *param_data;
655 	struct apm_module_hw_ep_mf_cfg *hw_cfg;
656 	int ic_sz = APM_DP_INTF_CFG_PSIZE;
657 	int ep_sz = APM_HW_EP_CFG_PSIZE;
658 	int fs_sz = APM_FS_CFG_PSIZE;
659 	int size = ic_sz + ep_sz + fs_sz;
660 	void *p;
661 
662 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
663 	if (IS_ERR(pkt))
664 		return PTR_ERR(pkt);
665 
666 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
667 
668 	hw_cfg = p;
669 	param_data = &hw_cfg->param_data;
670 	param_data->module_instance_id = module->instance_id;
671 	param_data->error_code = 0;
672 	param_data->param_id = PARAM_ID_HW_EP_MF_CFG;
673 	param_data->param_size = ep_sz - APM_MODULE_PARAM_DATA_SIZE;
674 
675 	hw_cfg->mf.sample_rate = cfg->sample_rate;
676 	hw_cfg->mf.bit_width = cfg->bit_width;
677 	hw_cfg->mf.num_channels = cfg->num_channels;
678 	hw_cfg->mf.data_format = module->data_format;
679 	p += ep_sz;
680 
681 	fs_cfg = p;
682 	param_data = &fs_cfg->param_data;
683 	param_data->module_instance_id = module->instance_id;
684 	param_data->error_code = 0;
685 	param_data->param_id = PARAM_ID_HW_EP_FRAME_SIZE_FACTOR;
686 	param_data->param_size = fs_sz - APM_MODULE_PARAM_DATA_SIZE;
687 	fs_cfg->frame_size_factor = 1;
688 	p += fs_sz;
689 
690 	intf_cfg = p;
691 	param_data = &intf_cfg->param_data;
692 	param_data->module_instance_id = module->instance_id;
693 	param_data->error_code = 0;
694 	param_data->param_id = PARAM_ID_DISPLAY_PORT_INTF_CFG;
695 	param_data->param_size = ic_sz - APM_MODULE_PARAM_DATA_SIZE;
696 
697 	intf_cfg->cfg.channel_allocation = cfg->channel_allocation;
698 	intf_cfg->cfg.mst_idx = 0;
699 	intf_cfg->cfg.dptx_idx = cfg->dp_idx;
700 
701 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
702 }
703 
704 /* LPASS Codec DMA port Module Media Format Setup */
705 static int audioreach_codec_dma_set_media_format(struct q6apm_graph *graph,
706 						 const struct audioreach_module *module,
707 						 const struct audioreach_module_config *cfg)
708 {
709 	struct apm_codec_dma_module_intf_cfg *intf_cfg;
710 	struct apm_module_frame_size_factor_cfg *fs_cfg;
711 	struct apm_module_hw_ep_power_mode_cfg *pm_cfg;
712 	struct apm_module_param_data *param_data;
713 	struct apm_module_hw_ep_mf_cfg *hw_cfg;
714 	int ic_sz = APM_CDMA_INTF_CFG_PSIZE;
715 	int ep_sz = APM_HW_EP_CFG_PSIZE;
716 	int fs_sz = APM_FS_CFG_PSIZE;
717 	int pm_sz = APM_HW_EP_PMODE_CFG_PSIZE;
718 	int size = ic_sz + ep_sz + fs_sz + pm_sz;
719 	void *p;
720 	int i;
721 
722 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
723 	if (IS_ERR(pkt))
724 		return PTR_ERR(pkt);
725 
726 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
727 
728 	hw_cfg = p;
729 	param_data = &hw_cfg->param_data;
730 	param_data->module_instance_id = module->instance_id;
731 	param_data->error_code = 0;
732 	param_data->param_id = PARAM_ID_HW_EP_MF_CFG;
733 	param_data->param_size = ep_sz - APM_MODULE_PARAM_DATA_SIZE;
734 
735 	hw_cfg->mf.sample_rate = cfg->sample_rate;
736 	hw_cfg->mf.bit_width = cfg->bit_width;
737 	hw_cfg->mf.num_channels = cfg->num_channels;
738 	hw_cfg->mf.data_format = module->data_format;
739 	p += ep_sz;
740 
741 	fs_cfg = p;
742 	param_data = &fs_cfg->param_data;
743 	param_data->module_instance_id = module->instance_id;
744 	param_data->error_code = 0;
745 	param_data->param_id = PARAM_ID_HW_EP_FRAME_SIZE_FACTOR;
746 	param_data->param_size = fs_sz - APM_MODULE_PARAM_DATA_SIZE;
747 	fs_cfg->frame_size_factor = 1;
748 	p += fs_sz;
749 
750 	intf_cfg = p;
751 	param_data = &intf_cfg->param_data;
752 	param_data->module_instance_id = module->instance_id;
753 	param_data->error_code = 0;
754 	param_data->param_id = PARAM_ID_CODEC_DMA_INTF_CFG;
755 	param_data->param_size = ic_sz - APM_MODULE_PARAM_DATA_SIZE;
756 
757 	intf_cfg->cfg.lpaif_type = module->hw_interface_type;
758 	intf_cfg->cfg.intf_index = module->hw_interface_idx;
759 	intf_cfg->cfg.active_channels_mask = 0;
760 	/* Convert the physical channel mapping into a bit field */
761 	for (i = 0; i < AR_PCM_MAX_NUM_CHANNEL; i++)
762 		if (cfg->channel_map[i])
763 			intf_cfg->cfg.active_channels_mask |= BIT(i);
764 
765 	p += ic_sz;
766 
767 	pm_cfg = p;
768 	param_data = &pm_cfg->param_data;
769 	param_data->module_instance_id = module->instance_id;
770 	param_data->error_code = 0;
771 	param_data->param_id = PARAM_ID_HW_EP_POWER_MODE_CFG;
772 	param_data->param_size = pm_sz - APM_MODULE_PARAM_DATA_SIZE;
773 	pm_cfg->power_mode.power_mode = 0;
774 
775 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
776 }
777 
778 int audioreach_send_u32_param(struct q6apm_graph *graph,
779 			      const struct audioreach_module *module,
780 			      uint32_t param_id, uint32_t param_val)
781 {
782 	struct apm_module_param_data *param_data;
783 	uint32_t *param;
784 	int payload_size = sizeof(uint32_t) + APM_MODULE_PARAM_DATA_SIZE;
785 	void *p;
786 
787 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
788 	if (IS_ERR(pkt))
789 		return -ENOMEM;
790 
791 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
792 
793 	param_data = p;
794 	param_data->module_instance_id = module->instance_id;
795 	param_data->error_code = 0;
796 	param_data->param_id = param_id;
797 	param_data->param_size = sizeof(uint32_t);
798 
799 	p = p + APM_MODULE_PARAM_DATA_SIZE;
800 	param = p;
801 	*param = param_val;
802 
803 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
804 }
805 EXPORT_SYMBOL_GPL(audioreach_send_u32_param);
806 
807 static int audioreach_sal_limiter_enable(struct q6apm_graph *graph,
808 					 const struct audioreach_module *module,
809 					 bool enable)
810 {
811 	return audioreach_send_u32_param(graph, module, PARAM_ID_SAL_LIMITER_ENABLE, enable);
812 }
813 
814 static int audioreach_sal_set_media_format(struct q6apm_graph *graph,
815 					   const struct audioreach_module *module,
816 					   const struct audioreach_module_config *cfg)
817 {
818 	return audioreach_send_u32_param(graph, module, PARAM_ID_SAL_OUTPUT_CFG,  cfg->bit_width);
819 }
820 
821 static int audioreach_module_enable(struct q6apm_graph *graph,
822 				    const struct audioreach_module *module,
823 				    bool enable)
824 {
825 	return audioreach_send_u32_param(graph, module, PARAM_ID_MODULE_ENABLE, enable);
826 }
827 
828 static int audioreach_gapless_set_media_format(struct q6apm_graph *graph,
829 					       const struct audioreach_module *module,
830 					       const struct audioreach_module_config *cfg)
831 {
832 	return audioreach_send_u32_param(graph, module, PARAM_ID_EARLY_EOS_DELAY,
833 					 EARLY_EOS_DELAY_MS);
834 }
835 
836 static int audioreach_set_module_config(struct q6apm_graph *graph,
837 					const struct audioreach_module *module,
838 					const struct audioreach_module_config *cfg)
839 {
840 	int size = le32_to_cpu(module->data->size);
841 	void *p;
842 
843 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
844 	if (IS_ERR(pkt))
845 		return PTR_ERR(pkt);
846 
847 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
848 
849 	memcpy(p, module->data->data, size);
850 
851 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
852 }
853 
854 static int audioreach_mfc_set_media_format(struct q6apm_graph *graph,
855 					   const struct audioreach_module *module,
856 					   const struct audioreach_module_config *cfg)
857 {
858 	struct apm_module_param_data *param_data;
859 	struct param_id_mfc_media_format *media_format;
860 	uint32_t num_channels = cfg->num_channels;
861 	int payload_size = APM_MFC_CFG_PSIZE(media_format, num_channels) +
862 				APM_MODULE_PARAM_DATA_SIZE;
863 	int i, j;
864 	void *p;
865 
866 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
867 	if (IS_ERR(pkt))
868 		return PTR_ERR(pkt);
869 
870 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
871 
872 	param_data = p;
873 	param_data->module_instance_id = module->instance_id;
874 	param_data->error_code = 0;
875 	param_data->param_id = PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
876 	param_data->param_size = APM_MFC_CFG_PSIZE(media_format, num_channels);
877 	p = p + APM_MODULE_PARAM_DATA_SIZE;
878 	media_format = p;
879 
880 	media_format->sample_rate = cfg->sample_rate;
881 	media_format->bit_width = cfg->bit_width;
882 	media_format->num_channels = cfg->num_channels;
883 	/* Convert the physical mapping to a logical mapping of the channels */
884 	for (i = 0, j = 0; i < AR_PCM_MAX_NUM_CHANNEL && j < cfg->num_channels; i++) {
885 		if (!cfg->channel_map[i])
886 			continue;
887 		media_format->channel_mapping[j++] = cfg->channel_map[i];
888 	}
889 
890 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
891 }
892 
893 static int audioreach_set_compr_media_format(struct media_format *media_fmt_hdr,
894 					     void *p,
895 					     const struct audioreach_module_config *mcfg)
896 {
897 	struct payload_media_fmt_aac_t *aac_cfg;
898 	struct payload_media_fmt_pcm *mp3_cfg;
899 	struct payload_media_fmt_flac_t *flac_cfg;
900 	struct payload_media_fmt_opus_t *opus_cfg;
901 
902 	switch (mcfg->fmt) {
903 	case SND_AUDIOCODEC_MP3:
904 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
905 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_MP3;
906 		media_fmt_hdr->payload_size = 0;
907 		p = p + sizeof(*media_fmt_hdr);
908 		mp3_cfg = p;
909 		mp3_cfg->sample_rate = mcfg->sample_rate;
910 		mp3_cfg->bit_width = mcfg->bit_width;
911 		mp3_cfg->alignment = PCM_LSB_ALIGNED;
912 		mp3_cfg->bits_per_sample = mcfg->bit_width;
913 		mp3_cfg->q_factor = mcfg->bit_width - 1;
914 		mp3_cfg->endianness = PCM_LITTLE_ENDIAN;
915 		mp3_cfg->num_channels = mcfg->num_channels;
916 		break;
917 	case SND_AUDIOCODEC_AAC:
918 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
919 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_AAC;
920 		media_fmt_hdr->payload_size = sizeof(struct payload_media_fmt_aac_t);
921 		p = p + sizeof(*media_fmt_hdr);
922 		aac_cfg = p;
923 		aac_cfg->aac_fmt_flag = 0;
924 		aac_cfg->audio_obj_type = 5;
925 		aac_cfg->num_channels = mcfg->num_channels;
926 		aac_cfg->total_size_of_PCE_bits = 0;
927 		aac_cfg->sample_rate = mcfg->sample_rate;
928 		break;
929 	case SND_AUDIOCODEC_FLAC:
930 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
931 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_FLAC;
932 		media_fmt_hdr->payload_size = sizeof(struct payload_media_fmt_flac_t);
933 		p = p + sizeof(*media_fmt_hdr);
934 		flac_cfg = p;
935 		flac_cfg->sample_size = mcfg->codec.options.flac_d.sample_size;
936 		flac_cfg->num_channels = mcfg->num_channels;
937 		flac_cfg->min_blk_size = mcfg->codec.options.flac_d.min_blk_size;
938 		flac_cfg->max_blk_size = mcfg->codec.options.flac_d.max_blk_size;
939 		flac_cfg->sample_rate = mcfg->sample_rate;
940 		flac_cfg->min_frame_size = mcfg->codec.options.flac_d.min_frame_size;
941 		flac_cfg->max_frame_size = mcfg->codec.options.flac_d.max_frame_size;
942 		break;
943 	case SND_AUDIOCODEC_OPUS_RAW:
944 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
945 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_OPUS;
946 		media_fmt_hdr->payload_size = sizeof(*opus_cfg);
947 		p = p + sizeof(*media_fmt_hdr);
948 		opus_cfg = p;
949 		/* raw opus packets prepended with 4 bytes of length */
950 		opus_cfg->bitstream_format = 1;
951 		/*
952 		 * payload_type:
953 		 * 0 -- read metadata from opus stream;
954 		 * 1 -- metadata is provided by filling in the struct here.
955 		 */
956 		opus_cfg->payload_type = 1;
957 		opus_cfg->version = mcfg->codec.options.opus_d.version;
958 		opus_cfg->num_channels = mcfg->codec.options.opus_d.num_channels;
959 		opus_cfg->pre_skip = mcfg->codec.options.opus_d.pre_skip;
960 		opus_cfg->sample_rate = mcfg->codec.options.opus_d.sample_rate;
961 		opus_cfg->output_gain = mcfg->codec.options.opus_d.output_gain;
962 		opus_cfg->mapping_family = mcfg->codec.options.opus_d.mapping_family;
963 		opus_cfg->stream_count = mcfg->codec.options.opus_d.chan_map.stream_count;
964 		opus_cfg->coupled_count = mcfg->codec.options.opus_d.chan_map.coupled_count;
965 		memcpy(opus_cfg->channel_mapping, mcfg->codec.options.opus_d.chan_map.channel_map,
966 		       sizeof(opus_cfg->channel_mapping));
967 		opus_cfg->reserved[0] = opus_cfg->reserved[1] = opus_cfg->reserved[2] = 0;
968 		break;
969 	default:
970 		return -EINVAL;
971 	}
972 
973 	return 0;
974 }
975 
976 int audioreach_compr_set_param(struct q6apm_graph *graph,
977 			       const struct audioreach_module_config *mcfg)
978 {
979 	struct media_format *header;
980 	int rc;
981 	void *p;
982 	int iid = graph->shm_iid;
983 	int payload_size = sizeof(struct apm_sh_module_media_fmt_cmd);
984 
985 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_cmd_pkt(payload_size,
986 					DATA_CMD_WR_SH_MEM_EP_MEDIA_FORMAT,
987 					0, graph->port->id, iid);
988 	if (IS_ERR(pkt))
989 		return -ENOMEM;
990 
991 	p = (void *)pkt + GPR_HDR_SIZE;
992 	header = p;
993 	rc = audioreach_set_compr_media_format(header, p, mcfg);
994 	if (rc)
995 		return rc;
996 
997 	return gpr_send_port_pkt(graph->port, pkt);
998 }
999 EXPORT_SYMBOL_GPL(audioreach_compr_set_param);
1000 
1001 static int audioreach_i2s_set_media_format(struct q6apm_graph *graph,
1002 					   const struct audioreach_module *module,
1003 					   const struct audioreach_module_config *cfg)
1004 {
1005 	struct apm_module_frame_size_factor_cfg *fs_cfg;
1006 	struct apm_module_param_data *param_data;
1007 	struct apm_i2s_module_intf_cfg *intf_cfg;
1008 	struct apm_module_hw_ep_mf_cfg *hw_cfg;
1009 	int ic_sz = APM_I2S_INTF_CFG_PSIZE;
1010 	int ep_sz = APM_HW_EP_CFG_PSIZE;
1011 	int fs_sz = APM_FS_CFG_PSIZE;
1012 	int size = ic_sz + ep_sz + fs_sz;
1013 	void *p;
1014 
1015 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1016 	if (IS_ERR(pkt))
1017 		return PTR_ERR(pkt);
1018 
1019 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1020 	intf_cfg = p;
1021 
1022 	param_data = &intf_cfg->param_data;
1023 	param_data->module_instance_id = module->instance_id;
1024 	param_data->error_code = 0;
1025 	param_data->param_id = PARAM_ID_I2S_INTF_CFG;
1026 	param_data->param_size = ic_sz - APM_MODULE_PARAM_DATA_SIZE;
1027 
1028 	intf_cfg->cfg.lpaif_type = module->hw_interface_type;
1029 	intf_cfg->cfg.intf_idx = module->hw_interface_idx;
1030 	intf_cfg->cfg.sd_line_idx = module->sd_line_idx;
1031 
1032 	switch (cfg->fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
1033 	case SND_SOC_DAIFMT_BP_FP:
1034 		intf_cfg->cfg.ws_src = CONFIG_I2S_WS_SRC_INTERNAL;
1035 		break;
1036 	case SND_SOC_DAIFMT_BC_FC:
1037 		/* CPU is slave */
1038 		intf_cfg->cfg.ws_src = CONFIG_I2S_WS_SRC_EXTERNAL;
1039 		break;
1040 	default:
1041 		break;
1042 	}
1043 
1044 	p += ic_sz;
1045 	hw_cfg = p;
1046 	param_data = &hw_cfg->param_data;
1047 	param_data->module_instance_id = module->instance_id;
1048 	param_data->error_code = 0;
1049 	param_data->param_id = PARAM_ID_HW_EP_MF_CFG;
1050 	param_data->param_size = ep_sz - APM_MODULE_PARAM_DATA_SIZE;
1051 
1052 	hw_cfg->mf.sample_rate = cfg->sample_rate;
1053 	hw_cfg->mf.bit_width = cfg->bit_width;
1054 	hw_cfg->mf.num_channels = cfg->num_channels;
1055 	hw_cfg->mf.data_format = module->data_format;
1056 
1057 	p += ep_sz;
1058 	fs_cfg = p;
1059 	param_data = &fs_cfg->param_data;
1060 	param_data->module_instance_id = module->instance_id;
1061 	param_data->error_code = 0;
1062 	param_data->param_id = PARAM_ID_HW_EP_FRAME_SIZE_FACTOR;
1063 	param_data->param_size = fs_sz - APM_MODULE_PARAM_DATA_SIZE;
1064 	fs_cfg->frame_size_factor = 1;
1065 
1066 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1067 }
1068 
1069 static int audioreach_audio_if_set_media_format(struct q6apm_graph *graph,
1070 						const struct audioreach_module *module,
1071 						const struct audioreach_module_config *cfg)
1072 {
1073 	struct apm_module_hw_ep_frame_duration_cfg *fd_cfg;
1074 	struct apm_module_param_data *param_data;
1075 	struct apm_audio_if_module_intf_cfg *intf_cfg;
1076 	struct apm_module_hw_ep_mf_cfg *hw_cfg;
1077 	int ic_sz = APM_AUDIO_IF_INTF_CFG_PSIZE;
1078 	int ep_sz = APM_HW_EP_CFG_PSIZE;
1079 	int fd_sz = APM_HW_EP_FRAME_DURATION_PSIZE;
1080 	int size = ic_sz + ep_sz + fd_sz;
1081 	u32 slot_mask = cfg->slot_mask ? cfg->slot_mask : module->slot_mask;
1082 	u16 nslots_per_frame = cfg->nslots_per_frame ?
1083 				 (u16)cfg->nslots_per_frame : module->nslots_per_frame;
1084 	u16 slot_width = cfg->slot_width ? (u16)cfg->slot_width : module->slot_width;
1085 	void *p;
1086 
1087 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1088 	if (IS_ERR(pkt))
1089 		return PTR_ERR(pkt);
1090 
1091 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1092 	intf_cfg = p;
1093 
1094 	param_data = &intf_cfg->param_data;
1095 	param_data->module_instance_id = module->instance_id;
1096 	param_data->error_code = 0;
1097 	param_data->param_id = PARAM_ID_AUDIO_IF_INTF_CFG;
1098 	param_data->param_size = ic_sz - APM_MODULE_PARAM_DATA_SIZE;
1099 	intf_cfg->cfg.qaif_type = module->qaif_type;
1100 	intf_cfg->cfg.intf_idx = (u16)module->hw_interface_idx;
1101 	intf_cfg->cfg.intf_mode = module->intf_mode;
1102 	intf_cfg->cfg.ctrl_data_out_enable = module->ctrl_data_out_enable;
1103 	intf_cfg->cfg.active_slot_mask = slot_mask;
1104 	intf_cfg->cfg.nslots_per_frame = nslots_per_frame;
1105 	intf_cfg->cfg.slot_width = slot_width;
1106 	intf_cfg->cfg.active_lane_mask = module->active_lane_mask;
1107 	intf_cfg->cfg.frame_sync_rate = module->frame_sync_rate;
1108 	intf_cfg->cfg.frame_sync_src = module->sync_src;
1109 	intf_cfg->cfg.frame_sync_mode = module->sync_mode;
1110 	intf_cfg->cfg.invert_frame_sync_pulse = module->ctrl_invert_sync_pulse;
1111 	intf_cfg->cfg.frame_sync_data_delay = module->ctrl_sync_data_delay;
1112 	intf_cfg->cfg.bit_clk_type = module->bit_clk_type;
1113 	intf_cfg->cfg.inv_int_bit_clk = module->inv_int_bit_clk;
1114 	intf_cfg->cfg.inv_ext_bit_clk = module->inv_ext_bit_clk;
1115 
1116 	p += ic_sz;
1117 	hw_cfg = p;
1118 	param_data = &hw_cfg->param_data;
1119 	param_data->module_instance_id = module->instance_id;
1120 	param_data->error_code = 0;
1121 	param_data->param_id = PARAM_ID_HW_EP_MF_CFG;
1122 	param_data->param_size = ep_sz - APM_MODULE_PARAM_DATA_SIZE;
1123 
1124 	hw_cfg->mf.sample_rate = cfg->sample_rate;
1125 	hw_cfg->mf.bit_width = cfg->bit_width;
1126 	hw_cfg->mf.num_channels = cfg->num_channels;
1127 	hw_cfg->mf.data_format = module->data_format;
1128 
1129 	p += ep_sz;
1130 	fd_cfg = p;
1131 	param_data = &fd_cfg->param_data;
1132 	param_data->module_instance_id = module->instance_id;
1133 	param_data->error_code = 0;
1134 	param_data->param_id = PARAM_ID_HW_EP_FRAME_DURATION;
1135 	param_data->param_size = fd_sz - APM_MODULE_PARAM_DATA_SIZE;
1136 	fd_cfg->frame_duration.frame_duration_in_us = AUDIO_IF_FRAME_DURATION_US;
1137 	fd_cfg->frame_duration.allow_frame_duration_normalization = AUDIO_IF_FRAME_DURATION_NORMALIZATION_ENABLE;
1138 	fd_cfg->frame_duration.min_normalized_frame_dur_us = AUDIO_IF_FRAME_DURATION_MIN_US;
1139 	fd_cfg->frame_duration.max_normalized_frame_dur_us = AUDIO_IF_FRAME_DURATION_MAX_US;
1140 
1141 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1142 }
1143 
1144 static int audioreach_logging_set_media_format(struct q6apm_graph *graph,
1145 					       const struct audioreach_module *module)
1146 {
1147 	struct apm_module_param_data *param_data;
1148 	struct data_logging_config *cfg;
1149 	int size = sizeof(*cfg) + APM_MODULE_PARAM_DATA_SIZE;
1150 	void *p;
1151 
1152 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1153 	if (IS_ERR(pkt))
1154 		return PTR_ERR(pkt);
1155 
1156 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1157 
1158 	param_data = p;
1159 	param_data->module_instance_id = module->instance_id;
1160 	param_data->error_code = 0;
1161 	param_data->param_id = PARAM_ID_DATA_LOGGING_CONFIG;
1162 	param_data->param_size = size - APM_MODULE_PARAM_DATA_SIZE;
1163 
1164 	p = p + APM_MODULE_PARAM_DATA_SIZE;
1165 	cfg = p;
1166 	cfg->log_code = module->log_code;
1167 	cfg->log_tap_point_id = module->log_tap_point_id;
1168 	cfg->mode = module->log_mode;
1169 
1170 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1171 }
1172 
1173 static int audioreach_pcm_set_media_format(struct q6apm_graph *graph,
1174 					   const struct audioreach_module *module,
1175 					   const struct audioreach_module_config *mcfg)
1176 {
1177 	struct payload_pcm_output_format_cfg *media_cfg;
1178 	uint32_t num_channels = mcfg->num_channels;
1179 	struct apm_pcm_module_media_fmt_cmd *cfg;
1180 	struct apm_module_param_data *param_data;
1181 	int payload_size;
1182 	int i, j;
1183 
1184 	if (num_channels > 4) {
1185 		dev_err(graph->dev, "Error: Invalid channels (%d)!\n", num_channels);
1186 		return -EINVAL;
1187 	}
1188 
1189 	payload_size = APM_PCM_MODULE_FMT_CMD_PSIZE(num_channels);
1190 
1191 	struct gpr_pkt *pkt __free(kfree) =
1192 		audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
1193 	if (IS_ERR(pkt))
1194 		return PTR_ERR(pkt);
1195 
1196 	cfg = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1197 
1198 	param_data = &cfg->param_data;
1199 	param_data->module_instance_id = module->instance_id;
1200 	param_data->error_code = 0;
1201 	param_data->param_id = PARAM_ID_PCM_OUTPUT_FORMAT_CFG;
1202 	param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
1203 
1204 	cfg->header.data_format = DATA_FORMAT_FIXED_POINT;
1205 	cfg->header.fmt_id = MEDIA_FMT_ID_PCM;
1206 	cfg->header.payload_size = APM_PCM_OUT_FMT_CFG_PSIZE(media_cfg, num_channels);
1207 
1208 	media_cfg = &cfg->media_cfg;
1209 	media_cfg->alignment = PCM_LSB_ALIGNED;
1210 	media_cfg->bit_width = mcfg->bit_width;
1211 	media_cfg->endianness = PCM_LITTLE_ENDIAN;
1212 	media_cfg->interleaved = module->interleave_type;
1213 	media_cfg->num_channels = mcfg->num_channels;
1214 	media_cfg->q_factor = mcfg->bit_width - 1;
1215 	media_cfg->bits_per_sample = mcfg->bit_width;
1216 	/* Convert the physical mapping to a logical mapping of the channels */
1217 	for (i = 0, j = 0; i < AR_PCM_MAX_NUM_CHANNEL && j < mcfg->num_channels; i++) {
1218 		if (!mcfg->channel_map[i])
1219 			continue;
1220 		media_cfg->channel_mapping[j++] = mcfg->channel_map[i];
1221 	}
1222 
1223 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1224 }
1225 
1226 int audioreach_shmem_register_event(struct q6apm_graph *graph, int bytes, int num_levels)
1227 {
1228 	struct apm_module_register_events *event;
1229 	struct event_cfg_sh_mem_pull_push_mode_watermark_t *level;
1230 	int i, payload_size;
1231 	struct gpr_pkt *pkt __free(kfree) = NULL;
1232 	void *p;
1233 
1234 	if (num_levels <= 0 || bytes <= 0)
1235 		return -EINVAL;
1236 
1237 	payload_size = sizeof(*event) + sizeof(*level) + num_levels * sizeof(uint32_t);
1238 
1239 	pkt = audioreach_alloc_cmd_pkt(payload_size, APM_CMD_REGISTER_MODULE_EVENTS, 0,
1240 				     graph->port->id, graph->shm_iid);
1241 	if (IS_ERR(pkt))
1242 		return PTR_ERR(pkt);
1243 
1244 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1245 
1246 	event = p;
1247 	event->module_instance_id = graph->shm_iid;
1248 	event->event_id = EVENT_ID_SH_MEM_PULL_PUSH_MODE_WATERMARK;
1249 	event->is_register = 1;
1250 	event->event_config_payload_size = sizeof(*level) + num_levels * sizeof(uint32_t);
1251 	p += sizeof(*event);
1252 	level = p;
1253 	level->num_water_mark_levels = num_levels;
1254 
1255 	for (i = 0; i < num_levels; i++)
1256 		level->level[i] = (i + 1) * bytes;
1257 
1258 	return audioreach_graph_send_cmd_sync(graph, pkt, 0);
1259 }
1260 EXPORT_SYMBOL_GPL(audioreach_shmem_register_event);
1261 
1262 static int audioreach_shmem_set_media_format(struct q6apm_graph *graph,
1263 					     const struct audioreach_module *module,
1264 					     const struct audioreach_module_config *mcfg)
1265 {
1266 	uint32_t num_channels = mcfg->num_channels;
1267 	struct apm_module_param_data *param_data;
1268 	struct payload_media_fmt_pcm *cfg;
1269 	struct media_format *header;
1270 	int rc, payload_size;
1271 	int i, j;
1272 	void *p;
1273 
1274 	if (num_channels > 4) {
1275 		dev_err(graph->dev, "Error: Invalid channels (%d)!\n", num_channels);
1276 		return -EINVAL;
1277 	}
1278 
1279 	payload_size = APM_SHMEM_FMT_CFG_PSIZE(num_channels) + APM_MODULE_PARAM_DATA_SIZE;
1280 
1281 	struct gpr_pkt *pkt __free(kfree) =
1282 		audioreach_alloc_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0,
1283 					 graph->port->id, module->instance_id);
1284 	if (IS_ERR(pkt))
1285 		return PTR_ERR(pkt);
1286 
1287 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1288 
1289 	param_data = p;
1290 	param_data->module_instance_id = module->instance_id;
1291 	param_data->error_code = 0;
1292 	param_data->param_id = PARAM_ID_MEDIA_FORMAT;
1293 	param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
1294 	p = p + APM_MODULE_PARAM_DATA_SIZE;
1295 
1296 	header = p;
1297 	if (mcfg->fmt == SND_AUDIOCODEC_PCM) {
1298 		header->data_format = DATA_FORMAT_FIXED_POINT;
1299 		header->fmt_id =  MEDIA_FMT_ID_PCM;
1300 		header->payload_size = payload_size - sizeof(*header);
1301 
1302 		p = p + sizeof(*header);
1303 		cfg = p;
1304 		cfg->sample_rate = mcfg->sample_rate;
1305 		cfg->bit_width = mcfg->bit_width;
1306 		cfg->alignment = PCM_LSB_ALIGNED;
1307 		cfg->bits_per_sample = mcfg->bit_width;
1308 		cfg->q_factor = mcfg->bit_width - 1;
1309 		cfg->endianness = PCM_LITTLE_ENDIAN;
1310 		cfg->num_channels = mcfg->num_channels;
1311 		/* Convert the physical mapping to a logical mapping of the channels */
1312 		for (i = 0, j = 0; i < AR_PCM_MAX_NUM_CHANNEL && j < cfg->num_channels; i++) {
1313 			if (!mcfg->channel_map[i])
1314 				continue;
1315 			cfg->channel_mapping[j++] = mcfg->channel_map[i];
1316 		}
1317 	} else {
1318 		rc = audioreach_set_compr_media_format(header, p, mcfg);
1319 		if (rc)
1320 			return rc;
1321 	}
1322 
1323 	return audioreach_graph_send_cmd_sync(graph, pkt, 0);
1324 }
1325 
1326 int audioreach_gain_set_vol_ctrl(struct q6apm *apm,
1327 				 const struct audioreach_module *module, int vol)
1328 {
1329 	struct param_id_vol_ctrl_master_gain *cfg;
1330 	struct apm_module_param_data *param_data;
1331 	int size = sizeof(*cfg) + APM_MODULE_PARAM_DATA_SIZE;
1332 	void *p;
1333 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1334 	if (IS_ERR(pkt))
1335 		return PTR_ERR(pkt);
1336 
1337 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1338 
1339 	param_data = p;
1340 	param_data->module_instance_id = module->instance_id;
1341 	param_data->error_code = 0;
1342 	param_data->param_id = PARAM_ID_VOL_CTRL_MASTER_GAIN;
1343 	param_data->param_size = size - APM_MODULE_PARAM_DATA_SIZE;
1344 
1345 	p = p + APM_MODULE_PARAM_DATA_SIZE;
1346 	cfg = p;
1347 	cfg->master_gain =  vol;
1348 	return q6apm_send_cmd_sync(apm, pkt, 0);
1349 }
1350 EXPORT_SYMBOL_GPL(audioreach_gain_set_vol_ctrl);
1351 
1352 static int audioreach_gain_set(struct q6apm_graph *graph,
1353 			       const struct audioreach_module *module)
1354 {
1355 	struct apm_module_param_data *param_data;
1356 	struct apm_gain_module_cfg *cfg;
1357 	int size = APM_GAIN_CFG_PSIZE;
1358 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1359 	if (IS_ERR(pkt))
1360 		return PTR_ERR(pkt);
1361 
1362 	cfg = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1363 
1364 	param_data = &cfg->param_data;
1365 	param_data->module_instance_id = module->instance_id;
1366 	param_data->error_code = 0;
1367 	param_data->param_id = APM_PARAM_ID_GAIN;
1368 	param_data->param_size = size - APM_MODULE_PARAM_DATA_SIZE;
1369 
1370 	cfg->gain_cfg.gain = module->gain;
1371 
1372 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1373 }
1374 
1375 static int audioreach_speaker_protection(struct q6apm_graph *graph,
1376 					 const struct audioreach_module *module,
1377 					 uint32_t operation_mode)
1378 {
1379 	return audioreach_send_u32_param(graph, module, PARAM_ID_SP_OP_MODE,
1380 					 operation_mode);
1381 }
1382 
1383 static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,
1384 					    const struct audioreach_module *module,
1385 					    const struct audioreach_module_config *mcfg)
1386 {
1387 	u32 num_channels = mcfg->num_channels;
1388 	struct apm_module_sp_vi_op_mode_cfg *op_cfg;
1389 	struct apm_module_sp_vi_channel_map_cfg *cm_cfg;
1390 	struct apm_module_sp_vi_ex_mode_cfg *ex_cfg;
1391 	int op_sz, cm_sz, ex_sz;
1392 	struct apm_module_param_data *param_data;
1393 	int rc, i, payload_size, j;
1394 	struct gpr_pkt *pkt;
1395 	void *p;
1396 
1397 	if (num_channels > 2) {
1398 		dev_err(graph->dev, "Error: Invalid channels (%d)!\n", num_channels);
1399 		return -EINVAL;
1400 	}
1401 
1402 	op_sz = APM_SP_VI_OP_MODE_CFG_PSIZE(num_channels);
1403 	/* Channel mapping for Isense and Vsense, thus twice number of speakers. */
1404 	cm_sz = APM_SP_VI_CH_MAP_CFG_PSIZE(num_channels * 2);
1405 	ex_sz = APM_SP_VI_EX_MODE_CFG_PSIZE;
1406 
1407 	payload_size = op_sz + cm_sz + ex_sz;
1408 
1409 	pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
1410 	if (IS_ERR(pkt))
1411 		return PTR_ERR(pkt);
1412 
1413 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1414 
1415 	op_cfg = p;
1416 	param_data = &op_cfg->param_data;
1417 	param_data->module_instance_id = module->instance_id;
1418 	param_data->error_code = 0;
1419 	param_data->param_id = PARAM_ID_SP_VI_OP_MODE_CFG;
1420 	param_data->param_size = op_sz - APM_MODULE_PARAM_DATA_SIZE;
1421 
1422 	op_cfg->cfg.num_channels = num_channels;
1423 	op_cfg->cfg.operation_mode = PARAM_ID_SP_VI_OP_MODE_NORMAL;
1424 	p += op_sz;
1425 
1426 	cm_cfg = p;
1427 	param_data = &cm_cfg->param_data;
1428 	param_data->module_instance_id = module->instance_id;
1429 	param_data->error_code = 0;
1430 	param_data->param_id = PARAM_ID_SP_VI_CHANNEL_MAP_CFG;
1431 	param_data->param_size = cm_sz - APM_MODULE_PARAM_DATA_SIZE;
1432 
1433 	cm_cfg->cfg.num_channels = num_channels * 2;
1434 	/* Convert the physical mapping to a logical mapping of the channels */
1435 	for (i = 0, j = 0; i < AR_PCM_MAX_NUM_CHANNEL && j < num_channels; i++) {
1436 		if (!mcfg->channel_map[i])
1437 			continue;
1438 		/*
1439 		 * Map speakers into Vsense and then Isense of each channel.
1440 		 * E.g. for PCM_CHANNEL_FL and PCM_CHANNEL_FR to:
1441 		 * [1, 2, 3, 4]
1442 		 */
1443 		cm_cfg->cfg.channel_mapping[2 * j] = (mcfg->channel_map[i] - 1) * 2 + 1;
1444 		cm_cfg->cfg.channel_mapping[2 * j + 1] = (mcfg->channel_map[i] - 1) * 2 + 2;
1445 
1446 		++j;
1447 	}
1448 
1449 	p += cm_sz;
1450 
1451 	ex_cfg = p;
1452 	param_data = &ex_cfg->param_data;
1453 	param_data->module_instance_id = module->instance_id;
1454 	param_data->error_code = 0;
1455 	param_data->param_id = PARAM_ID_SP_VI_EX_MODE_CFG;
1456 	param_data->param_size = ex_sz - APM_MODULE_PARAM_DATA_SIZE;
1457 
1458 	ex_cfg->cfg.factory_mode = 0;
1459 
1460 	rc = q6apm_send_cmd_sync(graph->apm, pkt, 0);
1461 
1462 	kfree(pkt);
1463 
1464 	return rc;
1465 }
1466 
1467 int audioreach_set_media_format(struct q6apm_graph *graph,
1468 				const struct audioreach_module *module,
1469 				const struct audioreach_module_config *cfg)
1470 {
1471 	int rc;
1472 
1473 	switch (module->module_id) {
1474 	case MODULE_ID_DATA_LOGGING:
1475 		rc = audioreach_module_enable(graph, module, true);
1476 		if (!rc)
1477 			rc = audioreach_logging_set_media_format(graph, module);
1478 		break;
1479 	case MODULE_ID_PCM_DEC:
1480 	case MODULE_ID_PCM_ENC:
1481 	case MODULE_ID_PCM_CNV:
1482 	case MODULE_ID_PLACEHOLDER_DECODER:
1483 	case MODULE_ID_PLACEHOLDER_ENCODER:
1484 		rc = audioreach_pcm_set_media_format(graph, module, cfg);
1485 		break;
1486 	case MODULE_ID_DISPLAY_PORT_SINK:
1487 		rc = audioreach_display_port_set_media_format(graph, module, cfg);
1488 		break;
1489 	case  MODULE_ID_SMECNS_V2:
1490 		rc = audioreach_set_module_config(graph, module, cfg);
1491 		break;
1492 	case MODULE_ID_I2S_SOURCE:
1493 	case MODULE_ID_I2S_SINK:
1494 		rc = audioreach_i2s_set_media_format(graph, module, cfg);
1495 		break;
1496 	case MODULE_ID_WR_SHARED_MEM_EP:
1497 	case MODULE_ID_SH_MEM_PULL_MODE:
1498 		rc = audioreach_shmem_set_media_format(graph, module, cfg);
1499 		break;
1500 	case MODULE_ID_GAIN:
1501 		rc = audioreach_gain_set(graph, module);
1502 		break;
1503 	case MODULE_ID_CODEC_DMA_SINK:
1504 	case MODULE_ID_CODEC_DMA_SOURCE:
1505 		rc = audioreach_codec_dma_set_media_format(graph, module, cfg);
1506 		break;
1507 	case MODULE_ID_SAL:
1508 		rc = audioreach_sal_set_media_format(graph, module, cfg);
1509 		if (!rc)
1510 			rc = audioreach_sal_limiter_enable(graph, module, true);
1511 		break;
1512 	case MODULE_ID_MFC:
1513 		rc = audioreach_mfc_set_media_format(graph, module, cfg);
1514 		break;
1515 	case MODULE_ID_GAPLESS:
1516 		rc = audioreach_gapless_set_media_format(graph, module, cfg);
1517 		break;
1518 	case MODULE_ID_SPEAKER_PROTECTION:
1519 		rc = audioreach_speaker_protection(graph, module,
1520 						   PARAM_ID_SP_OP_MODE_NORMAL);
1521 		if (!rc)
1522 			rc = audioreach_module_enable(graph, module, true);
1523 
1524 		break;
1525 	case MODULE_ID_SPEAKER_PROTECTION_VI:
1526 		rc = audioreach_speaker_protection_vi(graph, module, cfg);
1527 		if (!rc)
1528 			rc = audioreach_module_enable(graph, module, true);
1529 		break;
1530 	case MODULE_ID_AUDIO_IF_SOURCE:
1531 	case MODULE_ID_AUDIO_IF_SINK:
1532 		rc = audioreach_audio_if_set_media_format(graph, module, cfg);
1533 		break;
1534 
1535 	default:
1536 		rc = 0;
1537 	}
1538 
1539 	return rc;
1540 }
1541 EXPORT_SYMBOL_GPL(audioreach_set_media_format);
1542 
1543 void audioreach_graph_free_buf(struct q6apm_graph *graph)
1544 {
1545 	struct audioreach_graph_data *port;
1546 
1547 	mutex_lock(&graph->lock);
1548 	port = &graph->rx_data;
1549 	port->num_periods = 0;
1550 	kfree(port->buf);
1551 	port->buf = NULL;
1552 
1553 	port = &graph->tx_data;
1554 	port->num_periods = 0;
1555 	kfree(port->buf);
1556 	port->buf = NULL;
1557 	mutex_unlock(&graph->lock);
1558 }
1559 EXPORT_SYMBOL_GPL(audioreach_graph_free_buf);
1560 
1561 int audioreach_setup_push_pull(struct q6apm_graph *graph, phys_addr_t bphys,
1562 				phys_addr_t pphys, uint32_t mem_map_handle,
1563 				uint32_t pos_buf_mem_map_handle, uint32_t size)
1564 {
1565 	struct param_id_sh_mem_pull_push_mode_cfg *cfg;
1566 	struct apm_module_param_data *param_data;
1567 	int payload_size;
1568 	struct gpr_pkt *pkt __free(kfree) = NULL;
1569 	void *p;
1570 
1571 	payload_size = sizeof(*cfg) + APM_MODULE_PARAM_DATA_SIZE;
1572 	pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
1573 	if (IS_ERR(pkt))
1574 		return PTR_ERR(pkt);
1575 
1576 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1577 
1578 	param_data = p;
1579 	param_data->module_instance_id = graph->shm_iid;
1580 	param_data->error_code = 0;
1581 	param_data->param_id = PARAM_ID_SH_MEM_PULL_PUSH_MODE_CFG;
1582 	param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
1583 
1584 	p = p + APM_MODULE_PARAM_DATA_SIZE;
1585 	cfg = p;
1586 
1587 	cfg->shared_circ_buf_addr_lsw = lower_32_bits(bphys);
1588 	cfg->shared_circ_buf_addr_msw = upper_32_bits(bphys);
1589 	cfg->shared_circ_buf_size = size;
1590 	cfg->circ_buf_mem_map_handle = mem_map_handle;
1591 	cfg->shared_pos_buf_addr_lsw = lower_32_bits(pphys);
1592 	cfg->shared_pos_buf_addr_msw = upper_32_bits(pphys);
1593 	cfg->pos_buf_mem_map_handle = pos_buf_mem_map_handle;
1594 
1595 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1596 }
1597 EXPORT_SYMBOL_GPL(audioreach_setup_push_pull);
1598 
1599 int audioreach_shared_memory_send_eos(struct q6apm_graph *graph)
1600 {
1601 	struct data_cmd_wr_sh_mem_ep_eos *eos;
1602 	int iid = graph->shm_iid;
1603 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_cmd_pkt(sizeof(*eos),
1604 					DATA_CMD_WR_SH_MEM_EP_EOS, 0, graph->port->id, iid);
1605 	if (IS_ERR(pkt))
1606 		return PTR_ERR(pkt);
1607 
1608 	eos = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1609 
1610 	eos->policy = WR_SH_MEM_EP_EOS_POLICY_LAST;
1611 
1612 	return gpr_send_port_pkt(graph->port, pkt);
1613 }
1614 EXPORT_SYMBOL_GPL(audioreach_shared_memory_send_eos);
1615