xref: /linux/sound/soc/qcom/qdsp6/audioreach.c (revision d3ad9ee48425f06b2ae512a9d76c8ba855decdb9)
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 			     struct gpr_pkt *pkt, uint32_t rsp_opcode)
597 {
598 
599 	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, struct gpr_pkt *pkt,
640 				   uint32_t rsp_opcode)
641 {
642 
643 	pkt->hdr.dest_domain = audioreach_gpr_dest_domain(graph->apm->gdev);
644 
645 	return audioreach_send_cmd_sync(graph->dev, NULL,  &graph->result, &graph->lock,
646 					graph->port, &graph->cmd_wait, pkt, rsp_opcode);
647 }
648 EXPORT_SYMBOL_GPL(audioreach_graph_send_cmd_sync);
649 
650 static int audioreach_display_port_set_media_format(struct q6apm_graph *graph,
651 						    const struct audioreach_module *module,
652 						    const struct audioreach_module_config *cfg)
653 {
654 	struct apm_display_port_module_intf_cfg *intf_cfg;
655 	struct apm_module_frame_size_factor_cfg *fs_cfg;
656 	struct apm_module_param_data *param_data;
657 	struct apm_module_hw_ep_mf_cfg *hw_cfg;
658 	int ic_sz = APM_DP_INTF_CFG_PSIZE;
659 	int ep_sz = APM_HW_EP_CFG_PSIZE;
660 	int fs_sz = APM_FS_CFG_PSIZE;
661 	int size = ic_sz + ep_sz + fs_sz;
662 	void *p;
663 
664 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
665 	if (IS_ERR(pkt))
666 		return PTR_ERR(pkt);
667 
668 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
669 
670 	hw_cfg = p;
671 	param_data = &hw_cfg->param_data;
672 	param_data->module_instance_id = module->instance_id;
673 	param_data->error_code = 0;
674 	param_data->param_id = PARAM_ID_HW_EP_MF_CFG;
675 	param_data->param_size = ep_sz - APM_MODULE_PARAM_DATA_SIZE;
676 
677 	hw_cfg->mf.sample_rate = cfg->sample_rate;
678 	hw_cfg->mf.bit_width = cfg->bit_width;
679 	hw_cfg->mf.num_channels = cfg->num_channels;
680 	hw_cfg->mf.data_format = module->data_format;
681 	p += ep_sz;
682 
683 	fs_cfg = p;
684 	param_data = &fs_cfg->param_data;
685 	param_data->module_instance_id = module->instance_id;
686 	param_data->error_code = 0;
687 	param_data->param_id = PARAM_ID_HW_EP_FRAME_SIZE_FACTOR;
688 	param_data->param_size = fs_sz - APM_MODULE_PARAM_DATA_SIZE;
689 	fs_cfg->frame_size_factor = 1;
690 	p += fs_sz;
691 
692 	intf_cfg = p;
693 	param_data = &intf_cfg->param_data;
694 	param_data->module_instance_id = module->instance_id;
695 	param_data->error_code = 0;
696 	param_data->param_id = PARAM_ID_DISPLAY_PORT_INTF_CFG;
697 	param_data->param_size = ic_sz - APM_MODULE_PARAM_DATA_SIZE;
698 
699 	intf_cfg->cfg.channel_allocation = cfg->channel_allocation;
700 	intf_cfg->cfg.mst_idx = 0;
701 	intf_cfg->cfg.dptx_idx = cfg->dp_idx;
702 
703 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
704 }
705 
706 /* LPASS Codec DMA port Module Media Format Setup */
707 static int audioreach_codec_dma_set_media_format(struct q6apm_graph *graph,
708 						 const struct audioreach_module *module,
709 						 const struct audioreach_module_config *cfg)
710 {
711 	struct apm_codec_dma_module_intf_cfg *intf_cfg;
712 	struct apm_module_frame_size_factor_cfg *fs_cfg;
713 	struct apm_module_hw_ep_power_mode_cfg *pm_cfg;
714 	struct apm_module_param_data *param_data;
715 	struct apm_module_hw_ep_mf_cfg *hw_cfg;
716 	int ic_sz = APM_CDMA_INTF_CFG_PSIZE;
717 	int ep_sz = APM_HW_EP_CFG_PSIZE;
718 	int fs_sz = APM_FS_CFG_PSIZE;
719 	int pm_sz = APM_HW_EP_PMODE_CFG_PSIZE;
720 	int size = ic_sz + ep_sz + fs_sz + pm_sz;
721 	void *p;
722 	int i;
723 
724 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
725 	if (IS_ERR(pkt))
726 		return PTR_ERR(pkt);
727 
728 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
729 
730 	hw_cfg = p;
731 	param_data = &hw_cfg->param_data;
732 	param_data->module_instance_id = module->instance_id;
733 	param_data->error_code = 0;
734 	param_data->param_id = PARAM_ID_HW_EP_MF_CFG;
735 	param_data->param_size = ep_sz - APM_MODULE_PARAM_DATA_SIZE;
736 
737 	hw_cfg->mf.sample_rate = cfg->sample_rate;
738 	hw_cfg->mf.bit_width = cfg->bit_width;
739 	hw_cfg->mf.num_channels = cfg->num_channels;
740 	hw_cfg->mf.data_format = module->data_format;
741 	p += ep_sz;
742 
743 	fs_cfg = p;
744 	param_data = &fs_cfg->param_data;
745 	param_data->module_instance_id = module->instance_id;
746 	param_data->error_code = 0;
747 	param_data->param_id = PARAM_ID_HW_EP_FRAME_SIZE_FACTOR;
748 	param_data->param_size = fs_sz - APM_MODULE_PARAM_DATA_SIZE;
749 	fs_cfg->frame_size_factor = 1;
750 	p += fs_sz;
751 
752 	intf_cfg = p;
753 	param_data = &intf_cfg->param_data;
754 	param_data->module_instance_id = module->instance_id;
755 	param_data->error_code = 0;
756 	param_data->param_id = PARAM_ID_CODEC_DMA_INTF_CFG;
757 	param_data->param_size = ic_sz - APM_MODULE_PARAM_DATA_SIZE;
758 
759 	intf_cfg->cfg.lpaif_type = module->hw_interface_type;
760 	intf_cfg->cfg.intf_index = module->hw_interface_idx;
761 	intf_cfg->cfg.active_channels_mask = 0;
762 	/* Convert the physical channel mapping into a bit field */
763 	for (i = 0; i < AR_PCM_MAX_NUM_CHANNEL; i++)
764 		if (cfg->channel_map[i])
765 			intf_cfg->cfg.active_channels_mask |= BIT(i);
766 
767 	p += ic_sz;
768 
769 	pm_cfg = p;
770 	param_data = &pm_cfg->param_data;
771 	param_data->module_instance_id = module->instance_id;
772 	param_data->error_code = 0;
773 	param_data->param_id = PARAM_ID_HW_EP_POWER_MODE_CFG;
774 	param_data->param_size = pm_sz - APM_MODULE_PARAM_DATA_SIZE;
775 	pm_cfg->power_mode.power_mode = 0;
776 
777 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
778 }
779 
780 int audioreach_send_u32_param(struct q6apm_graph *graph,
781 			      const struct audioreach_module *module,
782 			      uint32_t param_id, uint32_t param_val)
783 {
784 	struct apm_module_param_data *param_data;
785 	uint32_t *param;
786 	int payload_size = sizeof(uint32_t) + APM_MODULE_PARAM_DATA_SIZE;
787 	void *p;
788 
789 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
790 	if (IS_ERR(pkt))
791 		return -ENOMEM;
792 
793 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
794 
795 	param_data = p;
796 	param_data->module_instance_id = module->instance_id;
797 	param_data->error_code = 0;
798 	param_data->param_id = param_id;
799 	param_data->param_size = sizeof(uint32_t);
800 
801 	p = p + APM_MODULE_PARAM_DATA_SIZE;
802 	param = p;
803 	*param = param_val;
804 
805 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
806 }
807 EXPORT_SYMBOL_GPL(audioreach_send_u32_param);
808 
809 static int audioreach_sal_limiter_enable(struct q6apm_graph *graph,
810 					 const struct audioreach_module *module,
811 					 bool enable)
812 {
813 	return audioreach_send_u32_param(graph, module, PARAM_ID_SAL_LIMITER_ENABLE, enable);
814 }
815 
816 static int audioreach_sal_set_media_format(struct q6apm_graph *graph,
817 					   const struct audioreach_module *module,
818 					   const struct audioreach_module_config *cfg)
819 {
820 	return audioreach_send_u32_param(graph, module, PARAM_ID_SAL_OUTPUT_CFG,  cfg->bit_width);
821 }
822 
823 static int audioreach_module_enable(struct q6apm_graph *graph,
824 				    const struct audioreach_module *module,
825 				    bool enable)
826 {
827 	return audioreach_send_u32_param(graph, module, PARAM_ID_MODULE_ENABLE, enable);
828 }
829 
830 static int audioreach_gapless_set_media_format(struct q6apm_graph *graph,
831 					       const struct audioreach_module *module,
832 					       const struct audioreach_module_config *cfg)
833 {
834 	return audioreach_send_u32_param(graph, module, PARAM_ID_EARLY_EOS_DELAY,
835 					 EARLY_EOS_DELAY_MS);
836 }
837 
838 static int audioreach_set_module_config(struct q6apm_graph *graph,
839 					const struct audioreach_module *module,
840 					const struct audioreach_module_config *cfg)
841 {
842 	int size = le32_to_cpu(module->data->size);
843 	void *p;
844 
845 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
846 	if (IS_ERR(pkt))
847 		return PTR_ERR(pkt);
848 
849 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
850 
851 	memcpy(p, module->data->data, size);
852 
853 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
854 }
855 
856 static int audioreach_mfc_set_media_format(struct q6apm_graph *graph,
857 					   const struct audioreach_module *module,
858 					   const struct audioreach_module_config *cfg)
859 {
860 	struct apm_module_param_data *param_data;
861 	struct param_id_mfc_media_format *media_format;
862 	uint32_t num_channels = cfg->num_channels;
863 	int payload_size = APM_MFC_CFG_PSIZE(media_format, num_channels) +
864 				APM_MODULE_PARAM_DATA_SIZE;
865 	int i, j;
866 	void *p;
867 
868 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
869 	if (IS_ERR(pkt))
870 		return PTR_ERR(pkt);
871 
872 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
873 
874 	param_data = p;
875 	param_data->module_instance_id = module->instance_id;
876 	param_data->error_code = 0;
877 	param_data->param_id = PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
878 	param_data->param_size = APM_MFC_CFG_PSIZE(media_format, num_channels);
879 	p = p + APM_MODULE_PARAM_DATA_SIZE;
880 	media_format = p;
881 
882 	media_format->sample_rate = cfg->sample_rate;
883 	media_format->bit_width = cfg->bit_width;
884 	media_format->num_channels = cfg->num_channels;
885 	/* Convert the physical mapping to a logical mapping of the channels */
886 	for (i = 0, j = 0; i < AR_PCM_MAX_NUM_CHANNEL && j < cfg->num_channels; i++) {
887 		if (!cfg->channel_map[i])
888 			continue;
889 		media_format->channel_mapping[j++] = cfg->channel_map[i];
890 	}
891 
892 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
893 }
894 
895 static int audioreach_set_compr_media_format(struct media_format *media_fmt_hdr,
896 					     void *p,
897 					     const struct audioreach_module_config *mcfg)
898 {
899 	struct payload_media_fmt_aac_t *aac_cfg;
900 	struct payload_media_fmt_pcm *mp3_cfg;
901 	struct payload_media_fmt_flac_t *flac_cfg;
902 	struct payload_media_fmt_opus_t *opus_cfg;
903 
904 	switch (mcfg->fmt) {
905 	case SND_AUDIOCODEC_MP3:
906 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
907 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_MP3;
908 		media_fmt_hdr->payload_size = 0;
909 		p = p + sizeof(*media_fmt_hdr);
910 		mp3_cfg = p;
911 		mp3_cfg->sample_rate = mcfg->sample_rate;
912 		mp3_cfg->bit_width = mcfg->bit_width;
913 		mp3_cfg->alignment = PCM_LSB_ALIGNED;
914 		mp3_cfg->bits_per_sample = mcfg->bit_width;
915 		mp3_cfg->q_factor = mcfg->bit_width - 1;
916 		mp3_cfg->endianness = PCM_LITTLE_ENDIAN;
917 		mp3_cfg->num_channels = mcfg->num_channels;
918 		break;
919 	case SND_AUDIOCODEC_AAC:
920 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
921 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_AAC;
922 		media_fmt_hdr->payload_size = sizeof(struct payload_media_fmt_aac_t);
923 		p = p + sizeof(*media_fmt_hdr);
924 		aac_cfg = p;
925 		aac_cfg->aac_fmt_flag = 0;
926 		aac_cfg->audio_obj_type = 5;
927 		aac_cfg->num_channels = mcfg->num_channels;
928 		aac_cfg->total_size_of_PCE_bits = 0;
929 		aac_cfg->sample_rate = mcfg->sample_rate;
930 		break;
931 	case SND_AUDIOCODEC_FLAC:
932 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
933 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_FLAC;
934 		media_fmt_hdr->payload_size = sizeof(struct payload_media_fmt_flac_t);
935 		p = p + sizeof(*media_fmt_hdr);
936 		flac_cfg = p;
937 		flac_cfg->sample_size = mcfg->codec.options.flac_d.sample_size;
938 		flac_cfg->num_channels = mcfg->num_channels;
939 		flac_cfg->min_blk_size = mcfg->codec.options.flac_d.min_blk_size;
940 		flac_cfg->max_blk_size = mcfg->codec.options.flac_d.max_blk_size;
941 		flac_cfg->sample_rate = mcfg->sample_rate;
942 		flac_cfg->min_frame_size = mcfg->codec.options.flac_d.min_frame_size;
943 		flac_cfg->max_frame_size = mcfg->codec.options.flac_d.max_frame_size;
944 		break;
945 	case SND_AUDIOCODEC_OPUS_RAW:
946 		media_fmt_hdr->data_format = DATA_FORMAT_RAW_COMPRESSED;
947 		media_fmt_hdr->fmt_id = MEDIA_FMT_ID_OPUS;
948 		media_fmt_hdr->payload_size = sizeof(*opus_cfg);
949 		p = p + sizeof(*media_fmt_hdr);
950 		opus_cfg = p;
951 		/* raw opus packets prepended with 4 bytes of length */
952 		opus_cfg->bitstream_format = 1;
953 		/*
954 		 * payload_type:
955 		 * 0 -- read metadata from opus stream;
956 		 * 1 -- metadata is provided by filling in the struct here.
957 		 */
958 		opus_cfg->payload_type = 1;
959 		opus_cfg->version = mcfg->codec.options.opus_d.version;
960 		opus_cfg->num_channels = mcfg->codec.options.opus_d.num_channels;
961 		opus_cfg->pre_skip = mcfg->codec.options.opus_d.pre_skip;
962 		opus_cfg->sample_rate = mcfg->codec.options.opus_d.sample_rate;
963 		opus_cfg->output_gain = mcfg->codec.options.opus_d.output_gain;
964 		opus_cfg->mapping_family = mcfg->codec.options.opus_d.mapping_family;
965 		opus_cfg->stream_count = mcfg->codec.options.opus_d.chan_map.stream_count;
966 		opus_cfg->coupled_count = mcfg->codec.options.opus_d.chan_map.coupled_count;
967 		memcpy(opus_cfg->channel_mapping, mcfg->codec.options.opus_d.chan_map.channel_map,
968 		       sizeof(opus_cfg->channel_mapping));
969 		opus_cfg->reserved[0] = opus_cfg->reserved[1] = opus_cfg->reserved[2] = 0;
970 		break;
971 	default:
972 		return -EINVAL;
973 	}
974 
975 	return 0;
976 }
977 
978 int audioreach_compr_set_param(struct q6apm_graph *graph,
979 			       const struct audioreach_module_config *mcfg)
980 {
981 	struct media_format *header;
982 	int rc;
983 	void *p;
984 	int iid = graph->shm_iid;
985 	int payload_size = sizeof(struct apm_sh_module_media_fmt_cmd);
986 
987 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_cmd_pkt(payload_size,
988 					DATA_CMD_WR_SH_MEM_EP_MEDIA_FORMAT,
989 					0, graph->port->id, iid);
990 	if (IS_ERR(pkt))
991 		return -ENOMEM;
992 
993 	p = (void *)pkt + GPR_HDR_SIZE;
994 	header = p;
995 	rc = audioreach_set_compr_media_format(header, p, mcfg);
996 	if (rc)
997 		return rc;
998 
999 	pkt->hdr.dest_domain = audioreach_gpr_dest_domain(graph->apm->gdev);
1000 
1001 	return gpr_send_port_pkt(graph->port, pkt);
1002 }
1003 EXPORT_SYMBOL_GPL(audioreach_compr_set_param);
1004 
1005 static int audioreach_i2s_set_media_format(struct q6apm_graph *graph,
1006 					   const struct audioreach_module *module,
1007 					   const struct audioreach_module_config *cfg)
1008 {
1009 	struct apm_module_frame_size_factor_cfg *fs_cfg;
1010 	struct apm_module_param_data *param_data;
1011 	struct apm_i2s_module_intf_cfg *intf_cfg;
1012 	struct apm_module_hw_ep_mf_cfg *hw_cfg;
1013 	int ic_sz = APM_I2S_INTF_CFG_PSIZE;
1014 	int ep_sz = APM_HW_EP_CFG_PSIZE;
1015 	int fs_sz = APM_FS_CFG_PSIZE;
1016 	int size = ic_sz + ep_sz + fs_sz;
1017 	void *p;
1018 
1019 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1020 	if (IS_ERR(pkt))
1021 		return PTR_ERR(pkt);
1022 
1023 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1024 	intf_cfg = p;
1025 
1026 	param_data = &intf_cfg->param_data;
1027 	param_data->module_instance_id = module->instance_id;
1028 	param_data->error_code = 0;
1029 	param_data->param_id = PARAM_ID_I2S_INTF_CFG;
1030 	param_data->param_size = ic_sz - APM_MODULE_PARAM_DATA_SIZE;
1031 
1032 	intf_cfg->cfg.lpaif_type = module->hw_interface_type;
1033 	intf_cfg->cfg.intf_idx = module->hw_interface_idx;
1034 	intf_cfg->cfg.sd_line_idx = module->sd_line_idx;
1035 
1036 	switch (cfg->fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
1037 	case SND_SOC_DAIFMT_BP_FP:
1038 		intf_cfg->cfg.ws_src = CONFIG_I2S_WS_SRC_INTERNAL;
1039 		break;
1040 	case SND_SOC_DAIFMT_BC_FC:
1041 		/* CPU is slave */
1042 		intf_cfg->cfg.ws_src = CONFIG_I2S_WS_SRC_EXTERNAL;
1043 		break;
1044 	default:
1045 		break;
1046 	}
1047 
1048 	p += ic_sz;
1049 	hw_cfg = p;
1050 	param_data = &hw_cfg->param_data;
1051 	param_data->module_instance_id = module->instance_id;
1052 	param_data->error_code = 0;
1053 	param_data->param_id = PARAM_ID_HW_EP_MF_CFG;
1054 	param_data->param_size = ep_sz - APM_MODULE_PARAM_DATA_SIZE;
1055 
1056 	hw_cfg->mf.sample_rate = cfg->sample_rate;
1057 	hw_cfg->mf.bit_width = cfg->bit_width;
1058 	hw_cfg->mf.num_channels = cfg->num_channels;
1059 	hw_cfg->mf.data_format = module->data_format;
1060 
1061 	p += ep_sz;
1062 	fs_cfg = p;
1063 	param_data = &fs_cfg->param_data;
1064 	param_data->module_instance_id = module->instance_id;
1065 	param_data->error_code = 0;
1066 	param_data->param_id = PARAM_ID_HW_EP_FRAME_SIZE_FACTOR;
1067 	param_data->param_size = fs_sz - APM_MODULE_PARAM_DATA_SIZE;
1068 	fs_cfg->frame_size_factor = 1;
1069 
1070 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1071 }
1072 
1073 static int audioreach_audio_if_set_media_format(struct q6apm_graph *graph,
1074 						const struct audioreach_module *module,
1075 						const struct audioreach_module_config *cfg)
1076 {
1077 	struct apm_module_hw_ep_frame_duration_cfg *fd_cfg;
1078 	struct apm_module_param_data *param_data;
1079 	struct apm_audio_if_module_intf_cfg *intf_cfg;
1080 	struct apm_module_hw_ep_mf_cfg *hw_cfg;
1081 	int ic_sz = APM_AUDIO_IF_INTF_CFG_PSIZE;
1082 	int ep_sz = APM_HW_EP_CFG_PSIZE;
1083 	int fd_sz = APM_HW_EP_FRAME_DURATION_PSIZE;
1084 	int size = ic_sz + ep_sz + fd_sz;
1085 	u32 slot_mask = cfg->slot_mask ? cfg->slot_mask : module->slot_mask;
1086 	u16 nslots_per_frame = cfg->nslots_per_frame ?
1087 				 (u16)cfg->nslots_per_frame : module->nslots_per_frame;
1088 	u16 slot_width = cfg->slot_width ? (u16)cfg->slot_width : module->slot_width;
1089 	void *p;
1090 
1091 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1092 	if (IS_ERR(pkt))
1093 		return PTR_ERR(pkt);
1094 
1095 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1096 	intf_cfg = p;
1097 
1098 	param_data = &intf_cfg->param_data;
1099 	param_data->module_instance_id = module->instance_id;
1100 	param_data->error_code = 0;
1101 	param_data->param_id = PARAM_ID_AUDIO_IF_INTF_CFG;
1102 	param_data->param_size = ic_sz - APM_MODULE_PARAM_DATA_SIZE;
1103 	intf_cfg->cfg.qaif_type = module->qaif_type;
1104 	intf_cfg->cfg.intf_idx = (u16)module->hw_interface_idx;
1105 	intf_cfg->cfg.intf_mode = module->intf_mode;
1106 	intf_cfg->cfg.ctrl_data_out_enable = module->ctrl_data_out_enable;
1107 	intf_cfg->cfg.active_slot_mask = slot_mask;
1108 	intf_cfg->cfg.nslots_per_frame = nslots_per_frame;
1109 	intf_cfg->cfg.slot_width = slot_width;
1110 	intf_cfg->cfg.active_lane_mask = module->active_lane_mask;
1111 	intf_cfg->cfg.frame_sync_rate = module->frame_sync_rate;
1112 	intf_cfg->cfg.frame_sync_src = module->sync_src;
1113 	intf_cfg->cfg.frame_sync_mode = module->sync_mode;
1114 	intf_cfg->cfg.invert_frame_sync_pulse = module->ctrl_invert_sync_pulse;
1115 	intf_cfg->cfg.frame_sync_data_delay = module->ctrl_sync_data_delay;
1116 	intf_cfg->cfg.bit_clk_type = module->bit_clk_type;
1117 	intf_cfg->cfg.inv_int_bit_clk = module->inv_int_bit_clk;
1118 	intf_cfg->cfg.inv_ext_bit_clk = module->inv_ext_bit_clk;
1119 
1120 	p += ic_sz;
1121 	hw_cfg = p;
1122 	param_data = &hw_cfg->param_data;
1123 	param_data->module_instance_id = module->instance_id;
1124 	param_data->error_code = 0;
1125 	param_data->param_id = PARAM_ID_HW_EP_MF_CFG;
1126 	param_data->param_size = ep_sz - APM_MODULE_PARAM_DATA_SIZE;
1127 
1128 	hw_cfg->mf.sample_rate = cfg->sample_rate;
1129 	hw_cfg->mf.bit_width = cfg->bit_width;
1130 	hw_cfg->mf.num_channels = cfg->num_channels;
1131 	hw_cfg->mf.data_format = module->data_format;
1132 
1133 	p += ep_sz;
1134 	fd_cfg = p;
1135 	param_data = &fd_cfg->param_data;
1136 	param_data->module_instance_id = module->instance_id;
1137 	param_data->error_code = 0;
1138 	param_data->param_id = PARAM_ID_HW_EP_FRAME_DURATION;
1139 	param_data->param_size = fd_sz - APM_MODULE_PARAM_DATA_SIZE;
1140 	fd_cfg->frame_duration.frame_duration_in_us = AUDIO_IF_FRAME_DURATION_US;
1141 	fd_cfg->frame_duration.allow_frame_duration_normalization = AUDIO_IF_FRAME_DURATION_NORMALIZATION_ENABLE;
1142 	fd_cfg->frame_duration.min_normalized_frame_dur_us = AUDIO_IF_FRAME_DURATION_MIN_US;
1143 	fd_cfg->frame_duration.max_normalized_frame_dur_us = AUDIO_IF_FRAME_DURATION_MAX_US;
1144 
1145 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1146 }
1147 
1148 static int audioreach_logging_set_media_format(struct q6apm_graph *graph,
1149 					       const struct audioreach_module *module)
1150 {
1151 	struct apm_module_param_data *param_data;
1152 	struct data_logging_config *cfg;
1153 	int size = sizeof(*cfg) + APM_MODULE_PARAM_DATA_SIZE;
1154 	void *p;
1155 
1156 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1157 	if (IS_ERR(pkt))
1158 		return PTR_ERR(pkt);
1159 
1160 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1161 
1162 	param_data = p;
1163 	param_data->module_instance_id = module->instance_id;
1164 	param_data->error_code = 0;
1165 	param_data->param_id = PARAM_ID_DATA_LOGGING_CONFIG;
1166 	param_data->param_size = size - APM_MODULE_PARAM_DATA_SIZE;
1167 
1168 	p = p + APM_MODULE_PARAM_DATA_SIZE;
1169 	cfg = p;
1170 	cfg->log_code = module->log_code;
1171 	cfg->log_tap_point_id = module->log_tap_point_id;
1172 	cfg->mode = module->log_mode;
1173 
1174 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1175 }
1176 
1177 static int audioreach_pcm_set_media_format(struct q6apm_graph *graph,
1178 					   const struct audioreach_module *module,
1179 					   const struct audioreach_module_config *mcfg)
1180 {
1181 	struct payload_pcm_output_format_cfg *media_cfg;
1182 	uint32_t num_channels = mcfg->num_channels;
1183 	struct apm_pcm_module_media_fmt_cmd *cfg;
1184 	struct apm_module_param_data *param_data;
1185 	int payload_size;
1186 	int i, j;
1187 
1188 	if (num_channels > 4) {
1189 		dev_err(graph->dev, "Error: Invalid channels (%d)!\n", num_channels);
1190 		return -EINVAL;
1191 	}
1192 
1193 	payload_size = APM_PCM_MODULE_FMT_CMD_PSIZE(num_channels);
1194 
1195 	struct gpr_pkt *pkt __free(kfree) =
1196 		audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
1197 	if (IS_ERR(pkt))
1198 		return PTR_ERR(pkt);
1199 
1200 	cfg = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1201 
1202 	param_data = &cfg->param_data;
1203 	param_data->module_instance_id = module->instance_id;
1204 	param_data->error_code = 0;
1205 	param_data->param_id = PARAM_ID_PCM_OUTPUT_FORMAT_CFG;
1206 	param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
1207 
1208 	cfg->header.data_format = DATA_FORMAT_FIXED_POINT;
1209 	cfg->header.fmt_id = MEDIA_FMT_ID_PCM;
1210 	cfg->header.payload_size = APM_PCM_OUT_FMT_CFG_PSIZE(media_cfg, num_channels);
1211 
1212 	media_cfg = &cfg->media_cfg;
1213 	media_cfg->alignment = PCM_LSB_ALIGNED;
1214 	media_cfg->bit_width = mcfg->bit_width;
1215 	media_cfg->endianness = PCM_LITTLE_ENDIAN;
1216 	media_cfg->interleaved = module->interleave_type;
1217 	media_cfg->num_channels = mcfg->num_channels;
1218 	media_cfg->q_factor = mcfg->bit_width - 1;
1219 	media_cfg->bits_per_sample = mcfg->bit_width;
1220 	/* Convert the physical mapping to a logical mapping of the channels */
1221 	for (i = 0, j = 0; i < AR_PCM_MAX_NUM_CHANNEL && j < mcfg->num_channels; i++) {
1222 		if (!mcfg->channel_map[i])
1223 			continue;
1224 		media_cfg->channel_mapping[j++] = mcfg->channel_map[i];
1225 	}
1226 
1227 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1228 }
1229 
1230 int audioreach_shmem_register_event(struct q6apm_graph *graph, int bytes, int num_levels)
1231 {
1232 	struct apm_module_register_events *event;
1233 	struct event_cfg_sh_mem_pull_push_mode_watermark_t *level;
1234 	int i, payload_size;
1235 	struct gpr_pkt *pkt __free(kfree) = NULL;
1236 	void *p;
1237 
1238 	if (num_levels <= 0 || bytes <= 0)
1239 		return -EINVAL;
1240 
1241 	payload_size = sizeof(*event) + sizeof(*level) + num_levels * sizeof(uint32_t);
1242 
1243 	pkt = audioreach_alloc_cmd_pkt(payload_size, APM_CMD_REGISTER_MODULE_EVENTS, 0,
1244 				     graph->port->id, graph->shm_iid);
1245 	if (IS_ERR(pkt))
1246 		return PTR_ERR(pkt);
1247 
1248 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1249 
1250 	event = p;
1251 	event->module_instance_id = graph->shm_iid;
1252 	event->event_id = EVENT_ID_SH_MEM_PULL_PUSH_MODE_WATERMARK;
1253 	event->is_register = 1;
1254 	event->event_config_payload_size = sizeof(*level) + num_levels * sizeof(uint32_t);
1255 	p += sizeof(*event);
1256 	level = p;
1257 	level->num_water_mark_levels = num_levels;
1258 
1259 	for (i = 0; i < num_levels; i++)
1260 		level->level[i] = (i + 1) * bytes;
1261 
1262 	return audioreach_graph_send_cmd_sync(graph, pkt, 0);
1263 }
1264 EXPORT_SYMBOL_GPL(audioreach_shmem_register_event);
1265 
1266 static int audioreach_shmem_set_media_format(struct q6apm_graph *graph,
1267 					     const struct audioreach_module *module,
1268 					     const struct audioreach_module_config *mcfg)
1269 {
1270 	uint32_t num_channels = mcfg->num_channels;
1271 	struct apm_module_param_data *param_data;
1272 	struct payload_media_fmt_pcm *cfg;
1273 	struct media_format *header;
1274 	int rc, payload_size;
1275 	int i, j;
1276 	void *p;
1277 
1278 	if (num_channels > 4) {
1279 		dev_err(graph->dev, "Error: Invalid channels (%d)!\n", num_channels);
1280 		return -EINVAL;
1281 	}
1282 
1283 	payload_size = APM_SHMEM_FMT_CFG_PSIZE(num_channels) + APM_MODULE_PARAM_DATA_SIZE;
1284 
1285 	struct gpr_pkt *pkt __free(kfree) =
1286 		audioreach_alloc_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0,
1287 					 graph->port->id, module->instance_id);
1288 	if (IS_ERR(pkt))
1289 		return PTR_ERR(pkt);
1290 
1291 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1292 
1293 	param_data = p;
1294 	param_data->module_instance_id = module->instance_id;
1295 	param_data->error_code = 0;
1296 	param_data->param_id = PARAM_ID_MEDIA_FORMAT;
1297 	param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
1298 	p = p + APM_MODULE_PARAM_DATA_SIZE;
1299 
1300 	header = p;
1301 	if (mcfg->fmt == SND_AUDIOCODEC_PCM) {
1302 		header->data_format = DATA_FORMAT_FIXED_POINT;
1303 		header->fmt_id =  MEDIA_FMT_ID_PCM;
1304 		header->payload_size = payload_size - sizeof(*header);
1305 
1306 		p = p + sizeof(*header);
1307 		cfg = p;
1308 		cfg->sample_rate = mcfg->sample_rate;
1309 		cfg->bit_width = mcfg->bit_width;
1310 		cfg->alignment = PCM_LSB_ALIGNED;
1311 		cfg->bits_per_sample = mcfg->bit_width;
1312 		cfg->q_factor = mcfg->bit_width - 1;
1313 		cfg->endianness = PCM_LITTLE_ENDIAN;
1314 		cfg->num_channels = mcfg->num_channels;
1315 		/* Convert the physical mapping to a logical mapping of the channels */
1316 		for (i = 0, j = 0; i < AR_PCM_MAX_NUM_CHANNEL && j < cfg->num_channels; i++) {
1317 			if (!mcfg->channel_map[i])
1318 				continue;
1319 			cfg->channel_mapping[j++] = mcfg->channel_map[i];
1320 		}
1321 	} else {
1322 		rc = audioreach_set_compr_media_format(header, p, mcfg);
1323 		if (rc)
1324 			return rc;
1325 	}
1326 
1327 	return audioreach_graph_send_cmd_sync(graph, pkt, 0);
1328 }
1329 
1330 int audioreach_gain_set_vol_ctrl(struct q6apm *apm,
1331 				 const struct audioreach_module *module, int vol)
1332 {
1333 	struct param_id_vol_ctrl_master_gain *cfg;
1334 	struct apm_module_param_data *param_data;
1335 	int size = sizeof(*cfg) + APM_MODULE_PARAM_DATA_SIZE;
1336 	void *p;
1337 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1338 	if (IS_ERR(pkt))
1339 		return PTR_ERR(pkt);
1340 
1341 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1342 
1343 	param_data = p;
1344 	param_data->module_instance_id = module->instance_id;
1345 	param_data->error_code = 0;
1346 	param_data->param_id = PARAM_ID_VOL_CTRL_MASTER_GAIN;
1347 	param_data->param_size = size - APM_MODULE_PARAM_DATA_SIZE;
1348 
1349 	p = p + APM_MODULE_PARAM_DATA_SIZE;
1350 	cfg = p;
1351 	cfg->master_gain =  vol;
1352 	return q6apm_send_cmd_sync(apm, pkt, 0);
1353 }
1354 EXPORT_SYMBOL_GPL(audioreach_gain_set_vol_ctrl);
1355 
1356 static int audioreach_gain_set(struct q6apm_graph *graph,
1357 			       const struct audioreach_module *module)
1358 {
1359 	struct apm_module_param_data *param_data;
1360 	struct apm_gain_module_cfg *cfg;
1361 	int size = APM_GAIN_CFG_PSIZE;
1362 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_apm_cmd_pkt(size, APM_CMD_SET_CFG, 0);
1363 	if (IS_ERR(pkt))
1364 		return PTR_ERR(pkt);
1365 
1366 	cfg = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1367 
1368 	param_data = &cfg->param_data;
1369 	param_data->module_instance_id = module->instance_id;
1370 	param_data->error_code = 0;
1371 	param_data->param_id = APM_PARAM_ID_GAIN;
1372 	param_data->param_size = size - APM_MODULE_PARAM_DATA_SIZE;
1373 
1374 	cfg->gain_cfg.gain = module->gain;
1375 
1376 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1377 }
1378 
1379 static int audioreach_speaker_protection(struct q6apm_graph *graph,
1380 					 const struct audioreach_module *module,
1381 					 uint32_t operation_mode)
1382 {
1383 	return audioreach_send_u32_param(graph, module, PARAM_ID_SP_OP_MODE,
1384 					 operation_mode);
1385 }
1386 
1387 static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,
1388 					    const struct audioreach_module *module,
1389 					    const struct audioreach_module_config *mcfg)
1390 {
1391 	u32 num_channels = mcfg->num_channels;
1392 	struct apm_module_sp_vi_op_mode_cfg *op_cfg;
1393 	struct apm_module_sp_vi_channel_map_cfg *cm_cfg;
1394 	struct apm_module_sp_vi_ex_mode_cfg *ex_cfg;
1395 	int op_sz, cm_sz, ex_sz;
1396 	struct apm_module_param_data *param_data;
1397 	int rc, i, payload_size, j;
1398 	struct gpr_pkt *pkt;
1399 	void *p;
1400 
1401 	if (num_channels > 2) {
1402 		dev_err(graph->dev, "Error: Invalid channels (%d)!\n", num_channels);
1403 		return -EINVAL;
1404 	}
1405 
1406 	op_sz = APM_SP_VI_OP_MODE_CFG_PSIZE(num_channels);
1407 	/* Channel mapping for Isense and Vsense, thus twice number of speakers. */
1408 	cm_sz = APM_SP_VI_CH_MAP_CFG_PSIZE(num_channels * 2);
1409 	ex_sz = APM_SP_VI_EX_MODE_CFG_PSIZE;
1410 
1411 	payload_size = op_sz + cm_sz + ex_sz;
1412 
1413 	pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
1414 	if (IS_ERR(pkt))
1415 		return PTR_ERR(pkt);
1416 
1417 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1418 
1419 	op_cfg = p;
1420 	param_data = &op_cfg->param_data;
1421 	param_data->module_instance_id = module->instance_id;
1422 	param_data->error_code = 0;
1423 	param_data->param_id = PARAM_ID_SP_VI_OP_MODE_CFG;
1424 	param_data->param_size = op_sz - APM_MODULE_PARAM_DATA_SIZE;
1425 
1426 	op_cfg->cfg.num_channels = num_channels;
1427 	op_cfg->cfg.operation_mode = PARAM_ID_SP_VI_OP_MODE_NORMAL;
1428 	p += op_sz;
1429 
1430 	cm_cfg = p;
1431 	param_data = &cm_cfg->param_data;
1432 	param_data->module_instance_id = module->instance_id;
1433 	param_data->error_code = 0;
1434 	param_data->param_id = PARAM_ID_SP_VI_CHANNEL_MAP_CFG;
1435 	param_data->param_size = cm_sz - APM_MODULE_PARAM_DATA_SIZE;
1436 
1437 	cm_cfg->cfg.num_channels = num_channels * 2;
1438 	/* Convert the physical mapping to a logical mapping of the channels */
1439 	for (i = 0, j = 0; i < AR_PCM_MAX_NUM_CHANNEL && j < num_channels; i++) {
1440 		if (!mcfg->channel_map[i])
1441 			continue;
1442 		/*
1443 		 * Map speakers into Vsense and then Isense of each channel.
1444 		 * E.g. for PCM_CHANNEL_FL and PCM_CHANNEL_FR to:
1445 		 * [1, 2, 3, 4]
1446 		 */
1447 		cm_cfg->cfg.channel_mapping[2 * j] = (mcfg->channel_map[i] - 1) * 2 + 1;
1448 		cm_cfg->cfg.channel_mapping[2 * j + 1] = (mcfg->channel_map[i] - 1) * 2 + 2;
1449 
1450 		++j;
1451 	}
1452 
1453 	p += cm_sz;
1454 
1455 	ex_cfg = p;
1456 	param_data = &ex_cfg->param_data;
1457 	param_data->module_instance_id = module->instance_id;
1458 	param_data->error_code = 0;
1459 	param_data->param_id = PARAM_ID_SP_VI_EX_MODE_CFG;
1460 	param_data->param_size = ex_sz - APM_MODULE_PARAM_DATA_SIZE;
1461 
1462 	ex_cfg->cfg.factory_mode = 0;
1463 
1464 	rc = q6apm_send_cmd_sync(graph->apm, pkt, 0);
1465 
1466 	kfree(pkt);
1467 
1468 	return rc;
1469 }
1470 
1471 int audioreach_set_media_format(struct q6apm_graph *graph,
1472 				const struct audioreach_module *module,
1473 				const struct audioreach_module_config *cfg)
1474 {
1475 	int rc;
1476 
1477 	switch (module->module_id) {
1478 	case MODULE_ID_DATA_LOGGING:
1479 		rc = audioreach_module_enable(graph, module, true);
1480 		if (!rc)
1481 			rc = audioreach_logging_set_media_format(graph, module);
1482 		break;
1483 	case MODULE_ID_PCM_DEC:
1484 	case MODULE_ID_PCM_ENC:
1485 	case MODULE_ID_PCM_CNV:
1486 	case MODULE_ID_PLACEHOLDER_DECODER:
1487 	case MODULE_ID_PLACEHOLDER_ENCODER:
1488 		rc = audioreach_pcm_set_media_format(graph, module, cfg);
1489 		break;
1490 	case MODULE_ID_DISPLAY_PORT_SINK:
1491 		rc = audioreach_display_port_set_media_format(graph, module, cfg);
1492 		break;
1493 	case  MODULE_ID_SMECNS_V2:
1494 		rc = audioreach_set_module_config(graph, module, cfg);
1495 		break;
1496 	case MODULE_ID_I2S_SOURCE:
1497 	case MODULE_ID_I2S_SINK:
1498 		rc = audioreach_i2s_set_media_format(graph, module, cfg);
1499 		break;
1500 	case MODULE_ID_WR_SHARED_MEM_EP:
1501 	case MODULE_ID_SH_MEM_PULL_MODE:
1502 		rc = audioreach_shmem_set_media_format(graph, module, cfg);
1503 		break;
1504 	case MODULE_ID_GAIN:
1505 		rc = audioreach_gain_set(graph, module);
1506 		break;
1507 	case MODULE_ID_CODEC_DMA_SINK:
1508 	case MODULE_ID_CODEC_DMA_SOURCE:
1509 		rc = audioreach_codec_dma_set_media_format(graph, module, cfg);
1510 		break;
1511 	case MODULE_ID_SAL:
1512 		rc = audioreach_sal_set_media_format(graph, module, cfg);
1513 		if (!rc)
1514 			rc = audioreach_sal_limiter_enable(graph, module, true);
1515 		break;
1516 	case MODULE_ID_MFC:
1517 		rc = audioreach_mfc_set_media_format(graph, module, cfg);
1518 		break;
1519 	case MODULE_ID_GAPLESS:
1520 		rc = audioreach_gapless_set_media_format(graph, module, cfg);
1521 		break;
1522 	case MODULE_ID_SPEAKER_PROTECTION:
1523 		rc = audioreach_speaker_protection(graph, module,
1524 						   PARAM_ID_SP_OP_MODE_NORMAL);
1525 		if (!rc)
1526 			rc = audioreach_module_enable(graph, module, true);
1527 
1528 		break;
1529 	case MODULE_ID_SPEAKER_PROTECTION_VI:
1530 		rc = audioreach_speaker_protection_vi(graph, module, cfg);
1531 		if (!rc)
1532 			rc = audioreach_module_enable(graph, module, true);
1533 		break;
1534 	case MODULE_ID_AUDIO_IF_SOURCE:
1535 	case MODULE_ID_AUDIO_IF_SINK:
1536 		rc = audioreach_audio_if_set_media_format(graph, module, cfg);
1537 		break;
1538 
1539 	default:
1540 		rc = 0;
1541 	}
1542 
1543 	return rc;
1544 }
1545 EXPORT_SYMBOL_GPL(audioreach_set_media_format);
1546 
1547 void audioreach_graph_free_buf(struct q6apm_graph *graph)
1548 {
1549 	struct audioreach_graph_data *port;
1550 
1551 	mutex_lock(&graph->lock);
1552 	port = &graph->rx_data;
1553 	port->num_periods = 0;
1554 	kfree(port->buf);
1555 	port->buf = NULL;
1556 
1557 	port = &graph->tx_data;
1558 	port->num_periods = 0;
1559 	kfree(port->buf);
1560 	port->buf = NULL;
1561 	mutex_unlock(&graph->lock);
1562 }
1563 EXPORT_SYMBOL_GPL(audioreach_graph_free_buf);
1564 
1565 int audioreach_setup_push_pull(struct q6apm_graph *graph, phys_addr_t bphys,
1566 				phys_addr_t pphys, uint32_t mem_map_handle,
1567 				uint32_t pos_buf_mem_map_handle, uint32_t size)
1568 {
1569 	struct param_id_sh_mem_pull_push_mode_cfg *cfg;
1570 	struct apm_module_param_data *param_data;
1571 	int payload_size;
1572 	struct gpr_pkt *pkt __free(kfree) = NULL;
1573 	void *p;
1574 
1575 	payload_size = sizeof(*cfg) + APM_MODULE_PARAM_DATA_SIZE;
1576 	pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
1577 	if (IS_ERR(pkt))
1578 		return PTR_ERR(pkt);
1579 
1580 	p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1581 
1582 	param_data = p;
1583 	param_data->module_instance_id = graph->shm_iid;
1584 	param_data->error_code = 0;
1585 	param_data->param_id = PARAM_ID_SH_MEM_PULL_PUSH_MODE_CFG;
1586 	param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
1587 
1588 	p = p + APM_MODULE_PARAM_DATA_SIZE;
1589 	cfg = p;
1590 
1591 	cfg->shared_circ_buf_addr_lsw = lower_32_bits(bphys);
1592 	cfg->shared_circ_buf_addr_msw = upper_32_bits(bphys);
1593 	cfg->shared_circ_buf_size = size;
1594 	cfg->circ_buf_mem_map_handle = mem_map_handle;
1595 	cfg->shared_pos_buf_addr_lsw = lower_32_bits(pphys);
1596 	cfg->shared_pos_buf_addr_msw = upper_32_bits(pphys);
1597 	cfg->pos_buf_mem_map_handle = pos_buf_mem_map_handle;
1598 
1599 	return q6apm_send_cmd_sync(graph->apm, pkt, 0);
1600 }
1601 EXPORT_SYMBOL_GPL(audioreach_setup_push_pull);
1602 
1603 int audioreach_shared_memory_send_eos(struct q6apm_graph *graph)
1604 {
1605 	struct data_cmd_wr_sh_mem_ep_eos *eos;
1606 	int iid = graph->shm_iid;
1607 	struct gpr_pkt *pkt __free(kfree) = audioreach_alloc_cmd_pkt(sizeof(*eos),
1608 					DATA_CMD_WR_SH_MEM_EP_EOS, 0, graph->port->id, iid);
1609 	if (IS_ERR(pkt))
1610 		return PTR_ERR(pkt);
1611 
1612 	eos = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1613 
1614 	eos->policy = WR_SH_MEM_EP_EOS_POLICY_LAST;
1615 
1616 	pkt->hdr.dest_domain = audioreach_gpr_dest_domain(graph->apm->gdev);
1617 
1618 	return gpr_send_port_pkt(graph->port, pkt);
1619 }
1620 EXPORT_SYMBOL_GPL(audioreach_shared_memory_send_eos);
1621