1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020, Linaro Limited
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/soc/qcom/apr.h>
8 #include <sound/soc-dai.h>
10 #include <sound/pcm_params.h>
11 #include <dt-bindings/soc/qcom,gpr.h>
13 #include "audioreach.h"
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;
27 #define APM_SUB_GRAPH_CFG_NPROP 3
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[];
35 #define APM_SUB_GRAPH_PSIZE(p, n) ALIGN(struct_size(p, sg_cfg, n), 8)
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;
45 /* Container graph Position */
46 struct apm_prop_data pos_data;
47 struct apm_cont_prop_id_graph_pos pos;
49 /* Container Stack size */
50 struct apm_prop_data stack_data;
51 struct apm_cont_prop_id_stack_size stack;
53 /* Container proc domain id */
54 struct apm_prop_data domain_data;
55 struct apm_cont_prop_id_domain domain;
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[];
64 #define APM_CONTAINER_PSIZE(p, n) ALIGN(struct_size(p, cont_obj, n), 8)
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;
72 struct apm_module_obj mod_cfg[];
75 #define APM_MOD_LIST_OBJ_PSIZE(p, n) struct_size(p, mod_cfg, n)
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[];
85 /* Module Properties */
86 struct apm_mod_prop_obj {
89 struct apm_prop_data prop_data_1;
90 struct apm_module_prop_id_port_info prop_id_port;
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[];
100 #define APM_MOD_PROP_PSIZE(p, n) ALIGN(struct_size(p, mod_prop_obj, n), 8)
102 /* Module Connections */
103 struct apm_mod_conn_list_params {
104 struct apm_module_param_data param_data;
106 struct apm_module_conn_obj conn_obj[];
110 #define APM_MOD_CONN_PSIZE(p, n) ALIGN(struct_size(p, conn_obj, n), 8)
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;
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;
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;
132 struct apm_sh_module_media_fmt_cmd {
133 struct media_format header;
134 struct payload_media_fmt_pcm cfg;
137 #define APM_SHMEM_FMT_CFG_PSIZE(ch) ALIGN( \
138 sizeof(struct apm_sh_module_media_fmt_cmd) + \
139 ch * sizeof(uint8_t), 8)
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)
146 #define APM_PCM_OUT_FMT_CFG_PSIZE(p, n) ALIGN(struct_size(p, channel_mapping, n), 4)
148 struct apm_i2s_module_intf_cfg {
149 struct apm_module_param_data param_data;
150 struct param_id_i2s_intf_cfg cfg;
153 #define APM_I2S_INTF_CFG_PSIZE ALIGN(sizeof(struct apm_i2s_module_intf_cfg), 8)
155 struct apm_module_hw_ep_mf_cfg {
156 struct apm_module_param_data param_data;
157 struct param_id_hw_ep_mf mf;
160 #define APM_HW_EP_CFG_PSIZE ALIGN(sizeof(struct apm_module_hw_ep_mf_cfg), 8)
162 #define APM_MFC_CFG_PSIZE(p, n) ALIGN(struct_size(p, channel_mapping, n), 4)
164 struct apm_module_frame_size_factor_cfg {
165 struct apm_module_param_data param_data;
166 uint32_t frame_size_factor;
169 #define APM_FS_CFG_PSIZE ALIGN(sizeof(struct apm_module_frame_size_factor_cfg), 8)
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;
176 #define APM_HW_EP_PMODE_CFG_PSIZE ALIGN(sizeof(struct apm_module_hw_ep_power_mode_cfg), 8)
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;
183 #define APM_HW_EP_DALIGN_CFG_PSIZE ALIGN(sizeof(struct apm_module_hw_ep_dma_data_align_cfg), 8)
185 struct apm_gain_module_cfg {
186 struct apm_module_param_data param_data;
187 struct param_id_gain_cfg gain_cfg;
190 #define APM_GAIN_CFG_PSIZE ALIGN(sizeof(struct apm_gain_module_cfg), 8)
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;
197 #define APM_CDMA_INTF_CFG_PSIZE ALIGN(sizeof(struct apm_codec_dma_module_intf_cfg), 8)
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)
204 int pkt_size = GPR_HDR_SIZE + payload_size;
207 pkt_size += APM_CMD_HDR_SIZE;
209 p = kzalloc(pkt_size, GFP_KERNEL);
211 return ERR_PTR(-ENOMEM);
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;
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;
226 struct apm_cmd_header *cmd_header;
228 p = p + GPR_HDR_SIZE;
230 cmd_header->payload_size = payload_size;
236 void *audioreach_alloc_pkt(int payload_size, uint32_t opcode, uint32_t token,
237 uint32_t src_port, uint32_t dest_port)
239 return __audioreach_alloc_pkt(payload_size, opcode, token, src_port, dest_port, false);
241 EXPORT_SYMBOL_GPL(audioreach_alloc_pkt);
243 void *audioreach_alloc_apm_pkt(int pkt_size, uint32_t opcode, uint32_t token, uint32_t src_port)
245 return __audioreach_alloc_pkt(pkt_size, opcode, token, src_port, APM_MODULE_INSTANCE_ID,
248 EXPORT_SYMBOL_GPL(audioreach_alloc_apm_pkt);
250 void *audioreach_alloc_cmd_pkt(int payload_size, uint32_t opcode, uint32_t token,
251 uint32_t src_port, uint32_t dest_port)
253 return __audioreach_alloc_pkt(payload_size, opcode, token, src_port, dest_port, true);
255 EXPORT_SYMBOL_GPL(audioreach_alloc_cmd_pkt);
257 void *audioreach_alloc_apm_cmd_pkt(int pkt_size, uint32_t opcode, uint32_t token)
259 return __audioreach_alloc_pkt(pkt_size, opcode, token, GPR_APM_MODULE_IID,
260 APM_MODULE_INSTANCE_ID, true);
262 EXPORT_SYMBOL_GPL(audioreach_alloc_apm_cmd_pkt);
264 static void apm_populate_container_config(struct apm_container_obj *cfg,
265 struct audioreach_container *cont)
268 /* Container Config */
269 cfg->container_cfg.container_id = cont->container_id;
270 cfg->container_cfg.num_prop = 4;
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;
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;
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;
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;
294 static void apm_populate_sub_graph_config(struct apm_sub_graph_data *cfg,
295 struct audioreach_sub_graph *sg)
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;
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;
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;
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;
316 static void apm_populate_module_prop_obj(struct apm_mod_prop_obj *obj,
317 struct audioreach_module *module)
320 obj->instance_id = module->instance_id;
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;
328 static void apm_populate_module_list_obj(struct apm_mod_list_obj *obj,
329 struct audioreach_container *container,
332 struct audioreach_module *module;
335 obj->sub_graph_id = sub_graph_id;
336 obj->container_id = container->container_id;
337 obj->num_modules = container->num_modules;
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;
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,
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;
366 mlobj = &ml_data->mod_list_obj[0];
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;
378 list_for_each_entry(sg, sg_list, node) {
379 struct apm_sub_graph_data *sg_cfg = &sg_data->sg_cfg[i++];
381 apm_populate_sub_graph_config(sg_cfg, sg);
383 list_for_each_entry(container, &sg->container_list, node) {
384 cobj = &c_data->cont_obj[ncontainer];
386 apm_populate_container_config(cobj, container);
387 apm_populate_module_list_obj(mlobj, container, sg->sub_graph_id);
389 list_for_each_entry(module, &container->modules_list, node) {
392 module_prop_obj = &mp_data->mod_prop_obj[nmodule++];
393 apm_populate_module_prop_obj(module_prop_obj, module);
395 if (!module->max_op_port)
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];
412 mlobj = (void *) mlobj + APM_MOD_LIST_OBJ_PSIZE(mlobj,
413 container->num_modules);
420 void *audioreach_alloc_graph_pkt(struct q6apm *apm, struct audioreach_graph_info *info)
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;
438 int num_modules_list;
442 sg_list = &info->sg_list;
445 /* add FE-BE connections */
446 if (info->dst_mod_inst_id && info->src_mod_inst_id)
449 list_for_each_entry(sgs, sg_list, node) {
451 list_for_each_entry(container, &sgs->container_list, node) {
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);
457 list_for_each_entry(module, &container->modules_list, node) {
458 num_connections += module->num_connections;
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);
467 ml_sz = ALIGN(ml_sz, 8);
469 mp_sz = APM_MOD_PROP_PSIZE(mprop, num_modules);
470 mc_sz = APM_MOD_CONN_PSIZE(mcon, num_connections);
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);
477 p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
481 param_data = ¶ms.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;
489 params.cont_data = p;
490 param_data = ¶ms.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;
498 params.mod_list_data = p;
499 param_data = ¶ms.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;
506 /* Module Properties */
507 params.mod_prop_data = p;
508 param_data = ¶ms.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;
515 /* Module Connections */
516 params.mod_conn_list_data = p;
517 param_data = ¶ms.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;
524 audioreach_populate_graph(apm, info, ¶ms, sg_list, num_sub_graphs);
528 EXPORT_SYMBOL_GPL(audioreach_alloc_graph_pkt);
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)
536 struct gpr_hdr *hdr = &pkt->hdr;
539 mutex_lock(cmd_lock);
544 rc = gpr_send_port_pkt(port, pkt);
546 rc = gpr_send_pkt(gdev, pkt);
554 rc = wait_event_timeout(*cmd_wait, (result->opcode == hdr->opcode) ||
555 (result->opcode == rsp_opcode), 5 * HZ);
557 rc = wait_event_timeout(*cmd_wait, (result->opcode == hdr->opcode), 5 * HZ);
560 dev_err(dev, "CMD timeout for [%x] opcode\n", hdr->opcode);
562 } else if (result->status > 0) {
563 dev_err(dev, "DSP returned error[%x] %x\n", hdr->opcode, result->status);
566 /* DSP successfully finished the command */
571 mutex_unlock(cmd_lock);
574 EXPORT_SYMBOL_GPL(audioreach_send_cmd_sync);
576 int audioreach_graph_send_cmd_sync(struct q6apm_graph *graph, struct gpr_pkt *pkt,
580 return audioreach_send_cmd_sync(graph->dev, NULL, &graph->result, &graph->lock,
581 graph->port, &graph->cmd_wait, pkt, rsp_opcode);
583 EXPORT_SYMBOL_GPL(audioreach_graph_send_cmd_sync);
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)
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;
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;
606 payload_size = ic_sz + ep_sz + fs_sz + pm_sz + dl_sz;
608 pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
612 p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
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;
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;
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;
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;
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;
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;
656 rc = q6apm_send_cmd_sync(graph->apm, pkt, 0);
663 static int audioreach_sal_limiter_enable(struct q6apm_graph *graph,
664 struct audioreach_module *module, bool enable)
666 struct apm_module_param_data *param_data;
667 struct param_id_sal_limiter_enable *limiter_enable;
673 payload_size = sizeof(*limiter_enable) + APM_MODULE_PARAM_DATA_SIZE;
675 pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
679 p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
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;
689 limiter_enable->enable_lim = enable;
691 rc = q6apm_send_cmd_sync(graph->apm, pkt, 0);
698 static int audioreach_sal_set_media_format(struct q6apm_graph *graph,
699 struct audioreach_module *module,
700 struct audioreach_module_config *cfg)
702 struct apm_module_param_data *param_data;
703 struct param_id_sal_output_config *media_format;
709 payload_size = sizeof(*media_format) + APM_MODULE_PARAM_DATA_SIZE;
711 pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
715 p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
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;
725 media_format->bits_per_sample = cfg->bit_width;
727 rc = q6apm_send_cmd_sync(graph->apm, pkt, 0);
734 static int audioreach_module_enable(struct q6apm_graph *graph,
735 struct audioreach_module *module,
738 struct apm_module_param_data *param_data;
739 struct param_id_module_enable *param;
745 payload_size = sizeof(*param) + APM_MODULE_PARAM_DATA_SIZE;
747 pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
751 p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
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;
761 param->enable = enable;
763 rc = q6apm_send_cmd_sync(graph->apm, pkt, 0);
770 static int audioreach_mfc_set_media_format(struct q6apm_graph *graph,
771 struct audioreach_module *module,
772 struct audioreach_module_config *cfg)
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;
782 payload_size = APM_MFC_CFG_PSIZE(media_format, num_channels) +
783 APM_MODULE_PARAM_DATA_SIZE;
785 pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
789 p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
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;
799 media_format->sample_rate = cfg->sample_rate;
800 media_format->bit_width = cfg->bit_width;
801 media_format->num_channels = cfg->num_channels;
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;
810 rc = q6apm_send_cmd_sync(graph->apm, pkt, 0);
817 static int audioreach_i2s_set_media_format(struct q6apm_graph *graph,
818 struct audioreach_module *module,
819 struct audioreach_module_config *cfg)
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;
830 ic_sz = APM_I2S_INTF_CFG_PSIZE;
831 ep_sz = APM_HW_EP_CFG_PSIZE;
832 fs_sz = APM_FS_CFG_PSIZE;
834 payload_size = ic_sz + ep_sz + fs_sz;
836 pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
840 p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
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;
849 intf_cfg->cfg.intf_idx = module->hw_interface_idx;
850 intf_cfg->cfg.sd_line_idx = module->sd_line_idx;
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;
856 case SND_SOC_DAIFMT_BC_FC:
858 intf_cfg->cfg.ws_src = CONFIG_I2S_WS_SRC_EXTERNAL;
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;
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;
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;
886 rc = q6apm_send_cmd_sync(graph->apm, pkt, 0);
893 static int audioreach_logging_set_media_format(struct q6apm_graph *graph,
894 struct audioreach_module *module)
896 struct apm_module_param_data *param_data;
897 struct data_logging_config *cfg;
898 int rc, payload_size;
902 payload_size = sizeof(*cfg) + APM_MODULE_PARAM_DATA_SIZE;
903 pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
907 p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
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;
915 p = p + APM_MODULE_PARAM_DATA_SIZE;
917 cfg->log_code = module->log_code;
918 cfg->log_tap_point_id = module->log_tap_point_id;
919 cfg->mode = module->log_mode;
921 rc = q6apm_send_cmd_sync(graph->apm, pkt, 0);
928 static int audioreach_pcm_set_media_format(struct q6apm_graph *graph,
929 struct audioreach_module *module,
930 struct audioreach_module_config *mcfg)
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;
939 if (num_channels > 2) {
940 dev_err(graph->dev, "Error: Invalid channels (%d)!\n", num_channels);
944 payload_size = APM_PCM_MODULE_FMT_CMD_PSIZE(num_channels);
946 pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
950 cfg = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
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;
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);
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;
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;
979 rc = q6apm_send_cmd_sync(graph->apm, pkt, 0);
986 static int audioreach_shmem_set_media_format(struct q6apm_graph *graph,
987 struct audioreach_module *module,
988 struct audioreach_module_config *mcfg)
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;
998 if (num_channels > 2) {
999 dev_err(graph->dev, "Error: Invalid channels (%d)!\n", num_channels);
1003 payload_size = APM_SHMEM_FMT_CFG_PSIZE(num_channels) + APM_MODULE_PARAM_DATA_SIZE;
1005 pkt = audioreach_alloc_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0,
1006 graph->port->id, module->instance_id);
1008 return PTR_ERR(pkt);
1010 p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
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;
1020 header->data_format = DATA_FORMAT_FIXED_POINT;
1021 header->fmt_id = MEDIA_FMT_ID_PCM;
1022 header->payload_size = payload_size - sizeof(*header);
1024 p = p + sizeof(*header);
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;
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;
1041 rc = audioreach_graph_send_cmd_sync(graph, pkt, 0);
1048 int audioreach_gain_set_vol_ctrl(struct q6apm *apm, struct audioreach_module *module, int vol)
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;
1056 payload_size = sizeof(*cfg) + APM_MODULE_PARAM_DATA_SIZE;
1057 pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
1059 return PTR_ERR(pkt);
1061 p = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
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;
1069 p = p + APM_MODULE_PARAM_DATA_SIZE;
1071 cfg->master_gain = vol;
1072 rc = q6apm_send_cmd_sync(apm, pkt, 0);
1078 EXPORT_SYMBOL_GPL(audioreach_gain_set_vol_ctrl);
1080 static int audioreach_gain_set(struct q6apm_graph *graph, struct audioreach_module *module)
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;
1087 payload_size = APM_GAIN_CFG_PSIZE;
1088 pkt = audioreach_alloc_apm_cmd_pkt(payload_size, APM_CMD_SET_CFG, 0);
1090 return PTR_ERR(pkt);
1092 cfg = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
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;
1100 cfg->gain_cfg.gain = module->gain;
1102 rc = q6apm_send_cmd_sync(graph->apm, pkt, 0);
1109 int audioreach_set_media_format(struct q6apm_graph *graph, struct audioreach_module *module,
1110 struct audioreach_module_config *cfg)
1114 switch (module->module_id) {
1115 case MODULE_ID_DATA_LOGGING:
1116 rc = audioreach_module_enable(graph, module, true);
1118 rc = audioreach_logging_set_media_format(graph, module);
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);
1125 case MODULE_ID_I2S_SOURCE:
1126 case MODULE_ID_I2S_SINK:
1127 rc = audioreach_i2s_set_media_format(graph, module, cfg);
1129 case MODULE_ID_WR_SHARED_MEM_EP:
1130 rc = audioreach_shmem_set_media_format(graph, module, cfg);
1132 case MODULE_ID_GAIN:
1133 rc = audioreach_gain_set(graph, module);
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);
1140 rc = audioreach_sal_set_media_format(graph, module, cfg);
1142 rc = audioreach_sal_limiter_enable(graph, module, true);
1145 rc = audioreach_mfc_set_media_format(graph, module, cfg);
1153 EXPORT_SYMBOL_GPL(audioreach_set_media_format);
1155 void audioreach_graph_free_buf(struct q6apm_graph *graph)
1157 struct audioreach_graph_data *port;
1159 mutex_lock(&graph->lock);
1160 port = &graph->rx_data;
1161 port->num_periods = 0;
1165 port = &graph->tx_data;
1166 port->num_periods = 0;
1169 mutex_unlock(&graph->lock);
1171 EXPORT_SYMBOL_GPL(audioreach_graph_free_buf);
1173 int audioreach_map_memory_regions(struct q6apm_graph *graph, unsigned int dir, size_t period_sz,
1174 unsigned int periods, bool is_contiguous)
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;
1184 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
1185 data = &graph->rx_data;
1187 data = &graph->tx_data;
1189 if (is_contiguous) {
1191 buf_sz = period_sz * periods;
1194 num_regions = periods;
1197 /* DSP expects size should be aligned to 4K */
1198 buf_sz = ALIGN(buf_sz, 4096);
1200 payload_size = sizeof(*cmd) + (sizeof(*mregions) * num_regions);
1202 pkt = audioreach_alloc_apm_pkt(payload_size, APM_CMD_SHARED_MEM_MAP_REGIONS, dir,
1205 return PTR_ERR(pkt);
1207 p = (void *)pkt + GPR_HDR_SIZE;
1209 cmd->mem_pool_id = APM_MEMORY_MAP_SHMEM8_4K_POOL;
1210 cmd->num_regions = num_regions;
1212 cmd->property_flag = 0x0;
1214 mregions = p + sizeof(*cmd);
1216 mutex_lock(&graph->lock);
1218 for (i = 0; i < num_regions; i++) {
1219 struct audio_buffer *ab;
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;
1227 mutex_unlock(&graph->lock);
1229 rc = audioreach_graph_send_cmd_sync(graph, pkt, APM_CMD_RSP_SHARED_MEM_MAP_REGIONS);
1235 EXPORT_SYMBOL_GPL(audioreach_map_memory_regions);
1237 int audioreach_shared_memory_send_eos(struct q6apm_graph *graph)
1239 struct data_cmd_wr_sh_mem_ep_eos *eos;
1240 struct gpr_pkt *pkt;
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);
1247 return PTR_ERR(pkt);
1249 eos = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
1251 eos->policy = WR_SH_MEM_EP_EOS_POLICY_LAST;
1253 rc = gpr_send_port_pkt(graph->port, pkt);
1258 EXPORT_SYMBOL_GPL(audioreach_shared_memory_send_eos);