Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2020, Linaro Limited
0003 
0004 #include <dt-bindings/soc/qcom,gpr.h>
0005 #include <linux/delay.h>
0006 #include <linux/jiffies.h>
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/of.h>
0010 #include <linux/of_platform.h>
0011 #include <linux/sched.h>
0012 #include <linux/slab.h>
0013 #include <linux/soc/qcom/apr.h>
0014 #include <linux/wait.h>
0015 #include <sound/soc.h>
0016 #include <sound/soc-dapm.h>
0017 #include <sound/pcm.h>
0018 #include "audioreach.h"
0019 #include "q6apm.h"
0020 
0021 /* Graph Management */
0022 struct apm_graph_mgmt_cmd {
0023     struct apm_module_param_data param_data;
0024     uint32_t num_sub_graphs;
0025     uint32_t sub_graph_id_list[];
0026 } __packed;
0027 
0028 #define APM_GRAPH_MGMT_PSIZE(p, n) ALIGN(struct_size(p, sub_graph_id_list, n), 8)
0029 
0030 int q6apm_send_cmd_sync(struct q6apm *apm, struct gpr_pkt *pkt, uint32_t rsp_opcode)
0031 {
0032     gpr_device_t *gdev = apm->gdev;
0033 
0034     return audioreach_send_cmd_sync(&gdev->dev, gdev, &apm->result, &apm->lock,
0035                     NULL, &apm->wait, pkt, rsp_opcode);
0036 }
0037 
0038 static struct audioreach_graph *q6apm_get_audioreach_graph(struct q6apm *apm, uint32_t graph_id)
0039 {
0040     struct audioreach_graph_info *info;
0041     struct audioreach_graph *graph;
0042     int id;
0043 
0044     mutex_lock(&apm->lock);
0045     graph = idr_find(&apm->graph_idr, graph_id);
0046     mutex_unlock(&apm->lock);
0047 
0048     if (graph) {
0049         kref_get(&graph->refcount);
0050         return graph;
0051     }
0052 
0053     info = idr_find(&apm->graph_info_idr, graph_id);
0054 
0055     if (!info)
0056         return ERR_PTR(-ENODEV);
0057 
0058     graph = kzalloc(sizeof(*graph), GFP_KERNEL);
0059     if (!graph)
0060         return ERR_PTR(-ENOMEM);
0061 
0062     graph->apm = apm;
0063     graph->info = info;
0064     graph->id = graph_id;
0065 
0066     graph->graph = audioreach_alloc_graph_pkt(apm, &info->sg_list, graph_id);
0067     if (IS_ERR(graph->graph)) {
0068         void *err = graph->graph;
0069 
0070         kfree(graph);
0071         return ERR_CAST(err);
0072     }
0073 
0074     mutex_lock(&apm->lock);
0075     id = idr_alloc(&apm->graph_idr, graph, graph_id, graph_id + 1, GFP_KERNEL);
0076     if (id < 0) {
0077         dev_err(apm->dev, "Unable to allocate graph id (%d)\n", graph_id);
0078         kfree(graph->graph);
0079         kfree(graph);
0080         mutex_unlock(&apm->lock);
0081         return ERR_PTR(id);
0082     }
0083     mutex_unlock(&apm->lock);
0084 
0085     kref_init(&graph->refcount);
0086 
0087     q6apm_send_cmd_sync(apm, graph->graph, 0);
0088 
0089     return graph;
0090 }
0091 
0092 static int audioreach_graph_mgmt_cmd(struct audioreach_graph *graph, uint32_t opcode)
0093 {
0094     struct audioreach_graph_info *info = graph->info;
0095     int num_sub_graphs = info->num_sub_graphs;
0096     struct apm_module_param_data *param_data;
0097     struct apm_graph_mgmt_cmd *mgmt_cmd;
0098     struct audioreach_sub_graph *sg;
0099     struct q6apm *apm = graph->apm;
0100     int i = 0, rc, payload_size;
0101     struct gpr_pkt *pkt;
0102 
0103     payload_size = APM_GRAPH_MGMT_PSIZE(mgmt_cmd, num_sub_graphs);
0104 
0105     pkt = audioreach_alloc_apm_cmd_pkt(payload_size, opcode, 0);
0106     if (IS_ERR(pkt))
0107         return PTR_ERR(pkt);
0108 
0109     mgmt_cmd = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
0110 
0111     mgmt_cmd->num_sub_graphs = num_sub_graphs;
0112 
0113     param_data = &mgmt_cmd->param_data;
0114     param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
0115     param_data->param_id = APM_PARAM_ID_SUB_GRAPH_LIST;
0116     param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
0117 
0118     list_for_each_entry(sg, &info->sg_list, node)
0119         mgmt_cmd->sub_graph_id_list[i++] = sg->sub_graph_id;
0120 
0121     rc = q6apm_send_cmd_sync(apm, pkt, 0);
0122 
0123     kfree(pkt);
0124 
0125     return rc;
0126 }
0127 
0128 static void q6apm_put_audioreach_graph(struct kref *ref)
0129 {
0130     struct audioreach_graph *graph;
0131     struct q6apm *apm;
0132 
0133     graph = container_of(ref, struct audioreach_graph, refcount);
0134     apm = graph->apm;
0135 
0136     audioreach_graph_mgmt_cmd(graph, APM_CMD_GRAPH_CLOSE);
0137 
0138     mutex_lock(&apm->lock);
0139     graph = idr_remove(&apm->graph_idr, graph->id);
0140     mutex_unlock(&apm->lock);
0141 
0142     kfree(graph->graph);
0143     kfree(graph);
0144 }
0145 
0146 static int q6apm_get_apm_state(struct q6apm *apm)
0147 {
0148     struct gpr_pkt *pkt;
0149 
0150     pkt = audioreach_alloc_apm_cmd_pkt(0, APM_CMD_GET_SPF_STATE, 0);
0151     if (IS_ERR(pkt))
0152         return PTR_ERR(pkt);
0153 
0154     q6apm_send_cmd_sync(apm, pkt, APM_CMD_RSP_GET_SPF_STATE);
0155 
0156     kfree(pkt);
0157 
0158     return apm->state;
0159 }
0160 
0161 static struct audioreach_module *__q6apm_find_module_by_mid(struct q6apm *apm,
0162                             struct audioreach_graph_info *info,
0163                             uint32_t mid)
0164 {
0165     struct audioreach_container *container;
0166     struct audioreach_sub_graph *sgs;
0167     struct audioreach_module *module;
0168 
0169     list_for_each_entry(sgs, &info->sg_list, node) {
0170         list_for_each_entry(container, &sgs->container_list, node) {
0171             list_for_each_entry(module, &container->modules_list, node) {
0172                 if (mid == module->module_id)
0173                     return module;
0174             }
0175         }
0176     }
0177 
0178     return NULL;
0179 }
0180 
0181 static struct audioreach_module *q6apm_graph_get_last_module(struct q6apm *apm, u32 sgid)
0182 {
0183     struct audioreach_container *container;
0184     struct audioreach_module *module;
0185     struct audioreach_sub_graph *sg;
0186 
0187     mutex_lock(&apm->lock);
0188     sg = idr_find(&apm->sub_graphs_idr, sgid);
0189     mutex_unlock(&apm->lock);
0190     if (!sg)
0191         return NULL;
0192 
0193     container = list_last_entry(&sg->container_list, struct audioreach_container, node);
0194     module = audioreach_get_container_last_module(container);
0195 
0196     return module;
0197 }
0198 
0199 static struct audioreach_module *q6apm_graph_get_first_module(struct q6apm *apm, u32 sgid)
0200 {
0201     struct audioreach_container *container;
0202     struct audioreach_module *module;
0203     struct audioreach_sub_graph *sg;
0204 
0205     mutex_lock(&apm->lock);
0206     sg = idr_find(&apm->sub_graphs_idr, sgid);
0207     mutex_unlock(&apm->lock);
0208     if (!sg)
0209         return NULL;
0210 
0211     container = list_first_entry(&sg->container_list, struct audioreach_container, node);
0212     module = audioreach_get_container_first_module(container);
0213 
0214     return module;
0215 }
0216 
0217 bool q6apm_is_sub_graphs_connected(struct q6apm *apm, u32 src_sgid, u32 dst_sgid)
0218 {
0219     struct audioreach_module *module;
0220     u32 iid;
0221 
0222     module = q6apm_graph_get_last_module(apm, src_sgid);
0223     if (!module)
0224         return false;
0225 
0226     iid = module->instance_id;
0227     module = q6apm_graph_get_first_module(apm, dst_sgid);
0228     if (!module)
0229         return false;
0230 
0231     if (module->src_mod_inst_id == iid)
0232         return true;
0233 
0234     return false;
0235 }
0236 
0237 int q6apm_connect_sub_graphs(struct q6apm *apm, u32 src_sgid, u32 dst_sgid, bool connect)
0238 {
0239     struct audioreach_module *module;
0240     u32 iid;
0241 
0242     if (connect) {
0243         module = q6apm_graph_get_last_module(apm, src_sgid);
0244         if (!module)
0245             return -ENODEV;
0246 
0247         iid = module->instance_id;
0248     } else {
0249         iid = 0;
0250     }
0251 
0252     module = q6apm_graph_get_first_module(apm, dst_sgid);
0253     if (!module)
0254         return -ENODEV;
0255 
0256     /* set src module in dst subgraph first module */
0257     module->src_mod_inst_id = iid;
0258 
0259     return 0;
0260 }
0261 
0262 int q6apm_graph_media_format_shmem(struct q6apm_graph *graph,
0263                    struct audioreach_module_config *cfg)
0264 {
0265     struct audioreach_module *module;
0266 
0267     if (cfg->direction == SNDRV_PCM_STREAM_CAPTURE)
0268         module = q6apm_find_module_by_mid(graph, MODULE_ID_RD_SHARED_MEM_EP);
0269     else
0270         module = q6apm_find_module_by_mid(graph, MODULE_ID_WR_SHARED_MEM_EP);
0271 
0272     if (!module)
0273         return -ENODEV;
0274 
0275     audioreach_set_media_format(graph, module, cfg);
0276 
0277     return 0;
0278 
0279 }
0280 EXPORT_SYMBOL_GPL(q6apm_graph_media_format_shmem);
0281 
0282 int q6apm_map_memory_regions(struct q6apm_graph *graph, unsigned int dir, phys_addr_t phys,
0283                  size_t period_sz, unsigned int periods)
0284 {
0285     struct audioreach_graph_data *data;
0286     struct audio_buffer *buf;
0287     int cnt;
0288     int rc;
0289 
0290     if (dir == SNDRV_PCM_STREAM_PLAYBACK)
0291         data = &graph->rx_data;
0292     else
0293         data = &graph->tx_data;
0294 
0295     mutex_lock(&graph->lock);
0296 
0297     if (data->buf) {
0298         mutex_unlock(&graph->lock);
0299         return 0;
0300     }
0301 
0302     buf = kzalloc(((sizeof(struct audio_buffer)) * periods), GFP_KERNEL);
0303     if (!buf) {
0304         mutex_unlock(&graph->lock);
0305         return -ENOMEM;
0306     }
0307 
0308     if (dir == SNDRV_PCM_STREAM_PLAYBACK)
0309         data = &graph->rx_data;
0310     else
0311         data = &graph->tx_data;
0312 
0313     data->buf = buf;
0314 
0315     buf[0].phys = phys;
0316     buf[0].size = period_sz;
0317 
0318     for (cnt = 1; cnt < periods; cnt++) {
0319         if (period_sz > 0) {
0320             buf[cnt].phys = buf[0].phys + (cnt * period_sz);
0321             buf[cnt].size = period_sz;
0322         }
0323     }
0324     data->num_periods = periods;
0325 
0326     mutex_unlock(&graph->lock);
0327 
0328     rc = audioreach_map_memory_regions(graph, dir, period_sz, periods, 1);
0329     if (rc < 0) {
0330         dev_err(graph->dev, "Memory_map_regions failed\n");
0331         audioreach_graph_free_buf(graph);
0332     }
0333 
0334     return rc;
0335 }
0336 EXPORT_SYMBOL_GPL(q6apm_map_memory_regions);
0337 
0338 int q6apm_unmap_memory_regions(struct q6apm_graph *graph, unsigned int dir)
0339 {
0340     struct apm_cmd_shared_mem_unmap_regions *cmd;
0341     struct audioreach_graph_data *data;
0342     struct gpr_pkt *pkt;
0343     int rc;
0344 
0345     if (dir == SNDRV_PCM_STREAM_PLAYBACK)
0346         data = &graph->rx_data;
0347     else
0348         data = &graph->tx_data;
0349 
0350     if (!data->mem_map_handle)
0351         return 0;
0352 
0353     pkt = audioreach_alloc_apm_pkt(sizeof(*cmd), APM_CMD_SHARED_MEM_UNMAP_REGIONS, dir,
0354                      graph->port->id);
0355     if (IS_ERR(pkt))
0356         return PTR_ERR(pkt);
0357 
0358     cmd = (void *)pkt + GPR_HDR_SIZE;
0359     cmd->mem_map_handle = data->mem_map_handle;
0360 
0361     rc = audioreach_graph_send_cmd_sync(graph, pkt, APM_CMD_SHARED_MEM_UNMAP_REGIONS);
0362     kfree(pkt);
0363 
0364     audioreach_graph_free_buf(graph);
0365 
0366     return rc;
0367 }
0368 EXPORT_SYMBOL_GPL(q6apm_unmap_memory_regions);
0369 
0370 int q6apm_graph_media_format_pcm(struct q6apm_graph *graph, struct audioreach_module_config *cfg)
0371 {
0372     struct audioreach_graph_info *info = graph->info;
0373     struct audioreach_sub_graph *sgs;
0374     struct audioreach_container *container;
0375     struct audioreach_module *module;
0376 
0377     list_for_each_entry(sgs, &info->sg_list, node) {
0378         list_for_each_entry(container, &sgs->container_list, node) {
0379             list_for_each_entry(module, &container->modules_list, node) {
0380                 if ((module->module_id == MODULE_ID_WR_SHARED_MEM_EP) ||
0381                     (module->module_id == MODULE_ID_RD_SHARED_MEM_EP))
0382                     continue;
0383 
0384                 audioreach_set_media_format(graph, module, cfg);
0385             }
0386         }
0387     }
0388 
0389     return 0;
0390 
0391 }
0392 EXPORT_SYMBOL_GPL(q6apm_graph_media_format_pcm);
0393 
0394 static int q6apm_graph_get_tx_shmem_module_iid(struct q6apm_graph *graph)
0395 {
0396     struct audioreach_module *module;
0397 
0398     module = q6apm_find_module_by_mid(graph, MODULE_ID_RD_SHARED_MEM_EP);
0399     if (!module)
0400         return -ENODEV;
0401 
0402     return module->instance_id;
0403 
0404 }
0405 
0406 int q6apm_graph_get_rx_shmem_module_iid(struct q6apm_graph *graph)
0407 {
0408     struct audioreach_module *module;
0409 
0410     module = q6apm_find_module_by_mid(graph, MODULE_ID_WR_SHARED_MEM_EP);
0411     if (!module)
0412         return -ENODEV;
0413 
0414     return module->instance_id;
0415 
0416 }
0417 EXPORT_SYMBOL_GPL(q6apm_graph_get_rx_shmem_module_iid);
0418 
0419 int q6apm_write_async(struct q6apm_graph *graph, uint32_t len, uint32_t msw_ts,
0420               uint32_t lsw_ts, uint32_t wflags)
0421 {
0422     struct apm_data_cmd_wr_sh_mem_ep_data_buffer_v2 *write_buffer;
0423     struct audio_buffer *ab;
0424     struct gpr_pkt *pkt;
0425     int rc, iid;
0426 
0427     iid = q6apm_graph_get_rx_shmem_module_iid(graph);
0428     pkt = audioreach_alloc_pkt(sizeof(*write_buffer), DATA_CMD_WR_SH_MEM_EP_DATA_BUFFER_V2,
0429                    graph->rx_data.dsp_buf | (len << APM_WRITE_TOKEN_LEN_SHIFT),
0430                    graph->port->id, iid);
0431     if (IS_ERR(pkt))
0432         return PTR_ERR(pkt);
0433 
0434     write_buffer = (void *)pkt + GPR_HDR_SIZE;
0435 
0436     mutex_lock(&graph->lock);
0437     ab = &graph->rx_data.buf[graph->rx_data.dsp_buf];
0438 
0439     write_buffer->buf_addr_lsw = lower_32_bits(ab->phys);
0440     write_buffer->buf_addr_msw = upper_32_bits(ab->phys);
0441     write_buffer->buf_size = len;
0442     write_buffer->timestamp_lsw = lsw_ts;
0443     write_buffer->timestamp_msw = msw_ts;
0444     write_buffer->mem_map_handle = graph->rx_data.mem_map_handle;
0445     write_buffer->flags = wflags;
0446 
0447     graph->rx_data.dsp_buf++;
0448 
0449     if (graph->rx_data.dsp_buf >= graph->rx_data.num_periods)
0450         graph->rx_data.dsp_buf = 0;
0451 
0452     mutex_unlock(&graph->lock);
0453 
0454     rc = gpr_send_port_pkt(graph->port, pkt);
0455 
0456     kfree(pkt);
0457 
0458     return rc;
0459 }
0460 EXPORT_SYMBOL_GPL(q6apm_write_async);
0461 
0462 int q6apm_read(struct q6apm_graph *graph)
0463 {
0464     struct data_cmd_rd_sh_mem_ep_data_buffer_v2 *read_buffer;
0465     struct audioreach_graph_data *port;
0466     struct audio_buffer *ab;
0467     struct gpr_pkt *pkt;
0468     int rc, iid;
0469 
0470     iid = q6apm_graph_get_tx_shmem_module_iid(graph);
0471     pkt = audioreach_alloc_pkt(sizeof(*read_buffer), DATA_CMD_RD_SH_MEM_EP_DATA_BUFFER_V2,
0472                    graph->tx_data.dsp_buf, graph->port->id, iid);
0473     if (IS_ERR(pkt))
0474         return PTR_ERR(pkt);
0475 
0476     read_buffer = (void *)pkt + GPR_HDR_SIZE;
0477 
0478     mutex_lock(&graph->lock);
0479     port = &graph->tx_data;
0480     ab = &port->buf[port->dsp_buf];
0481 
0482     read_buffer->buf_addr_lsw = lower_32_bits(ab->phys);
0483     read_buffer->buf_addr_msw = upper_32_bits(ab->phys);
0484     read_buffer->mem_map_handle = port->mem_map_handle;
0485     read_buffer->buf_size = ab->size;
0486 
0487     port->dsp_buf++;
0488 
0489     if (port->dsp_buf >= port->num_periods)
0490         port->dsp_buf = 0;
0491 
0492     mutex_unlock(&graph->lock);
0493 
0494     rc = gpr_send_port_pkt(graph->port, pkt);
0495     kfree(pkt);
0496 
0497     return rc;
0498 }
0499 EXPORT_SYMBOL_GPL(q6apm_read);
0500 
0501 static int graph_callback(struct gpr_resp_pkt *data, void *priv, int op)
0502 {
0503     struct data_cmd_rsp_rd_sh_mem_ep_data_buffer_done_v2 *rd_done;
0504     struct data_cmd_rsp_wr_sh_mem_ep_data_buffer_done_v2 *done;
0505     struct apm_cmd_rsp_shared_mem_map_regions *rsp;
0506     struct gpr_ibasic_rsp_result_t *result;
0507     struct q6apm_graph *graph = priv;
0508     struct gpr_hdr *hdr = &data->hdr;
0509     struct device *dev = graph->dev;
0510     uint32_t client_event;
0511     phys_addr_t phys;
0512     int token;
0513 
0514     result = data->payload;
0515 
0516     switch (hdr->opcode) {
0517     case DATA_CMD_RSP_WR_SH_MEM_EP_DATA_BUFFER_DONE_V2:
0518         client_event = APM_CLIENT_EVENT_DATA_WRITE_DONE;
0519         mutex_lock(&graph->lock);
0520         token = hdr->token & APM_WRITE_TOKEN_MASK;
0521 
0522         done = data->payload;
0523         phys = graph->rx_data.buf[token].phys;
0524         mutex_unlock(&graph->lock);
0525 
0526         if (lower_32_bits(phys) == done->buf_addr_lsw &&
0527             upper_32_bits(phys) == done->buf_addr_msw) {
0528             graph->result.opcode = hdr->opcode;
0529             graph->result.status = done->status;
0530             if (graph->cb)
0531                 graph->cb(client_event, hdr->token, data->payload, graph->priv);
0532         } else {
0533             dev_err(dev, "WR BUFF Unexpected addr %08x-%08x\n", done->buf_addr_lsw,
0534                 done->buf_addr_msw);
0535         }
0536 
0537         break;
0538     case APM_CMD_RSP_SHARED_MEM_MAP_REGIONS:
0539         graph->result.opcode = hdr->opcode;
0540         graph->result.status = 0;
0541         rsp = data->payload;
0542 
0543         if (hdr->token == SNDRV_PCM_STREAM_PLAYBACK)
0544             graph->rx_data.mem_map_handle = rsp->mem_map_handle;
0545         else
0546             graph->tx_data.mem_map_handle = rsp->mem_map_handle;
0547 
0548         wake_up(&graph->cmd_wait);
0549         break;
0550     case DATA_CMD_RSP_RD_SH_MEM_EP_DATA_BUFFER_V2:
0551         client_event = APM_CLIENT_EVENT_DATA_READ_DONE;
0552         mutex_lock(&graph->lock);
0553         rd_done = data->payload;
0554         phys = graph->tx_data.buf[hdr->token].phys;
0555         mutex_unlock(&graph->lock);
0556 
0557         if (upper_32_bits(phys) == rd_done->buf_addr_msw &&
0558             lower_32_bits(phys) == rd_done->buf_addr_lsw) {
0559             graph->result.opcode = hdr->opcode;
0560             graph->result.status = rd_done->status;
0561             if (graph->cb)
0562                 graph->cb(client_event, hdr->token, data->payload, graph->priv);
0563         } else {
0564             dev_err(dev, "RD BUFF Unexpected addr %08x-%08x\n", rd_done->buf_addr_lsw,
0565                 rd_done->buf_addr_msw);
0566         }
0567         break;
0568     case DATA_CMD_WR_SH_MEM_EP_EOS_RENDERED:
0569         break;
0570     case GPR_BASIC_RSP_RESULT:
0571         switch (result->opcode) {
0572         case APM_CMD_SHARED_MEM_UNMAP_REGIONS:
0573             graph->result.opcode = result->opcode;
0574             graph->result.status = 0;
0575             if (hdr->token == SNDRV_PCM_STREAM_PLAYBACK)
0576                 graph->rx_data.mem_map_handle = 0;
0577             else
0578                 graph->tx_data.mem_map_handle = 0;
0579 
0580             wake_up(&graph->cmd_wait);
0581             break;
0582         case APM_CMD_SHARED_MEM_MAP_REGIONS:
0583         case DATA_CMD_WR_SH_MEM_EP_MEDIA_FORMAT:
0584         case APM_CMD_SET_CFG:
0585             graph->result.opcode = result->opcode;
0586             graph->result.status = result->status;
0587             if (result->status)
0588                 dev_err(dev, "Error (%d) Processing 0x%08x cmd\n",
0589                     result->status, result->opcode);
0590             wake_up(&graph->cmd_wait);
0591             break;
0592         default:
0593             break;
0594         }
0595         break;
0596     default:
0597         break;
0598     }
0599     return 0;
0600 }
0601 
0602 struct q6apm_graph *q6apm_graph_open(struct device *dev, q6apm_cb cb,
0603                      void *priv, int graph_id)
0604 {
0605     struct q6apm *apm = dev_get_drvdata(dev->parent);
0606     struct audioreach_graph *ar_graph;
0607     struct q6apm_graph *graph;
0608     int ret;
0609 
0610     ar_graph = q6apm_get_audioreach_graph(apm, graph_id);
0611     if (IS_ERR(ar_graph)) {
0612         dev_err(dev, "No graph found with id %d\n", graph_id);
0613         return ERR_CAST(ar_graph);
0614     }
0615 
0616     graph = kzalloc(sizeof(*graph), GFP_KERNEL);
0617     if (!graph) {
0618         ret = -ENOMEM;
0619         goto put_ar_graph;
0620     }
0621 
0622     graph->apm = apm;
0623     graph->priv = priv;
0624     graph->cb = cb;
0625     graph->info = ar_graph->info;
0626     graph->ar_graph = ar_graph;
0627     graph->id = ar_graph->id;
0628     graph->dev = dev;
0629 
0630     mutex_init(&graph->lock);
0631     init_waitqueue_head(&graph->cmd_wait);
0632 
0633     graph->port = gpr_alloc_port(apm->gdev, dev, graph_callback, graph);
0634     if (IS_ERR(graph->port)) {
0635         ret = PTR_ERR(graph->port);
0636         goto free_graph;
0637     }
0638 
0639     return graph;
0640 
0641 free_graph:
0642     kfree(graph);
0643 put_ar_graph:
0644     kref_put(&ar_graph->refcount, q6apm_put_audioreach_graph);
0645     return ERR_PTR(ret);
0646 }
0647 EXPORT_SYMBOL_GPL(q6apm_graph_open);
0648 
0649 int q6apm_graph_close(struct q6apm_graph *graph)
0650 {
0651     struct audioreach_graph *ar_graph = graph->ar_graph;
0652 
0653     gpr_free_port(graph->port);
0654     kref_put(&ar_graph->refcount, q6apm_put_audioreach_graph);
0655     kfree(graph);
0656 
0657     return 0;
0658 }
0659 EXPORT_SYMBOL_GPL(q6apm_graph_close);
0660 
0661 int q6apm_graph_prepare(struct q6apm_graph *graph)
0662 {
0663     return audioreach_graph_mgmt_cmd(graph->ar_graph, APM_CMD_GRAPH_PREPARE);
0664 }
0665 EXPORT_SYMBOL_GPL(q6apm_graph_prepare);
0666 
0667 int q6apm_graph_start(struct q6apm_graph *graph)
0668 {
0669     struct audioreach_graph *ar_graph = graph->ar_graph;
0670     int ret = 0;
0671 
0672     if (ar_graph->start_count == 0)
0673         ret = audioreach_graph_mgmt_cmd(ar_graph, APM_CMD_GRAPH_START);
0674 
0675     ar_graph->start_count++;
0676 
0677     return ret;
0678 }
0679 EXPORT_SYMBOL_GPL(q6apm_graph_start);
0680 
0681 int q6apm_graph_stop(struct q6apm_graph *graph)
0682 {
0683     struct audioreach_graph *ar_graph = graph->ar_graph;
0684 
0685     if (--ar_graph->start_count > 0)
0686         return 0;
0687 
0688     return audioreach_graph_mgmt_cmd(ar_graph, APM_CMD_GRAPH_STOP);
0689 }
0690 EXPORT_SYMBOL_GPL(q6apm_graph_stop);
0691 
0692 int q6apm_graph_flush(struct q6apm_graph *graph)
0693 {
0694     return audioreach_graph_mgmt_cmd(graph->ar_graph, APM_CMD_GRAPH_FLUSH);
0695 }
0696 EXPORT_SYMBOL_GPL(q6apm_graph_flush);
0697 
0698 static int q6apm_audio_probe(struct snd_soc_component *component)
0699 {
0700     return audioreach_tplg_init(component);
0701 }
0702 
0703 static void q6apm_audio_remove(struct snd_soc_component *component)
0704 {
0705     /* remove topology */
0706     snd_soc_tplg_component_remove(component);
0707 }
0708 
0709 #define APM_AUDIO_DRV_NAME "q6apm-audio"
0710 
0711 static const struct snd_soc_component_driver q6apm_audio_component = {
0712     .name       = APM_AUDIO_DRV_NAME,
0713     .probe      = q6apm_audio_probe,
0714     .remove     = q6apm_audio_remove,
0715 };
0716 
0717 static int apm_probe(gpr_device_t *gdev)
0718 {
0719     struct device *dev = &gdev->dev;
0720     struct q6apm *apm;
0721     int ret;
0722 
0723     apm = devm_kzalloc(dev, sizeof(*apm), GFP_KERNEL);
0724     if (!apm)
0725         return -ENOMEM;
0726 
0727     dev_set_drvdata(dev, apm);
0728 
0729     mutex_init(&apm->lock);
0730     apm->dev = dev;
0731     apm->gdev = gdev;
0732     init_waitqueue_head(&apm->wait);
0733 
0734     idr_init(&apm->graph_idr);
0735     idr_init(&apm->graph_info_idr);
0736     idr_init(&apm->sub_graphs_idr);
0737     idr_init(&apm->containers_idr);
0738 
0739     idr_init(&apm->modules_idr);
0740 
0741     q6apm_get_apm_state(apm);
0742 
0743     ret = devm_snd_soc_register_component(dev, &q6apm_audio_component, NULL, 0);
0744     if (ret < 0) {
0745         dev_err(dev, "failed to get register q6apm: %d\n", ret);
0746         return ret;
0747     }
0748 
0749     return of_platform_populate(dev->of_node, NULL, NULL, dev);
0750 }
0751 
0752 struct audioreach_module *q6apm_find_module_by_mid(struct q6apm_graph *graph, uint32_t mid)
0753 {
0754     struct audioreach_graph_info *info = graph->info;
0755     struct q6apm *apm = graph->apm;
0756 
0757     return __q6apm_find_module_by_mid(apm, info, mid);
0758 
0759 }
0760 
0761 static int apm_callback(struct gpr_resp_pkt *data, void *priv, int op)
0762 {
0763     gpr_device_t *gdev = priv;
0764     struct q6apm *apm = dev_get_drvdata(&gdev->dev);
0765     struct device *dev = &gdev->dev;
0766     struct gpr_ibasic_rsp_result_t *result;
0767     struct gpr_hdr *hdr = &data->hdr;
0768 
0769     result = data->payload;
0770 
0771     switch (hdr->opcode) {
0772     case APM_CMD_RSP_GET_SPF_STATE:
0773         apm->result.opcode = hdr->opcode;
0774         apm->result.status = 0;
0775         /* First word of result it state */
0776         apm->state = result->opcode;
0777         wake_up(&apm->wait);
0778         break;
0779     case GPR_BASIC_RSP_RESULT:
0780         switch (result->opcode) {
0781         case APM_CMD_GRAPH_START:
0782         case APM_CMD_GRAPH_OPEN:
0783         case APM_CMD_GRAPH_PREPARE:
0784         case APM_CMD_GRAPH_CLOSE:
0785         case APM_CMD_GRAPH_FLUSH:
0786         case APM_CMD_GRAPH_STOP:
0787         case APM_CMD_SET_CFG:
0788             apm->result.opcode = result->opcode;
0789             apm->result.status = result->status;
0790             if (result->status)
0791                 dev_err(dev, "Error (%d) Processing 0x%08x cmd\n", result->status,
0792                     result->opcode);
0793             wake_up(&apm->wait);
0794             break;
0795         default:
0796             break;
0797         }
0798         break;
0799     default:
0800         break;
0801     }
0802 
0803     return 0;
0804 }
0805 
0806 #ifdef CONFIG_OF
0807 static const struct of_device_id apm_device_id[]  = {
0808     { .compatible = "qcom,q6apm" },
0809     {},
0810 };
0811 MODULE_DEVICE_TABLE(of, apm_device_id);
0812 #endif
0813 
0814 static gpr_driver_t apm_driver = {
0815     .probe = apm_probe,
0816     .gpr_callback = apm_callback,
0817     .driver = {
0818         .name = "qcom-apm",
0819         .of_match_table = of_match_ptr(apm_device_id),
0820     },
0821 };
0822 
0823 module_gpr_driver(apm_driver);
0824 MODULE_DESCRIPTION("Audio Process Manager");
0825 MODULE_LICENSE("GPL");