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