1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020, Linaro Limited
4 #include <dt-bindings/soc/qcom,gpr.h>
5 #include <linux/delay.h>
6 #include <linux/jiffies.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
10 #include <linux/of_platform.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <linux/soc/qcom/apr.h>
14 #include <linux/wait.h>
15 #include <sound/soc.h>
16 #include <sound/soc-dapm.h>
17 #include <sound/pcm.h>
18 #include "audioreach.h"
21 /* Graph Management */
22 struct apm_graph_mgmt_cmd {
23 struct apm_module_param_data param_data;
24 uint32_t num_sub_graphs;
25 uint32_t sub_graph_id_list[];
28 #define APM_GRAPH_MGMT_PSIZE(p, n) ALIGN(struct_size(p, sub_graph_id_list, n), 8)
30 int q6apm_send_cmd_sync(struct q6apm *apm, struct gpr_pkt *pkt, uint32_t rsp_opcode)
32 gpr_device_t *gdev = apm->gdev;
34 return audioreach_send_cmd_sync(&gdev->dev, gdev, &apm->result, &apm->lock,
35 NULL, &apm->wait, pkt, rsp_opcode);
38 static struct audioreach_graph *q6apm_get_audioreach_graph(struct q6apm *apm, uint32_t graph_id)
40 struct audioreach_graph_info *info;
41 struct audioreach_graph *graph;
44 mutex_lock(&apm->lock);
45 graph = idr_find(&apm->graph_idr, graph_id);
46 mutex_unlock(&apm->lock);
49 kref_get(&graph->refcount);
53 info = idr_find(&apm->graph_info_idr, graph_id);
56 return ERR_PTR(-ENODEV);
58 graph = kzalloc(sizeof(*graph), GFP_KERNEL);
60 return ERR_PTR(-ENOMEM);
66 graph->graph = audioreach_alloc_graph_pkt(apm, &info->sg_list, graph_id);
67 if (IS_ERR(graph->graph)) {
68 void *err = graph->graph;
74 mutex_lock(&apm->lock);
75 id = idr_alloc(&apm->graph_idr, graph, graph_id, graph_id + 1, GFP_KERNEL);
77 dev_err(apm->dev, "Unable to allocate graph id (%d)\n", graph_id);
79 mutex_unlock(&apm->lock);
82 mutex_unlock(&apm->lock);
84 kref_init(&graph->refcount);
86 q6apm_send_cmd_sync(apm, graph->graph, 0);
91 static int audioreach_graph_mgmt_cmd(struct audioreach_graph *graph, uint32_t opcode)
93 struct audioreach_graph_info *info = graph->info;
94 int num_sub_graphs = info->num_sub_graphs;
95 struct apm_module_param_data *param_data;
96 struct apm_graph_mgmt_cmd *mgmt_cmd;
97 struct audioreach_sub_graph *sg;
98 struct q6apm *apm = graph->apm;
99 int i = 0, rc, payload_size;
102 payload_size = APM_GRAPH_MGMT_PSIZE(mgmt_cmd, num_sub_graphs);
104 pkt = audioreach_alloc_apm_cmd_pkt(payload_size, opcode, 0);
108 mgmt_cmd = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
110 mgmt_cmd->num_sub_graphs = num_sub_graphs;
112 param_data = &mgmt_cmd->param_data;
113 param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
114 param_data->param_id = APM_PARAM_ID_SUB_GRAPH_LIST;
115 param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
117 list_for_each_entry(sg, &info->sg_list, node)
118 mgmt_cmd->sub_graph_id_list[i++] = sg->sub_graph_id;
120 rc = q6apm_send_cmd_sync(apm, pkt, 0);
127 static void q6apm_put_audioreach_graph(struct kref *ref)
129 struct audioreach_graph *graph;
132 graph = container_of(ref, struct audioreach_graph, refcount);
135 audioreach_graph_mgmt_cmd(graph, APM_CMD_GRAPH_CLOSE);
137 mutex_lock(&apm->lock);
138 graph = idr_remove(&apm->graph_idr, graph->id);
139 mutex_unlock(&apm->lock);
145 static int q6apm_get_apm_state(struct q6apm *apm)
149 pkt = audioreach_alloc_apm_cmd_pkt(0, APM_CMD_GET_SPF_STATE, 0);
153 q6apm_send_cmd_sync(apm, pkt, APM_CMD_RSP_GET_SPF_STATE);
160 static struct audioreach_module *__q6apm_find_module_by_mid(struct q6apm *apm,
161 struct audioreach_graph_info *info,
164 struct audioreach_container *container;
165 struct audioreach_sub_graph *sgs;
166 struct audioreach_module *module;
168 list_for_each_entry(sgs, &info->sg_list, node) {
169 list_for_each_entry(container, &sgs->container_list, node) {
170 list_for_each_entry(module, &container->modules_list, node) {
171 if (mid == module->module_id)
180 static struct audioreach_module *q6apm_graph_get_last_module(struct q6apm *apm, u32 sgid)
182 struct audioreach_container *container;
183 struct audioreach_module *module;
184 struct audioreach_sub_graph *sg;
186 mutex_lock(&apm->lock);
187 sg = idr_find(&apm->sub_graphs_idr, sgid);
188 mutex_unlock(&apm->lock);
192 container = list_last_entry(&sg->container_list, struct audioreach_container, node);
193 module = audioreach_get_container_last_module(container);
198 static struct audioreach_module *q6apm_graph_get_first_module(struct q6apm *apm, u32 sgid)
200 struct audioreach_container *container;
201 struct audioreach_module *module;
202 struct audioreach_sub_graph *sg;
204 mutex_lock(&apm->lock);
205 sg = idr_find(&apm->sub_graphs_idr, sgid);
206 mutex_unlock(&apm->lock);
210 container = list_first_entry(&sg->container_list, struct audioreach_container, node);
211 module = audioreach_get_container_first_module(container);
216 bool q6apm_is_sub_graphs_connected(struct q6apm *apm, u32 src_sgid, u32 dst_sgid)
218 struct audioreach_module *module;
221 module = q6apm_graph_get_last_module(apm, src_sgid);
225 iid = module->instance_id;
226 module = q6apm_graph_get_first_module(apm, dst_sgid);
230 if (module->src_mod_inst_id == iid)
236 int q6apm_connect_sub_graphs(struct q6apm *apm, u32 src_sgid, u32 dst_sgid, bool connect)
238 struct audioreach_module *module;
242 module = q6apm_graph_get_last_module(apm, src_sgid);
246 iid = module->instance_id;
251 module = q6apm_graph_get_first_module(apm, dst_sgid);
255 /* set src module in dst subgraph first module */
256 module->src_mod_inst_id = iid;
261 int q6apm_graph_get_rx_shmem_module_iid(struct q6apm_graph *graph)
263 struct audioreach_module *module;
265 module = q6apm_find_module_by_mid(graph, MODULE_ID_WR_SHARED_MEM_EP);
269 return module->instance_id;
272 EXPORT_SYMBOL_GPL(q6apm_graph_get_rx_shmem_module_iid);
274 static int graph_callback(struct gpr_resp_pkt *data, void *priv, int op)
276 struct data_cmd_rsp_rd_sh_mem_ep_data_buffer_done_v2 *rd_done;
277 struct data_cmd_rsp_wr_sh_mem_ep_data_buffer_done_v2 *done;
278 struct apm_cmd_rsp_shared_mem_map_regions *rsp;
279 struct gpr_ibasic_rsp_result_t *result;
280 struct q6apm_graph *graph = priv;
281 struct gpr_hdr *hdr = &data->hdr;
282 struct device *dev = graph->dev;
283 uint32_t client_event;
287 result = data->payload;
289 switch (hdr->opcode) {
290 case DATA_CMD_RSP_WR_SH_MEM_EP_DATA_BUFFER_DONE_V2:
291 client_event = APM_CLIENT_EVENT_DATA_WRITE_DONE;
292 mutex_lock(&graph->lock);
293 token = hdr->token & APM_WRITE_TOKEN_MASK;
295 done = data->payload;
296 phys = graph->rx_data.buf[token].phys;
297 mutex_unlock(&graph->lock);
299 if (lower_32_bits(phys) == done->buf_addr_lsw &&
300 upper_32_bits(phys) == done->buf_addr_msw) {
301 graph->result.opcode = hdr->opcode;
302 graph->result.status = done->status;
304 graph->cb(client_event, hdr->token, data->payload, graph->priv);
306 dev_err(dev, "WR BUFF Unexpected addr %08x-%08x\n", done->buf_addr_lsw,
311 case APM_CMD_RSP_SHARED_MEM_MAP_REGIONS:
312 graph->result.opcode = hdr->opcode;
313 graph->result.status = 0;
316 if (hdr->token == SNDRV_PCM_STREAM_PLAYBACK)
317 graph->rx_data.mem_map_handle = rsp->mem_map_handle;
319 graph->tx_data.mem_map_handle = rsp->mem_map_handle;
321 wake_up(&graph->cmd_wait);
323 case DATA_CMD_RSP_RD_SH_MEM_EP_DATA_BUFFER_V2:
324 client_event = APM_CLIENT_EVENT_DATA_READ_DONE;
325 mutex_lock(&graph->lock);
326 rd_done = data->payload;
327 phys = graph->tx_data.buf[hdr->token].phys;
328 mutex_unlock(&graph->lock);
330 if (upper_32_bits(phys) == rd_done->buf_addr_msw &&
331 lower_32_bits(phys) == rd_done->buf_addr_lsw) {
332 graph->result.opcode = hdr->opcode;
333 graph->result.status = rd_done->status;
335 graph->cb(client_event, hdr->token, data->payload, graph->priv);
337 dev_err(dev, "RD BUFF Unexpected addr %08x-%08x\n", rd_done->buf_addr_lsw,
338 rd_done->buf_addr_msw);
341 case DATA_CMD_WR_SH_MEM_EP_EOS_RENDERED:
343 case GPR_BASIC_RSP_RESULT:
344 switch (result->opcode) {
345 case APM_CMD_SHARED_MEM_UNMAP_REGIONS:
346 graph->result.opcode = result->opcode;
347 graph->result.status = 0;
348 if (hdr->token == SNDRV_PCM_STREAM_PLAYBACK)
349 graph->rx_data.mem_map_handle = 0;
351 graph->tx_data.mem_map_handle = 0;
353 wake_up(&graph->cmd_wait);
355 case APM_CMD_SHARED_MEM_MAP_REGIONS:
356 case DATA_CMD_WR_SH_MEM_EP_MEDIA_FORMAT:
357 case APM_CMD_SET_CFG:
358 graph->result.opcode = result->opcode;
359 graph->result.status = result->status;
361 dev_err(dev, "Error (%d) Processing 0x%08x cmd\n",
362 result->status, result->opcode);
363 wake_up(&graph->cmd_wait);
375 struct q6apm_graph *q6apm_graph_open(struct device *dev, q6apm_cb cb,
376 void *priv, int graph_id)
378 struct q6apm *apm = dev_get_drvdata(dev->parent);
379 struct audioreach_graph *ar_graph;
380 struct q6apm_graph *graph;
383 ar_graph = q6apm_get_audioreach_graph(apm, graph_id);
384 if (IS_ERR(ar_graph)) {
385 dev_err(dev, "No graph found with id %d\n", graph_id);
386 return ERR_CAST(ar_graph);
389 graph = kzalloc(sizeof(*graph), GFP_KERNEL);
398 graph->info = ar_graph->info;
399 graph->ar_graph = ar_graph;
400 graph->id = ar_graph->id;
403 mutex_init(&graph->lock);
404 init_waitqueue_head(&graph->cmd_wait);
406 graph->port = gpr_alloc_port(apm->gdev, dev, graph_callback, graph);
415 kref_put(&ar_graph->refcount, q6apm_put_audioreach_graph);
418 EXPORT_SYMBOL_GPL(q6apm_graph_open);
420 int q6apm_graph_close(struct q6apm_graph *graph)
422 struct audioreach_graph *ar_graph = graph->ar_graph;
424 gpr_free_port(graph->port);
425 kref_put(&ar_graph->refcount, q6apm_put_audioreach_graph);
430 EXPORT_SYMBOL_GPL(q6apm_graph_close);
432 int q6apm_graph_prepare(struct q6apm_graph *graph)
434 return audioreach_graph_mgmt_cmd(graph->ar_graph, APM_CMD_GRAPH_PREPARE);
436 EXPORT_SYMBOL_GPL(q6apm_graph_prepare);
438 int q6apm_graph_start(struct q6apm_graph *graph)
440 struct audioreach_graph *ar_graph = graph->ar_graph;
443 if (ar_graph->start_count == 0)
444 ret = audioreach_graph_mgmt_cmd(ar_graph, APM_CMD_GRAPH_START);
446 ar_graph->start_count++;
450 EXPORT_SYMBOL_GPL(q6apm_graph_start);
452 int q6apm_graph_stop(struct q6apm_graph *graph)
454 struct audioreach_graph *ar_graph = graph->ar_graph;
456 if (--ar_graph->start_count > 0)
459 return audioreach_graph_mgmt_cmd(ar_graph, APM_CMD_GRAPH_STOP);
461 EXPORT_SYMBOL_GPL(q6apm_graph_stop);
463 int q6apm_graph_flush(struct q6apm_graph *graph)
465 return audioreach_graph_mgmt_cmd(graph->ar_graph, APM_CMD_GRAPH_FLUSH);
467 EXPORT_SYMBOL_GPL(q6apm_graph_flush);
469 static int q6apm_audio_probe(struct snd_soc_component *component)
474 static void q6apm_audio_remove(struct snd_soc_component *component)
476 /* remove topology */
477 snd_soc_tplg_component_remove(component);
480 #define APM_AUDIO_DRV_NAME "q6apm-audio"
482 static const struct snd_soc_component_driver q6apm_audio_component = {
483 .name = APM_AUDIO_DRV_NAME,
484 .probe = q6apm_audio_probe,
485 .remove = q6apm_audio_remove,
488 static int apm_probe(gpr_device_t *gdev)
490 struct device *dev = &gdev->dev;
494 apm = devm_kzalloc(dev, sizeof(*apm), GFP_KERNEL);
498 dev_set_drvdata(dev, apm);
500 mutex_init(&apm->lock);
503 init_waitqueue_head(&apm->wait);
505 idr_init(&apm->graph_idr);
506 idr_init(&apm->graph_info_idr);
507 idr_init(&apm->sub_graphs_idr);
508 idr_init(&apm->containers_idr);
510 idr_init(&apm->modules_idr);
512 q6apm_get_apm_state(apm);
514 ret = devm_snd_soc_register_component(dev, &q6apm_audio_component, NULL, 0);
516 dev_err(dev, "failed to get register q6apm: %d\n", ret);
520 return of_platform_populate(dev->of_node, NULL, NULL, dev);
523 struct audioreach_module *q6apm_find_module_by_mid(struct q6apm_graph *graph, uint32_t mid)
525 struct audioreach_graph_info *info = graph->info;
526 struct q6apm *apm = graph->apm;
528 return __q6apm_find_module_by_mid(apm, info, mid);
532 static int apm_callback(struct gpr_resp_pkt *data, void *priv, int op)
534 gpr_device_t *gdev = priv;
535 struct q6apm *apm = dev_get_drvdata(&gdev->dev);
536 struct device *dev = &gdev->dev;
537 struct gpr_ibasic_rsp_result_t *result;
538 struct gpr_hdr *hdr = &data->hdr;
540 result = data->payload;
542 switch (hdr->opcode) {
543 case APM_CMD_RSP_GET_SPF_STATE:
544 apm->result.opcode = hdr->opcode;
545 apm->result.status = 0;
546 /* First word of result it state */
547 apm->state = result->opcode;
550 case GPR_BASIC_RSP_RESULT:
551 switch (result->opcode) {
552 case APM_CMD_GRAPH_START:
553 case APM_CMD_GRAPH_OPEN:
554 case APM_CMD_GRAPH_PREPARE:
555 case APM_CMD_GRAPH_CLOSE:
556 case APM_CMD_GRAPH_FLUSH:
557 case APM_CMD_GRAPH_STOP:
558 case APM_CMD_SET_CFG:
559 apm->result.opcode = result->opcode;
560 apm->result.status = result->status;
562 dev_err(dev, "Error (%d) Processing 0x%08x cmd\n", result->status,
578 static const struct of_device_id apm_device_id[] = {
579 { .compatible = "qcom,q6apm" },
582 MODULE_DEVICE_TABLE(of, apm_device_id);
585 static gpr_driver_t apm_driver = {
587 .gpr_callback = apm_callback,
590 .of_match_table = of_match_ptr(apm_device_id),
594 module_gpr_driver(apm_driver);
595 MODULE_DESCRIPTION("Audio Process Manager");
596 MODULE_LICENSE("GPL");