xref: /openbmc/linux/sound/soc/qcom/qdsp6/q6apm.c (revision c765ceda78f0bd9df1217f9beaefea58ecf3865c)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020, Linaro Limited
3 
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>
9 #include <linux/of.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"
19 #include "q6apm.h"
20 
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[];
26 } __packed;
27 
28 #define APM_GRAPH_MGMT_PSIZE(p, n) ALIGN(struct_size(p, sub_graph_id_list, n), 8)
29 
30 struct q6apm *g_apm;
31 
32 int q6apm_send_cmd_sync(struct q6apm *apm, struct gpr_pkt *pkt, uint32_t rsp_opcode)
33 {
34 	gpr_device_t *gdev = apm->gdev;
35 
36 	return audioreach_send_cmd_sync(&gdev->dev, gdev, &apm->result, &apm->lock,
37 					NULL, &apm->wait, pkt, rsp_opcode);
38 }
39 
40 static struct audioreach_graph *q6apm_get_audioreach_graph(struct q6apm *apm, uint32_t graph_id)
41 {
42 	struct audioreach_graph_info *info;
43 	struct audioreach_graph *graph;
44 	int id;
45 
46 	mutex_lock(&apm->lock);
47 	graph = idr_find(&apm->graph_idr, graph_id);
48 	mutex_unlock(&apm->lock);
49 
50 	if (graph) {
51 		kref_get(&graph->refcount);
52 		return graph;
53 	}
54 
55 	info = idr_find(&apm->graph_info_idr, graph_id);
56 
57 	if (!info)
58 		return ERR_PTR(-ENODEV);
59 
60 	graph = kzalloc(sizeof(*graph), GFP_KERNEL);
61 	if (!graph)
62 		return ERR_PTR(-ENOMEM);
63 
64 	graph->apm = apm;
65 	graph->info = info;
66 	graph->id = graph_id;
67 
68 	graph->graph = audioreach_alloc_graph_pkt(apm, info);
69 	if (IS_ERR(graph->graph)) {
70 		void *err = graph->graph;
71 
72 		kfree(graph);
73 		return ERR_CAST(err);
74 	}
75 
76 	mutex_lock(&apm->lock);
77 	id = idr_alloc(&apm->graph_idr, graph, graph_id, graph_id + 1, GFP_KERNEL);
78 	if (id < 0) {
79 		dev_err(apm->dev, "Unable to allocate graph id (%d)\n", graph_id);
80 		kfree(graph->graph);
81 		kfree(graph);
82 		mutex_unlock(&apm->lock);
83 		return ERR_PTR(id);
84 	}
85 	mutex_unlock(&apm->lock);
86 
87 	kref_init(&graph->refcount);
88 
89 	q6apm_send_cmd_sync(apm, graph->graph, 0);
90 
91 	return graph;
92 }
93 
94 static int audioreach_graph_mgmt_cmd(struct audioreach_graph *graph, uint32_t opcode)
95 {
96 	struct audioreach_graph_info *info = graph->info;
97 	int num_sub_graphs = info->num_sub_graphs;
98 	struct apm_module_param_data *param_data;
99 	struct apm_graph_mgmt_cmd *mgmt_cmd;
100 	struct audioreach_sub_graph *sg;
101 	struct q6apm *apm = graph->apm;
102 	int i = 0, rc, payload_size;
103 	struct gpr_pkt *pkt;
104 
105 	payload_size = APM_GRAPH_MGMT_PSIZE(mgmt_cmd, num_sub_graphs);
106 
107 	pkt = audioreach_alloc_apm_cmd_pkt(payload_size, opcode, 0);
108 	if (IS_ERR(pkt))
109 		return PTR_ERR(pkt);
110 
111 	mgmt_cmd = (void *)pkt + GPR_HDR_SIZE + APM_CMD_HDR_SIZE;
112 
113 	mgmt_cmd->num_sub_graphs = num_sub_graphs;
114 
115 	param_data = &mgmt_cmd->param_data;
116 	param_data->module_instance_id = APM_MODULE_INSTANCE_ID;
117 	param_data->param_id = APM_PARAM_ID_SUB_GRAPH_LIST;
118 	param_data->param_size = payload_size - APM_MODULE_PARAM_DATA_SIZE;
119 
120 	list_for_each_entry(sg, &info->sg_list, node)
121 		mgmt_cmd->sub_graph_id_list[i++] = sg->sub_graph_id;
122 
123 	rc = q6apm_send_cmd_sync(apm, pkt, 0);
124 
125 	kfree(pkt);
126 
127 	return rc;
128 }
129 
130 static void q6apm_put_audioreach_graph(struct kref *ref)
131 {
132 	struct audioreach_graph *graph;
133 	struct q6apm *apm;
134 
135 	graph = container_of(ref, struct audioreach_graph, refcount);
136 	apm = graph->apm;
137 
138 	audioreach_graph_mgmt_cmd(graph, APM_CMD_GRAPH_CLOSE);
139 
140 	mutex_lock(&apm->lock);
141 	graph = idr_remove(&apm->graph_idr, graph->id);
142 	mutex_unlock(&apm->lock);
143 
144 	kfree(graph->graph);
145 	kfree(graph);
146 }
147 
148 bool q6apm_is_adsp_ready(void)
149 {
150 	if (g_apm && g_apm->state)
151 		return true;
152 
153 	return false;
154 }
155 EXPORT_SYMBOL_GPL(q6apm_is_adsp_ready);
156 
157 static int q6apm_get_apm_state(struct q6apm *apm)
158 {
159 	struct gpr_pkt *pkt;
160 
161 	pkt = audioreach_alloc_apm_cmd_pkt(0, APM_CMD_GET_SPF_STATE, 0);
162 	if (IS_ERR(pkt))
163 		return PTR_ERR(pkt);
164 
165 	q6apm_send_cmd_sync(apm, pkt, APM_CMD_RSP_GET_SPF_STATE);
166 
167 	kfree(pkt);
168 
169 	return apm->state;
170 }
171 
172 static struct audioreach_module *__q6apm_find_module_by_mid(struct q6apm *apm,
173 						    struct audioreach_graph_info *info,
174 						    uint32_t mid)
175 {
176 	struct audioreach_container *container;
177 	struct audioreach_sub_graph *sgs;
178 	struct audioreach_module *module;
179 
180 	list_for_each_entry(sgs, &info->sg_list, node) {
181 		list_for_each_entry(container, &sgs->container_list, node) {
182 			list_for_each_entry(module, &container->modules_list, node) {
183 				if (mid == module->module_id)
184 					return module;
185 			}
186 		}
187 	}
188 
189 	return NULL;
190 }
191 
192 int q6apm_graph_media_format_shmem(struct q6apm_graph *graph,
193 				   struct audioreach_module_config *cfg)
194 {
195 	struct audioreach_module *module;
196 
197 	if (cfg->direction == SNDRV_PCM_STREAM_CAPTURE)
198 		module = q6apm_find_module_by_mid(graph, MODULE_ID_RD_SHARED_MEM_EP);
199 	else
200 		module = q6apm_find_module_by_mid(graph, MODULE_ID_WR_SHARED_MEM_EP);
201 
202 	if (!module)
203 		return -ENODEV;
204 
205 	audioreach_set_media_format(graph, module, cfg);
206 
207 	return 0;
208 
209 }
210 EXPORT_SYMBOL_GPL(q6apm_graph_media_format_shmem);
211 
212 int q6apm_map_memory_regions(struct q6apm_graph *graph, unsigned int dir, phys_addr_t phys,
213 			     size_t period_sz, unsigned int periods)
214 {
215 	struct audioreach_graph_data *data;
216 	struct audio_buffer *buf;
217 	int cnt;
218 	int rc;
219 
220 	if (dir == SNDRV_PCM_STREAM_PLAYBACK)
221 		data = &graph->rx_data;
222 	else
223 		data = &graph->tx_data;
224 
225 	mutex_lock(&graph->lock);
226 
227 	if (data->buf) {
228 		mutex_unlock(&graph->lock);
229 		return 0;
230 	}
231 
232 	buf = kzalloc(((sizeof(struct audio_buffer)) * periods), GFP_KERNEL);
233 	if (!buf) {
234 		mutex_unlock(&graph->lock);
235 		return -ENOMEM;
236 	}
237 
238 	if (dir == SNDRV_PCM_STREAM_PLAYBACK)
239 		data = &graph->rx_data;
240 	else
241 		data = &graph->tx_data;
242 
243 	data->buf = buf;
244 
245 	buf[0].phys = phys;
246 	buf[0].size = period_sz;
247 
248 	for (cnt = 1; cnt < periods; cnt++) {
249 		if (period_sz > 0) {
250 			buf[cnt].phys = buf[0].phys + (cnt * period_sz);
251 			buf[cnt].size = period_sz;
252 		}
253 	}
254 	data->num_periods = periods;
255 
256 	mutex_unlock(&graph->lock);
257 
258 	rc = audioreach_map_memory_regions(graph, dir, period_sz, periods, 1);
259 	if (rc < 0) {
260 		dev_err(graph->dev, "Memory_map_regions failed\n");
261 		audioreach_graph_free_buf(graph);
262 	}
263 
264 	return rc;
265 }
266 EXPORT_SYMBOL_GPL(q6apm_map_memory_regions);
267 
268 int q6apm_unmap_memory_regions(struct q6apm_graph *graph, unsigned int dir)
269 {
270 	struct apm_cmd_shared_mem_unmap_regions *cmd;
271 	struct audioreach_graph_data *data;
272 	struct gpr_pkt *pkt;
273 	int rc;
274 
275 	if (dir == SNDRV_PCM_STREAM_PLAYBACK)
276 		data = &graph->rx_data;
277 	else
278 		data = &graph->tx_data;
279 
280 	if (!data->mem_map_handle)
281 		return 0;
282 
283 	pkt = audioreach_alloc_apm_pkt(sizeof(*cmd), APM_CMD_SHARED_MEM_UNMAP_REGIONS, dir,
284 				     graph->port->id);
285 	if (IS_ERR(pkt))
286 		return PTR_ERR(pkt);
287 
288 	cmd = (void *)pkt + GPR_HDR_SIZE;
289 	cmd->mem_map_handle = data->mem_map_handle;
290 
291 	rc = audioreach_graph_send_cmd_sync(graph, pkt, APM_CMD_SHARED_MEM_UNMAP_REGIONS);
292 	kfree(pkt);
293 
294 	audioreach_graph_free_buf(graph);
295 
296 	return rc;
297 }
298 EXPORT_SYMBOL_GPL(q6apm_unmap_memory_regions);
299 
300 int q6apm_graph_media_format_pcm(struct q6apm_graph *graph, struct audioreach_module_config *cfg)
301 {
302 	struct audioreach_graph_info *info = graph->info;
303 	struct audioreach_sub_graph *sgs;
304 	struct audioreach_container *container;
305 	struct audioreach_module *module;
306 
307 	list_for_each_entry(sgs, &info->sg_list, node) {
308 		list_for_each_entry(container, &sgs->container_list, node) {
309 			list_for_each_entry(module, &container->modules_list, node) {
310 				if ((module->module_id == MODULE_ID_WR_SHARED_MEM_EP) ||
311 					(module->module_id == MODULE_ID_RD_SHARED_MEM_EP))
312 					continue;
313 
314 				audioreach_set_media_format(graph, module, cfg);
315 			}
316 		}
317 	}
318 
319 	return 0;
320 
321 }
322 EXPORT_SYMBOL_GPL(q6apm_graph_media_format_pcm);
323 
324 static int q6apm_graph_get_tx_shmem_module_iid(struct q6apm_graph *graph)
325 {
326 	struct audioreach_module *module;
327 
328 	module = q6apm_find_module_by_mid(graph, MODULE_ID_RD_SHARED_MEM_EP);
329 	if (!module)
330 		return -ENODEV;
331 
332 	return module->instance_id;
333 
334 }
335 
336 int q6apm_graph_get_rx_shmem_module_iid(struct q6apm_graph *graph)
337 {
338 	struct audioreach_module *module;
339 
340 	module = q6apm_find_module_by_mid(graph, MODULE_ID_WR_SHARED_MEM_EP);
341 	if (!module)
342 		return -ENODEV;
343 
344 	return module->instance_id;
345 
346 }
347 EXPORT_SYMBOL_GPL(q6apm_graph_get_rx_shmem_module_iid);
348 
349 int q6apm_write_async(struct q6apm_graph *graph, uint32_t len, uint32_t msw_ts,
350 		      uint32_t lsw_ts, uint32_t wflags)
351 {
352 	struct apm_data_cmd_wr_sh_mem_ep_data_buffer_v2 *write_buffer;
353 	struct audio_buffer *ab;
354 	struct gpr_pkt *pkt;
355 	int rc, iid;
356 
357 	iid = q6apm_graph_get_rx_shmem_module_iid(graph);
358 	pkt = audioreach_alloc_pkt(sizeof(*write_buffer), DATA_CMD_WR_SH_MEM_EP_DATA_BUFFER_V2,
359 				   graph->rx_data.dsp_buf | (len << APM_WRITE_TOKEN_LEN_SHIFT),
360 				   graph->port->id, iid);
361 	if (IS_ERR(pkt))
362 		return PTR_ERR(pkt);
363 
364 	write_buffer = (void *)pkt + GPR_HDR_SIZE;
365 
366 	mutex_lock(&graph->lock);
367 	ab = &graph->rx_data.buf[graph->rx_data.dsp_buf];
368 
369 	write_buffer->buf_addr_lsw = lower_32_bits(ab->phys);
370 	write_buffer->buf_addr_msw = upper_32_bits(ab->phys);
371 	write_buffer->buf_size = len;
372 	write_buffer->timestamp_lsw = lsw_ts;
373 	write_buffer->timestamp_msw = msw_ts;
374 	write_buffer->mem_map_handle = graph->rx_data.mem_map_handle;
375 	write_buffer->flags = wflags;
376 
377 	graph->rx_data.dsp_buf++;
378 
379 	if (graph->rx_data.dsp_buf >= graph->rx_data.num_periods)
380 		graph->rx_data.dsp_buf = 0;
381 
382 	mutex_unlock(&graph->lock);
383 
384 	rc = gpr_send_port_pkt(graph->port, pkt);
385 
386 	kfree(pkt);
387 
388 	return rc;
389 }
390 EXPORT_SYMBOL_GPL(q6apm_write_async);
391 
392 int q6apm_read(struct q6apm_graph *graph)
393 {
394 	struct data_cmd_rd_sh_mem_ep_data_buffer_v2 *read_buffer;
395 	struct audioreach_graph_data *port;
396 	struct audio_buffer *ab;
397 	struct gpr_pkt *pkt;
398 	int rc, iid;
399 
400 	iid = q6apm_graph_get_tx_shmem_module_iid(graph);
401 	pkt = audioreach_alloc_pkt(sizeof(*read_buffer), DATA_CMD_RD_SH_MEM_EP_DATA_BUFFER_V2,
402 				   graph->tx_data.dsp_buf, graph->port->id, iid);
403 	if (IS_ERR(pkt))
404 		return PTR_ERR(pkt);
405 
406 	read_buffer = (void *)pkt + GPR_HDR_SIZE;
407 
408 	mutex_lock(&graph->lock);
409 	port = &graph->tx_data;
410 	ab = &port->buf[port->dsp_buf];
411 
412 	read_buffer->buf_addr_lsw = lower_32_bits(ab->phys);
413 	read_buffer->buf_addr_msw = upper_32_bits(ab->phys);
414 	read_buffer->mem_map_handle = port->mem_map_handle;
415 	read_buffer->buf_size = ab->size;
416 
417 	port->dsp_buf++;
418 
419 	if (port->dsp_buf >= port->num_periods)
420 		port->dsp_buf = 0;
421 
422 	mutex_unlock(&graph->lock);
423 
424 	rc = gpr_send_port_pkt(graph->port, pkt);
425 	kfree(pkt);
426 
427 	return rc;
428 }
429 EXPORT_SYMBOL_GPL(q6apm_read);
430 
431 static int graph_callback(struct gpr_resp_pkt *data, void *priv, int op)
432 {
433 	struct data_cmd_rsp_rd_sh_mem_ep_data_buffer_done_v2 *rd_done;
434 	struct data_cmd_rsp_wr_sh_mem_ep_data_buffer_done_v2 *done;
435 	struct apm_cmd_rsp_shared_mem_map_regions *rsp;
436 	struct gpr_ibasic_rsp_result_t *result;
437 	struct q6apm_graph *graph = priv;
438 	struct gpr_hdr *hdr = &data->hdr;
439 	struct device *dev = graph->dev;
440 	uint32_t client_event;
441 	phys_addr_t phys;
442 	int token;
443 
444 	result = data->payload;
445 
446 	switch (hdr->opcode) {
447 	case DATA_CMD_RSP_WR_SH_MEM_EP_DATA_BUFFER_DONE_V2:
448 		client_event = APM_CLIENT_EVENT_DATA_WRITE_DONE;
449 		mutex_lock(&graph->lock);
450 		token = hdr->token & APM_WRITE_TOKEN_MASK;
451 
452 		done = data->payload;
453 		phys = graph->rx_data.buf[token].phys;
454 		mutex_unlock(&graph->lock);
455 
456 		if (lower_32_bits(phys) == done->buf_addr_lsw &&
457 		    upper_32_bits(phys) == done->buf_addr_msw) {
458 			graph->result.opcode = hdr->opcode;
459 			graph->result.status = done->status;
460 			if (graph->cb)
461 				graph->cb(client_event, hdr->token, data->payload, graph->priv);
462 		} else {
463 			dev_err(dev, "WR BUFF Unexpected addr %08x-%08x\n", done->buf_addr_lsw,
464 				done->buf_addr_msw);
465 		}
466 
467 		break;
468 	case APM_CMD_RSP_SHARED_MEM_MAP_REGIONS:
469 		graph->result.opcode = hdr->opcode;
470 		graph->result.status = 0;
471 		rsp = data->payload;
472 
473 		if (hdr->token == SNDRV_PCM_STREAM_PLAYBACK)
474 			graph->rx_data.mem_map_handle = rsp->mem_map_handle;
475 		else
476 			graph->tx_data.mem_map_handle = rsp->mem_map_handle;
477 
478 		wake_up(&graph->cmd_wait);
479 		break;
480 	case DATA_CMD_RSP_RD_SH_MEM_EP_DATA_BUFFER_V2:
481 		client_event = APM_CLIENT_EVENT_DATA_READ_DONE;
482 		mutex_lock(&graph->lock);
483 		rd_done = data->payload;
484 		phys = graph->tx_data.buf[hdr->token].phys;
485 		mutex_unlock(&graph->lock);
486 
487 		if (upper_32_bits(phys) == rd_done->buf_addr_msw &&
488 		    lower_32_bits(phys) == rd_done->buf_addr_lsw) {
489 			graph->result.opcode = hdr->opcode;
490 			graph->result.status = rd_done->status;
491 			if (graph->cb)
492 				graph->cb(client_event, hdr->token, data->payload, graph->priv);
493 		} else {
494 			dev_err(dev, "RD BUFF Unexpected addr %08x-%08x\n", rd_done->buf_addr_lsw,
495 				rd_done->buf_addr_msw);
496 		}
497 		break;
498 	case DATA_CMD_WR_SH_MEM_EP_EOS_RENDERED:
499 		break;
500 	case GPR_BASIC_RSP_RESULT:
501 		switch (result->opcode) {
502 		case APM_CMD_SHARED_MEM_UNMAP_REGIONS:
503 			graph->result.opcode = result->opcode;
504 			graph->result.status = 0;
505 			if (hdr->token == SNDRV_PCM_STREAM_PLAYBACK)
506 				graph->rx_data.mem_map_handle = 0;
507 			else
508 				graph->tx_data.mem_map_handle = 0;
509 
510 			wake_up(&graph->cmd_wait);
511 			break;
512 		case APM_CMD_SHARED_MEM_MAP_REGIONS:
513 		case DATA_CMD_WR_SH_MEM_EP_MEDIA_FORMAT:
514 		case APM_CMD_SET_CFG:
515 			graph->result.opcode = result->opcode;
516 			graph->result.status = result->status;
517 			if (result->status)
518 				dev_err(dev, "Error (%d) Processing 0x%08x cmd\n",
519 					result->status, result->opcode);
520 			wake_up(&graph->cmd_wait);
521 			break;
522 		default:
523 			break;
524 		}
525 		break;
526 	default:
527 		break;
528 	}
529 	return 0;
530 }
531 
532 struct q6apm_graph *q6apm_graph_open(struct device *dev, q6apm_cb cb,
533 				     void *priv, int graph_id)
534 {
535 	struct q6apm *apm = dev_get_drvdata(dev->parent);
536 	struct audioreach_graph *ar_graph;
537 	struct q6apm_graph *graph;
538 	int ret;
539 
540 	ar_graph = q6apm_get_audioreach_graph(apm, graph_id);
541 	if (IS_ERR(ar_graph)) {
542 		dev_err(dev, "No graph found with id %d\n", graph_id);
543 		return ERR_CAST(ar_graph);
544 	}
545 
546 	graph = kzalloc(sizeof(*graph), GFP_KERNEL);
547 	if (!graph) {
548 		ret = -ENOMEM;
549 		goto put_ar_graph;
550 	}
551 
552 	graph->apm = apm;
553 	graph->priv = priv;
554 	graph->cb = cb;
555 	graph->info = ar_graph->info;
556 	graph->ar_graph = ar_graph;
557 	graph->id = ar_graph->id;
558 	graph->dev = dev;
559 
560 	mutex_init(&graph->lock);
561 	init_waitqueue_head(&graph->cmd_wait);
562 
563 	graph->port = gpr_alloc_port(apm->gdev, dev, graph_callback, graph);
564 	if (IS_ERR(graph->port)) {
565 		ret = PTR_ERR(graph->port);
566 		goto free_graph;
567 	}
568 
569 	return graph;
570 
571 free_graph:
572 	kfree(graph);
573 put_ar_graph:
574 	kref_put(&ar_graph->refcount, q6apm_put_audioreach_graph);
575 	return ERR_PTR(ret);
576 }
577 EXPORT_SYMBOL_GPL(q6apm_graph_open);
578 
579 int q6apm_graph_close(struct q6apm_graph *graph)
580 {
581 	struct audioreach_graph *ar_graph = graph->ar_graph;
582 
583 	gpr_free_port(graph->port);
584 	kref_put(&ar_graph->refcount, q6apm_put_audioreach_graph);
585 	kfree(graph);
586 
587 	return 0;
588 }
589 EXPORT_SYMBOL_GPL(q6apm_graph_close);
590 
591 int q6apm_graph_prepare(struct q6apm_graph *graph)
592 {
593 	return audioreach_graph_mgmt_cmd(graph->ar_graph, APM_CMD_GRAPH_PREPARE);
594 }
595 EXPORT_SYMBOL_GPL(q6apm_graph_prepare);
596 
597 int q6apm_graph_start(struct q6apm_graph *graph)
598 {
599 	struct audioreach_graph *ar_graph = graph->ar_graph;
600 	int ret = 0;
601 
602 	if (ar_graph->start_count == 0)
603 		ret = audioreach_graph_mgmt_cmd(ar_graph, APM_CMD_GRAPH_START);
604 
605 	ar_graph->start_count++;
606 
607 	return ret;
608 }
609 EXPORT_SYMBOL_GPL(q6apm_graph_start);
610 
611 int q6apm_graph_stop(struct q6apm_graph *graph)
612 {
613 	struct audioreach_graph *ar_graph = graph->ar_graph;
614 
615 	if (--ar_graph->start_count > 0)
616 		return 0;
617 
618 	return audioreach_graph_mgmt_cmd(ar_graph, APM_CMD_GRAPH_STOP);
619 }
620 EXPORT_SYMBOL_GPL(q6apm_graph_stop);
621 
622 int q6apm_graph_flush(struct q6apm_graph *graph)
623 {
624 	return audioreach_graph_mgmt_cmd(graph->ar_graph, APM_CMD_GRAPH_FLUSH);
625 }
626 EXPORT_SYMBOL_GPL(q6apm_graph_flush);
627 
628 static int q6apm_audio_probe(struct snd_soc_component *component)
629 {
630 	return audioreach_tplg_init(component);
631 }
632 
633 static void q6apm_audio_remove(struct snd_soc_component *component)
634 {
635 	/* remove topology */
636 	snd_soc_tplg_component_remove(component);
637 }
638 
639 #define APM_AUDIO_DRV_NAME "q6apm-audio"
640 
641 static const struct snd_soc_component_driver q6apm_audio_component = {
642 	.name		= APM_AUDIO_DRV_NAME,
643 	.probe		= q6apm_audio_probe,
644 	.remove		= q6apm_audio_remove,
645 };
646 
647 static int apm_probe(gpr_device_t *gdev)
648 {
649 	struct device *dev = &gdev->dev;
650 	struct q6apm *apm;
651 	int ret;
652 
653 	apm = devm_kzalloc(dev, sizeof(*apm), GFP_KERNEL);
654 	if (!apm)
655 		return -ENOMEM;
656 
657 	dev_set_drvdata(dev, apm);
658 
659 	mutex_init(&apm->lock);
660 	apm->dev = dev;
661 	apm->gdev = gdev;
662 	init_waitqueue_head(&apm->wait);
663 
664 	INIT_LIST_HEAD(&apm->widget_list);
665 	idr_init(&apm->graph_idr);
666 	idr_init(&apm->graph_info_idr);
667 	idr_init(&apm->sub_graphs_idr);
668 	idr_init(&apm->containers_idr);
669 
670 	idr_init(&apm->modules_idr);
671 
672 	g_apm = apm;
673 
674 	q6apm_get_apm_state(apm);
675 
676 	ret = devm_snd_soc_register_component(dev, &q6apm_audio_component, NULL, 0);
677 	if (ret < 0) {
678 		dev_err(dev, "failed to get register q6apm: %d\n", ret);
679 		return ret;
680 	}
681 
682 	return of_platform_populate(dev->of_node, NULL, NULL, dev);
683 }
684 
685 struct audioreach_module *q6apm_find_module_by_mid(struct q6apm_graph *graph, uint32_t mid)
686 {
687 	struct audioreach_graph_info *info = graph->info;
688 	struct q6apm *apm = graph->apm;
689 
690 	return __q6apm_find_module_by_mid(apm, info, mid);
691 
692 }
693 
694 static int apm_callback(struct gpr_resp_pkt *data, void *priv, int op)
695 {
696 	gpr_device_t *gdev = priv;
697 	struct q6apm *apm = dev_get_drvdata(&gdev->dev);
698 	struct device *dev = &gdev->dev;
699 	struct gpr_ibasic_rsp_result_t *result;
700 	struct gpr_hdr *hdr = &data->hdr;
701 
702 	result = data->payload;
703 
704 	switch (hdr->opcode) {
705 	case APM_CMD_RSP_GET_SPF_STATE:
706 		apm->result.opcode = hdr->opcode;
707 		apm->result.status = 0;
708 		/* First word of result it state */
709 		apm->state = result->opcode;
710 		wake_up(&apm->wait);
711 		break;
712 	case GPR_BASIC_RSP_RESULT:
713 		switch (result->opcode) {
714 		case APM_CMD_GRAPH_START:
715 		case APM_CMD_GRAPH_OPEN:
716 		case APM_CMD_GRAPH_PREPARE:
717 		case APM_CMD_GRAPH_CLOSE:
718 		case APM_CMD_GRAPH_FLUSH:
719 		case APM_CMD_GRAPH_STOP:
720 		case APM_CMD_SET_CFG:
721 			apm->result.opcode = result->opcode;
722 			apm->result.status = result->status;
723 			if (result->status)
724 				dev_err(dev, "Error (%d) Processing 0x%08x cmd\n", result->status,
725 					result->opcode);
726 			wake_up(&apm->wait);
727 			break;
728 		default:
729 			break;
730 		}
731 		break;
732 	default:
733 		break;
734 	}
735 
736 	return 0;
737 }
738 
739 #ifdef CONFIG_OF
740 static const struct of_device_id apm_device_id[]  = {
741 	{ .compatible = "qcom,q6apm" },
742 	{},
743 };
744 MODULE_DEVICE_TABLE(of, apm_device_id);
745 #endif
746 
747 static gpr_driver_t apm_driver = {
748 	.probe = apm_probe,
749 	.gpr_callback = apm_callback,
750 	.driver = {
751 		.name = "qcom-apm",
752 		.of_match_table = of_match_ptr(apm_device_id),
753 	},
754 };
755 
756 module_gpr_driver(apm_driver);
757 MODULE_DESCRIPTION("Audio Process Manager");
758 MODULE_LICENSE("GPL");
759