Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/soc/qcom/qdsp6/q6apm.c
29268 views
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
static 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
149
static int q6apm_get_apm_state(struct q6apm *apm)
150
{
151
struct gpr_pkt *pkt;
152
153
pkt = audioreach_alloc_apm_cmd_pkt(0, APM_CMD_GET_SPF_STATE, 0);
154
if (IS_ERR(pkt))
155
return PTR_ERR(pkt);
156
157
q6apm_send_cmd_sync(apm, pkt, APM_CMD_RSP_GET_SPF_STATE);
158
159
kfree(pkt);
160
161
return apm->state;
162
}
163
164
bool q6apm_is_adsp_ready(void)
165
{
166
if (g_apm)
167
return q6apm_get_apm_state(g_apm);
168
169
return false;
170
}
171
EXPORT_SYMBOL_GPL(q6apm_is_adsp_ready);
172
173
static struct audioreach_module *__q6apm_find_module_by_mid(struct q6apm *apm,
174
struct audioreach_graph_info *info,
175
uint32_t mid)
176
{
177
struct audioreach_container *container;
178
struct audioreach_sub_graph *sgs;
179
struct audioreach_module *module;
180
181
list_for_each_entry(sgs, &info->sg_list, node) {
182
list_for_each_entry(container, &sgs->container_list, node) {
183
list_for_each_entry(module, &container->modules_list, node) {
184
if (mid == module->module_id)
185
return module;
186
}
187
}
188
}
189
190
return NULL;
191
}
192
193
int q6apm_graph_media_format_shmem(struct q6apm_graph *graph,
194
struct audioreach_module_config *cfg)
195
{
196
struct audioreach_module *module;
197
198
if (cfg->direction == SNDRV_PCM_STREAM_CAPTURE)
199
module = q6apm_find_module_by_mid(graph, MODULE_ID_RD_SHARED_MEM_EP);
200
else
201
module = q6apm_find_module_by_mid(graph, MODULE_ID_WR_SHARED_MEM_EP);
202
203
if (!module)
204
return -ENODEV;
205
206
audioreach_set_media_format(graph, module, cfg);
207
208
return 0;
209
210
}
211
EXPORT_SYMBOL_GPL(q6apm_graph_media_format_shmem);
212
213
int q6apm_map_memory_regions(struct q6apm_graph *graph, unsigned int dir, phys_addr_t phys,
214
size_t period_sz, unsigned int periods)
215
{
216
struct audioreach_graph_data *data;
217
struct audio_buffer *buf;
218
int cnt;
219
int rc;
220
221
if (dir == SNDRV_PCM_STREAM_PLAYBACK)
222
data = &graph->rx_data;
223
else
224
data = &graph->tx_data;
225
226
mutex_lock(&graph->lock);
227
228
if (data->buf) {
229
mutex_unlock(&graph->lock);
230
return 0;
231
}
232
233
buf = kcalloc(periods, sizeof(struct audio_buffer), GFP_KERNEL);
234
if (!buf) {
235
mutex_unlock(&graph->lock);
236
return -ENOMEM;
237
}
238
239
if (dir == SNDRV_PCM_STREAM_PLAYBACK)
240
data = &graph->rx_data;
241
else
242
data = &graph->tx_data;
243
244
data->buf = buf;
245
246
buf[0].phys = phys;
247
buf[0].size = period_sz;
248
249
for (cnt = 1; cnt < periods; cnt++) {
250
if (period_sz > 0) {
251
buf[cnt].phys = buf[0].phys + (cnt * period_sz);
252
buf[cnt].size = period_sz;
253
}
254
}
255
data->num_periods = periods;
256
257
mutex_unlock(&graph->lock);
258
259
rc = audioreach_map_memory_regions(graph, dir, period_sz, periods, 1);
260
if (rc < 0) {
261
dev_err(graph->dev, "Memory_map_regions failed\n");
262
audioreach_graph_free_buf(graph);
263
}
264
265
return rc;
266
}
267
EXPORT_SYMBOL_GPL(q6apm_map_memory_regions);
268
269
int q6apm_unmap_memory_regions(struct q6apm_graph *graph, unsigned int dir)
270
{
271
struct apm_cmd_shared_mem_unmap_regions *cmd;
272
struct audioreach_graph_data *data;
273
struct gpr_pkt *pkt;
274
int rc;
275
276
if (dir == SNDRV_PCM_STREAM_PLAYBACK)
277
data = &graph->rx_data;
278
else
279
data = &graph->tx_data;
280
281
if (!data->mem_map_handle)
282
return 0;
283
284
pkt = audioreach_alloc_apm_pkt(sizeof(*cmd), APM_CMD_SHARED_MEM_UNMAP_REGIONS, dir,
285
graph->port->id);
286
if (IS_ERR(pkt))
287
return PTR_ERR(pkt);
288
289
cmd = (void *)pkt + GPR_HDR_SIZE;
290
cmd->mem_map_handle = data->mem_map_handle;
291
292
rc = audioreach_graph_send_cmd_sync(graph, pkt, APM_CMD_SHARED_MEM_UNMAP_REGIONS);
293
kfree(pkt);
294
295
audioreach_graph_free_buf(graph);
296
297
return rc;
298
}
299
EXPORT_SYMBOL_GPL(q6apm_unmap_memory_regions);
300
301
int q6apm_remove_initial_silence(struct device *dev, struct q6apm_graph *graph, uint32_t samples)
302
{
303
struct audioreach_module *module;
304
305
module = q6apm_find_module_by_mid(graph, MODULE_ID_PLACEHOLDER_DECODER);
306
if (!module)
307
return -ENODEV;
308
309
return audioreach_send_u32_param(graph, module, PARAM_ID_REMOVE_INITIAL_SILENCE, samples);
310
}
311
EXPORT_SYMBOL_GPL(q6apm_remove_initial_silence);
312
313
int q6apm_remove_trailing_silence(struct device *dev, struct q6apm_graph *graph, uint32_t samples)
314
{
315
struct audioreach_module *module;
316
317
module = q6apm_find_module_by_mid(graph, MODULE_ID_PLACEHOLDER_DECODER);
318
if (!module)
319
return -ENODEV;
320
321
return audioreach_send_u32_param(graph, module, PARAM_ID_REMOVE_TRAILING_SILENCE, samples);
322
}
323
EXPORT_SYMBOL_GPL(q6apm_remove_trailing_silence);
324
325
int q6apm_enable_compress_module(struct device *dev, struct q6apm_graph *graph, bool en)
326
{
327
struct audioreach_module *module;
328
329
module = q6apm_find_module_by_mid(graph, MODULE_ID_PLACEHOLDER_DECODER);
330
if (!module)
331
return -ENODEV;
332
333
return audioreach_send_u32_param(graph, module, PARAM_ID_MODULE_ENABLE, en);
334
}
335
EXPORT_SYMBOL_GPL(q6apm_enable_compress_module);
336
337
int q6apm_set_real_module_id(struct device *dev, struct q6apm_graph *graph,
338
uint32_t codec_id)
339
{
340
struct audioreach_module *module;
341
uint32_t module_id;
342
343
module = q6apm_find_module_by_mid(graph, MODULE_ID_PLACEHOLDER_DECODER);
344
if (!module)
345
return -ENODEV;
346
347
switch (codec_id) {
348
case SND_AUDIOCODEC_MP3:
349
module_id = MODULE_ID_MP3_DECODE;
350
break;
351
case SND_AUDIOCODEC_AAC:
352
module_id = MODULE_ID_AAC_DEC;
353
break;
354
case SND_AUDIOCODEC_FLAC:
355
module_id = MODULE_ID_FLAC_DEC;
356
break;
357
case SND_AUDIOCODEC_OPUS_RAW:
358
module_id = MODULE_ID_OPUS_DEC;
359
break;
360
default:
361
return -EINVAL;
362
}
363
364
return audioreach_send_u32_param(graph, module, PARAM_ID_REAL_MODULE_ID,
365
module_id);
366
}
367
EXPORT_SYMBOL_GPL(q6apm_set_real_module_id);
368
369
int q6apm_graph_media_format_pcm(struct q6apm_graph *graph, struct audioreach_module_config *cfg)
370
{
371
struct audioreach_graph_info *info = graph->info;
372
struct audioreach_sub_graph *sgs;
373
struct audioreach_container *container;
374
struct audioreach_module *module;
375
376
list_for_each_entry(sgs, &info->sg_list, node) {
377
list_for_each_entry(container, &sgs->container_list, node) {
378
list_for_each_entry(module, &container->modules_list, node) {
379
if ((module->module_id == MODULE_ID_WR_SHARED_MEM_EP) ||
380
(module->module_id == MODULE_ID_RD_SHARED_MEM_EP))
381
continue;
382
383
audioreach_set_media_format(graph, module, cfg);
384
}
385
}
386
}
387
388
return 0;
389
390
}
391
EXPORT_SYMBOL_GPL(q6apm_graph_media_format_pcm);
392
393
static int q6apm_graph_get_tx_shmem_module_iid(struct q6apm_graph *graph)
394
{
395
struct audioreach_module *module;
396
397
module = q6apm_find_module_by_mid(graph, MODULE_ID_RD_SHARED_MEM_EP);
398
if (!module)
399
return -ENODEV;
400
401
return module->instance_id;
402
403
}
404
405
int q6apm_graph_get_rx_shmem_module_iid(struct q6apm_graph *graph)
406
{
407
struct audioreach_module *module;
408
409
module = q6apm_find_module_by_mid(graph, MODULE_ID_WR_SHARED_MEM_EP);
410
if (!module)
411
return -ENODEV;
412
413
return module->instance_id;
414
415
}
416
EXPORT_SYMBOL_GPL(q6apm_graph_get_rx_shmem_module_iid);
417
418
int q6apm_write_async(struct q6apm_graph *graph, uint32_t len, uint32_t msw_ts,
419
uint32_t lsw_ts, uint32_t wflags)
420
{
421
struct apm_data_cmd_wr_sh_mem_ep_data_buffer_v2 *write_buffer;
422
struct audio_buffer *ab;
423
struct gpr_pkt *pkt;
424
int rc, iid;
425
426
iid = q6apm_graph_get_rx_shmem_module_iid(graph);
427
pkt = audioreach_alloc_pkt(sizeof(*write_buffer), DATA_CMD_WR_SH_MEM_EP_DATA_BUFFER_V2,
428
graph->rx_data.dsp_buf | (len << APM_WRITE_TOKEN_LEN_SHIFT),
429
graph->port->id, iid);
430
if (IS_ERR(pkt))
431
return PTR_ERR(pkt);
432
433
write_buffer = (void *)pkt + GPR_HDR_SIZE;
434
435
mutex_lock(&graph->lock);
436
ab = &graph->rx_data.buf[graph->rx_data.dsp_buf];
437
438
write_buffer->buf_addr_lsw = lower_32_bits(ab->phys);
439
write_buffer->buf_addr_msw = upper_32_bits(ab->phys);
440
write_buffer->buf_size = len;
441
write_buffer->timestamp_lsw = lsw_ts;
442
write_buffer->timestamp_msw = msw_ts;
443
write_buffer->mem_map_handle = graph->rx_data.mem_map_handle;
444
write_buffer->flags = wflags;
445
446
graph->rx_data.dsp_buf++;
447
448
if (graph->rx_data.dsp_buf >= graph->rx_data.num_periods)
449
graph->rx_data.dsp_buf = 0;
450
451
mutex_unlock(&graph->lock);
452
453
rc = gpr_send_port_pkt(graph->port, pkt);
454
455
kfree(pkt);
456
457
return rc;
458
}
459
EXPORT_SYMBOL_GPL(q6apm_write_async);
460
461
int q6apm_read(struct q6apm_graph *graph)
462
{
463
struct data_cmd_rd_sh_mem_ep_data_buffer_v2 *read_buffer;
464
struct audioreach_graph_data *port;
465
struct audio_buffer *ab;
466
struct gpr_pkt *pkt;
467
int rc, iid;
468
469
iid = q6apm_graph_get_tx_shmem_module_iid(graph);
470
pkt = audioreach_alloc_pkt(sizeof(*read_buffer), DATA_CMD_RD_SH_MEM_EP_DATA_BUFFER_V2,
471
graph->tx_data.dsp_buf, graph->port->id, iid);
472
if (IS_ERR(pkt))
473
return PTR_ERR(pkt);
474
475
read_buffer = (void *)pkt + GPR_HDR_SIZE;
476
477
mutex_lock(&graph->lock);
478
port = &graph->tx_data;
479
ab = &port->buf[port->dsp_buf];
480
481
read_buffer->buf_addr_lsw = lower_32_bits(ab->phys);
482
read_buffer->buf_addr_msw = upper_32_bits(ab->phys);
483
read_buffer->mem_map_handle = port->mem_map_handle;
484
read_buffer->buf_size = ab->size;
485
486
port->dsp_buf++;
487
488
if (port->dsp_buf >= port->num_periods)
489
port->dsp_buf = 0;
490
491
mutex_unlock(&graph->lock);
492
493
rc = gpr_send_port_pkt(graph->port, pkt);
494
kfree(pkt);
495
496
return rc;
497
}
498
EXPORT_SYMBOL_GPL(q6apm_read);
499
500
int q6apm_get_hw_pointer(struct q6apm_graph *graph, int dir)
501
{
502
struct audioreach_graph_data *data;
503
504
if (dir == SNDRV_PCM_STREAM_PLAYBACK)
505
data = &graph->rx_data;
506
else
507
data = &graph->tx_data;
508
509
return (int)atomic_read(&data->hw_ptr);
510
}
511
EXPORT_SYMBOL_GPL(q6apm_get_hw_pointer);
512
513
static int graph_callback(struct gpr_resp_pkt *data, void *priv, int op)
514
{
515
struct data_cmd_rsp_rd_sh_mem_ep_data_buffer_done_v2 *rd_done;
516
struct data_cmd_rsp_wr_sh_mem_ep_data_buffer_done_v2 *done;
517
struct apm_cmd_rsp_shared_mem_map_regions *rsp;
518
struct gpr_ibasic_rsp_result_t *result;
519
struct q6apm_graph *graph = priv;
520
struct gpr_hdr *hdr = &data->hdr;
521
struct device *dev = graph->dev;
522
uint32_t client_event;
523
phys_addr_t phys;
524
int token;
525
526
result = data->payload;
527
528
switch (hdr->opcode) {
529
case DATA_CMD_RSP_WR_SH_MEM_EP_DATA_BUFFER_DONE_V2:
530
if (!graph->ar_graph)
531
break;
532
client_event = APM_CLIENT_EVENT_DATA_WRITE_DONE;
533
mutex_lock(&graph->lock);
534
token = hdr->token & APM_WRITE_TOKEN_MASK;
535
536
done = data->payload;
537
phys = graph->rx_data.buf[token].phys;
538
mutex_unlock(&graph->lock);
539
/* token numbering starts at 0 */
540
atomic_set(&graph->rx_data.hw_ptr, token + 1);
541
if (lower_32_bits(phys) == done->buf_addr_lsw &&
542
upper_32_bits(phys) == done->buf_addr_msw) {
543
graph->result.opcode = hdr->opcode;
544
graph->result.status = done->status;
545
if (graph->cb)
546
graph->cb(client_event, hdr->token, data->payload, graph->priv);
547
} else {
548
dev_err(dev, "WR BUFF Unexpected addr %08x-%08x\n", done->buf_addr_lsw,
549
done->buf_addr_msw);
550
}
551
552
break;
553
case APM_CMD_RSP_SHARED_MEM_MAP_REGIONS:
554
graph->result.opcode = hdr->opcode;
555
graph->result.status = 0;
556
rsp = data->payload;
557
558
if (hdr->token == SNDRV_PCM_STREAM_PLAYBACK)
559
graph->rx_data.mem_map_handle = rsp->mem_map_handle;
560
else
561
graph->tx_data.mem_map_handle = rsp->mem_map_handle;
562
563
wake_up(&graph->cmd_wait);
564
break;
565
case DATA_CMD_RSP_RD_SH_MEM_EP_DATA_BUFFER_V2:
566
if (!graph->ar_graph)
567
break;
568
client_event = APM_CLIENT_EVENT_DATA_READ_DONE;
569
mutex_lock(&graph->lock);
570
rd_done = data->payload;
571
phys = graph->tx_data.buf[hdr->token].phys;
572
mutex_unlock(&graph->lock);
573
/* token numbering starts at 0 */
574
atomic_set(&graph->tx_data.hw_ptr, hdr->token + 1);
575
576
if (upper_32_bits(phys) == rd_done->buf_addr_msw &&
577
lower_32_bits(phys) == rd_done->buf_addr_lsw) {
578
graph->result.opcode = hdr->opcode;
579
graph->result.status = rd_done->status;
580
if (graph->cb)
581
graph->cb(client_event, hdr->token, data->payload, graph->priv);
582
} else {
583
dev_err(dev, "RD BUFF Unexpected addr %08x-%08x\n", rd_done->buf_addr_lsw,
584
rd_done->buf_addr_msw);
585
}
586
break;
587
case DATA_CMD_WR_SH_MEM_EP_EOS_RENDERED:
588
client_event = APM_CLIENT_EVENT_CMD_EOS_DONE;
589
if (graph->cb)
590
graph->cb(client_event, hdr->token, data->payload, graph->priv);
591
break;
592
case GPR_BASIC_RSP_RESULT:
593
switch (result->opcode) {
594
case APM_CMD_SHARED_MEM_UNMAP_REGIONS:
595
graph->result.opcode = result->opcode;
596
graph->result.status = 0;
597
if (hdr->token == SNDRV_PCM_STREAM_PLAYBACK)
598
graph->rx_data.mem_map_handle = 0;
599
else
600
graph->tx_data.mem_map_handle = 0;
601
602
wake_up(&graph->cmd_wait);
603
break;
604
case APM_CMD_SHARED_MEM_MAP_REGIONS:
605
case DATA_CMD_WR_SH_MEM_EP_MEDIA_FORMAT:
606
case APM_CMD_SET_CFG:
607
graph->result.opcode = result->opcode;
608
graph->result.status = result->status;
609
if (result->status)
610
dev_err(dev, "Error (%d) Processing 0x%08x cmd\n",
611
result->status, result->opcode);
612
wake_up(&graph->cmd_wait);
613
break;
614
default:
615
break;
616
}
617
break;
618
default:
619
break;
620
}
621
return 0;
622
}
623
624
struct q6apm_graph *q6apm_graph_open(struct device *dev, q6apm_cb cb,
625
void *priv, int graph_id)
626
{
627
struct q6apm *apm = dev_get_drvdata(dev->parent);
628
struct audioreach_graph *ar_graph;
629
struct q6apm_graph *graph;
630
int ret;
631
632
ar_graph = q6apm_get_audioreach_graph(apm, graph_id);
633
if (IS_ERR(ar_graph)) {
634
dev_err(dev, "No graph found with id %d\n", graph_id);
635
return ERR_CAST(ar_graph);
636
}
637
638
graph = kzalloc(sizeof(*graph), GFP_KERNEL);
639
if (!graph) {
640
ret = -ENOMEM;
641
goto put_ar_graph;
642
}
643
644
graph->apm = apm;
645
graph->priv = priv;
646
graph->cb = cb;
647
graph->info = ar_graph->info;
648
graph->ar_graph = ar_graph;
649
graph->id = ar_graph->id;
650
graph->dev = dev;
651
652
mutex_init(&graph->lock);
653
init_waitqueue_head(&graph->cmd_wait);
654
655
graph->port = gpr_alloc_port(apm->gdev, dev, graph_callback, graph);
656
if (IS_ERR(graph->port)) {
657
ret = PTR_ERR(graph->port);
658
goto free_graph;
659
}
660
661
return graph;
662
663
free_graph:
664
kfree(graph);
665
put_ar_graph:
666
kref_put(&ar_graph->refcount, q6apm_put_audioreach_graph);
667
return ERR_PTR(ret);
668
}
669
EXPORT_SYMBOL_GPL(q6apm_graph_open);
670
671
int q6apm_graph_close(struct q6apm_graph *graph)
672
{
673
struct audioreach_graph *ar_graph = graph->ar_graph;
674
675
graph->ar_graph = NULL;
676
kref_put(&ar_graph->refcount, q6apm_put_audioreach_graph);
677
gpr_free_port(graph->port);
678
kfree(graph);
679
680
return 0;
681
}
682
EXPORT_SYMBOL_GPL(q6apm_graph_close);
683
684
int q6apm_graph_prepare(struct q6apm_graph *graph)
685
{
686
return audioreach_graph_mgmt_cmd(graph->ar_graph, APM_CMD_GRAPH_PREPARE);
687
}
688
EXPORT_SYMBOL_GPL(q6apm_graph_prepare);
689
690
int q6apm_graph_start(struct q6apm_graph *graph)
691
{
692
struct audioreach_graph *ar_graph = graph->ar_graph;
693
int ret = 0;
694
695
if (ar_graph->start_count == 0)
696
ret = audioreach_graph_mgmt_cmd(ar_graph, APM_CMD_GRAPH_START);
697
698
ar_graph->start_count++;
699
700
return ret;
701
}
702
EXPORT_SYMBOL_GPL(q6apm_graph_start);
703
704
int q6apm_graph_stop(struct q6apm_graph *graph)
705
{
706
struct audioreach_graph *ar_graph = graph->ar_graph;
707
708
if (--ar_graph->start_count > 0)
709
return 0;
710
711
return audioreach_graph_mgmt_cmd(ar_graph, APM_CMD_GRAPH_STOP);
712
}
713
EXPORT_SYMBOL_GPL(q6apm_graph_stop);
714
715
int q6apm_graph_flush(struct q6apm_graph *graph)
716
{
717
return audioreach_graph_mgmt_cmd(graph->ar_graph, APM_CMD_GRAPH_FLUSH);
718
}
719
EXPORT_SYMBOL_GPL(q6apm_graph_flush);
720
721
static int q6apm_audio_probe(struct snd_soc_component *component)
722
{
723
return audioreach_tplg_init(component);
724
}
725
726
static void q6apm_audio_remove(struct snd_soc_component *component)
727
{
728
/* remove topology */
729
snd_soc_tplg_component_remove(component);
730
}
731
732
#define APM_AUDIO_DRV_NAME "q6apm-audio"
733
734
static const struct snd_soc_component_driver q6apm_audio_component = {
735
.name = APM_AUDIO_DRV_NAME,
736
.probe = q6apm_audio_probe,
737
.remove = q6apm_audio_remove,
738
};
739
740
static int apm_probe(gpr_device_t *gdev)
741
{
742
struct device *dev = &gdev->dev;
743
struct q6apm *apm;
744
int ret;
745
746
apm = devm_kzalloc(dev, sizeof(*apm), GFP_KERNEL);
747
if (!apm)
748
return -ENOMEM;
749
750
dev_set_drvdata(dev, apm);
751
752
mutex_init(&apm->lock);
753
apm->dev = dev;
754
apm->gdev = gdev;
755
init_waitqueue_head(&apm->wait);
756
757
INIT_LIST_HEAD(&apm->widget_list);
758
idr_init(&apm->graph_idr);
759
idr_init(&apm->graph_info_idr);
760
idr_init(&apm->sub_graphs_idr);
761
idr_init(&apm->containers_idr);
762
763
idr_init(&apm->modules_idr);
764
765
g_apm = apm;
766
767
q6apm_get_apm_state(apm);
768
769
ret = devm_snd_soc_register_component(dev, &q6apm_audio_component, NULL, 0);
770
if (ret < 0) {
771
dev_err(dev, "failed to register q6apm: %d\n", ret);
772
return ret;
773
}
774
775
return of_platform_populate(dev->of_node, NULL, NULL, dev);
776
}
777
778
struct audioreach_module *q6apm_find_module_by_mid(struct q6apm_graph *graph, uint32_t mid)
779
{
780
struct audioreach_graph_info *info = graph->info;
781
struct q6apm *apm = graph->apm;
782
783
return __q6apm_find_module_by_mid(apm, info, mid);
784
785
}
786
787
static int apm_callback(struct gpr_resp_pkt *data, void *priv, int op)
788
{
789
gpr_device_t *gdev = priv;
790
struct q6apm *apm = dev_get_drvdata(&gdev->dev);
791
struct device *dev = &gdev->dev;
792
struct gpr_ibasic_rsp_result_t *result;
793
struct gpr_hdr *hdr = &data->hdr;
794
795
result = data->payload;
796
797
switch (hdr->opcode) {
798
case APM_CMD_RSP_GET_SPF_STATE:
799
apm->result.opcode = hdr->opcode;
800
apm->result.status = 0;
801
/* First word of result it state */
802
apm->state = result->opcode;
803
wake_up(&apm->wait);
804
break;
805
case GPR_BASIC_RSP_RESULT:
806
switch (result->opcode) {
807
case APM_CMD_GRAPH_START:
808
case APM_CMD_GRAPH_OPEN:
809
case APM_CMD_GRAPH_PREPARE:
810
case APM_CMD_GRAPH_CLOSE:
811
case APM_CMD_GRAPH_FLUSH:
812
case APM_CMD_GRAPH_STOP:
813
case APM_CMD_SET_CFG:
814
apm->result.opcode = result->opcode;
815
apm->result.status = result->status;
816
if (result->status)
817
dev_err(dev, "Error (%d) Processing 0x%08x cmd\n", result->status,
818
result->opcode);
819
wake_up(&apm->wait);
820
break;
821
default:
822
break;
823
}
824
break;
825
default:
826
break;
827
}
828
829
return 0;
830
}
831
832
#ifdef CONFIG_OF
833
static const struct of_device_id apm_device_id[] = {
834
{ .compatible = "qcom,q6apm" },
835
{},
836
};
837
MODULE_DEVICE_TABLE(of, apm_device_id);
838
#endif
839
840
static gpr_driver_t apm_driver = {
841
.probe = apm_probe,
842
.gpr_callback = apm_callback,
843
.driver = {
844
.name = "qcom-apm",
845
.of_match_table = of_match_ptr(apm_device_id),
846
},
847
};
848
849
module_gpr_driver(apm_driver);
850
MODULE_DESCRIPTION("Audio Process Manager");
851
MODULE_LICENSE("GPL");
852
853