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