Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/soc/qcom/qdsp6/q6adm.c
53364 views
1
// SPDX-License-Identifier: GPL-2.0
2
// Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
3
// Copyright (c) 2018, Linaro Limited
4
5
#include <linux/device.h>
6
#include <linux/jiffies.h>
7
#include <linux/kernel.h>
8
#include <linux/kref.h>
9
#include <linux/module.h>
10
#include <linux/of.h>
11
#include <linux/of_platform.h>
12
#include <linux/platform_device.h>
13
#include <linux/sched.h>
14
#include <linux/slab.h>
15
#include <linux/soc/qcom/apr.h>
16
#include <linux/wait.h>
17
#include <sound/asound.h>
18
#include "q6adm.h"
19
#include "q6afe.h"
20
#include "q6core.h"
21
#include "q6dsp-common.h"
22
#include "q6dsp-errno.h"
23
24
#define ADM_CMD_DEVICE_OPEN_V5 0x00010326
25
#define ADM_CMDRSP_DEVICE_OPEN_V5 0x00010329
26
#define ADM_CMD_DEVICE_CLOSE_V5 0x00010327
27
#define ADM_CMD_MATRIX_MAP_ROUTINGS_V5 0x00010325
28
29
#define TIMEOUT_MS 1000
30
#define RESET_COPP_ID 99
31
#define INVALID_COPP_ID 0xFF
32
/* Definition for a legacy device session. */
33
#define ADM_LEGACY_DEVICE_SESSION 0
34
#define ADM_MATRIX_ID_AUDIO_RX 0
35
#define ADM_MATRIX_ID_AUDIO_TX 1
36
37
struct q6copp {
38
int afe_port;
39
int copp_idx;
40
int id;
41
int topology;
42
int mode;
43
int rate;
44
int bit_width;
45
int channels;
46
int app_type;
47
int acdb_id;
48
49
struct aprv2_ibasic_rsp_result_t result;
50
struct kref refcount;
51
wait_queue_head_t wait;
52
struct list_head node;
53
struct q6adm *adm;
54
};
55
56
struct q6adm {
57
struct apr_device *apr;
58
struct device *dev;
59
struct q6core_svc_api_info ainfo;
60
unsigned long copp_bitmap[AFE_MAX_PORTS];
61
struct list_head copps_list;
62
spinlock_t copps_list_lock;
63
struct aprv2_ibasic_rsp_result_t result;
64
struct mutex lock;
65
wait_queue_head_t matrix_map_wait;
66
};
67
68
struct q6adm_cmd_device_open_v5 {
69
u16 flags;
70
u16 mode_of_operation;
71
u16 endpoint_id_1;
72
u16 endpoint_id_2;
73
u32 topology_id;
74
u16 dev_num_channel;
75
u16 bit_width;
76
u32 sample_rate;
77
u8 dev_channel_mapping[8];
78
} __packed;
79
80
struct q6adm_cmd_matrix_map_routings_v5 {
81
u32 matrix_id;
82
u32 num_sessions;
83
} __packed;
84
85
struct q6adm_session_map_node_v5 {
86
u16 session_id;
87
u16 num_copps;
88
} __packed;
89
90
static struct q6copp *q6adm_find_copp(struct q6adm *adm, int port_idx,
91
int copp_idx)
92
{
93
struct q6copp *c;
94
struct q6copp *ret = NULL;
95
unsigned long flags;
96
97
spin_lock_irqsave(&adm->copps_list_lock, flags);
98
list_for_each_entry(c, &adm->copps_list, node) {
99
if ((port_idx == c->afe_port) && (copp_idx == c->copp_idx)) {
100
ret = c;
101
kref_get(&c->refcount);
102
break;
103
}
104
}
105
106
spin_unlock_irqrestore(&adm->copps_list_lock, flags);
107
108
return ret;
109
110
}
111
112
static int q6adm_apr_send_copp_pkt(struct q6adm *adm, struct q6copp *copp,
113
struct apr_pkt *pkt, uint32_t rsp_opcode)
114
{
115
struct device *dev = adm->dev;
116
uint32_t opcode = pkt->hdr.opcode;
117
int ret;
118
119
mutex_lock(&adm->lock);
120
copp->result.opcode = 0;
121
copp->result.status = 0;
122
ret = apr_send_pkt(adm->apr, pkt);
123
if (ret < 0) {
124
dev_err(dev, "Failed to send APR packet\n");
125
ret = -EINVAL;
126
goto err;
127
}
128
129
/* Wait for the callback with copp id */
130
if (rsp_opcode)
131
ret = wait_event_timeout(copp->wait,
132
(copp->result.opcode == opcode) ||
133
(copp->result.opcode == rsp_opcode),
134
msecs_to_jiffies(TIMEOUT_MS));
135
else
136
ret = wait_event_timeout(copp->wait,
137
(copp->result.opcode == opcode),
138
msecs_to_jiffies(TIMEOUT_MS));
139
140
if (!ret) {
141
dev_err(dev, "ADM copp cmd timedout\n");
142
ret = -ETIMEDOUT;
143
} else if (copp->result.status > 0) {
144
dev_err(dev, "DSP returned error[%d]\n",
145
copp->result.status);
146
ret = -EINVAL;
147
}
148
149
err:
150
mutex_unlock(&adm->lock);
151
return ret;
152
}
153
154
static int q6adm_device_close(struct q6adm *adm, struct q6copp *copp,
155
int port_id, int copp_idx)
156
{
157
struct apr_pkt close;
158
159
close.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
160
APR_HDR_LEN(APR_HDR_SIZE),
161
APR_PKT_VER);
162
close.hdr.pkt_size = sizeof(close);
163
close.hdr.src_port = port_id;
164
close.hdr.dest_port = copp->id;
165
close.hdr.token = port_id << 16 | copp_idx;
166
close.hdr.opcode = ADM_CMD_DEVICE_CLOSE_V5;
167
168
return q6adm_apr_send_copp_pkt(adm, copp, &close, 0);
169
}
170
171
static void q6adm_free_copp(struct kref *ref)
172
{
173
struct q6copp *c = container_of(ref, struct q6copp, refcount);
174
struct q6adm *adm = c->adm;
175
unsigned long flags;
176
int ret;
177
178
ret = q6adm_device_close(adm, c, c->afe_port, c->copp_idx);
179
if (ret < 0)
180
dev_err(adm->dev, "Failed to close copp %d\n", ret);
181
182
spin_lock_irqsave(&adm->copps_list_lock, flags);
183
clear_bit(c->copp_idx, &adm->copp_bitmap[c->afe_port]);
184
list_del(&c->node);
185
spin_unlock_irqrestore(&adm->copps_list_lock, flags);
186
kfree(c);
187
}
188
189
static int q6adm_callback(struct apr_device *adev, const struct apr_resp_pkt *data)
190
{
191
const struct aprv2_ibasic_rsp_result_t *result = data->payload;
192
int port_idx, copp_idx;
193
const struct apr_hdr *hdr = &data->hdr;
194
struct q6copp *copp;
195
struct q6adm *adm = dev_get_drvdata(&adev->dev);
196
197
if (!data->payload_size)
198
return 0;
199
200
copp_idx = (hdr->token) & 0XFF;
201
port_idx = ((hdr->token) >> 16) & 0xFF;
202
if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
203
dev_err(&adev->dev, "Invalid port idx %d token %d\n",
204
port_idx, hdr->token);
205
return 0;
206
}
207
if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
208
dev_err(&adev->dev, "Invalid copp idx %d token %d\n",
209
copp_idx, hdr->token);
210
return 0;
211
}
212
213
switch (hdr->opcode) {
214
case APR_BASIC_RSP_RESULT: {
215
if (result->status != 0) {
216
dev_err(&adev->dev, "cmd = 0x%x return error = 0x%x\n",
217
result->opcode, result->status);
218
}
219
switch (result->opcode) {
220
case ADM_CMD_DEVICE_OPEN_V5:
221
case ADM_CMD_DEVICE_CLOSE_V5:
222
list_for_each_entry(copp, &adm->copps_list, node) {
223
if ((port_idx == copp->afe_port) && (copp_idx == copp->copp_idx)) {
224
copp->result = *result;
225
wake_up(&copp->wait);
226
break;
227
}
228
}
229
break;
230
case ADM_CMD_MATRIX_MAP_ROUTINGS_V5:
231
adm->result = *result;
232
wake_up(&adm->matrix_map_wait);
233
break;
234
235
default:
236
dev_err(&adev->dev, "Unknown Cmd: 0x%x\n",
237
result->opcode);
238
break;
239
}
240
return 0;
241
}
242
case ADM_CMDRSP_DEVICE_OPEN_V5: {
243
struct adm_cmd_rsp_device_open_v5 {
244
u32 status;
245
u16 copp_id;
246
u16 reserved;
247
} __packed *open = data->payload;
248
249
copp = q6adm_find_copp(adm, port_idx, copp_idx);
250
if (!copp)
251
return 0;
252
253
if (open->copp_id == INVALID_COPP_ID) {
254
dev_err(&adev->dev, "Invalid coppid rxed %d\n",
255
open->copp_id);
256
copp->result.status = ADSP_EBADPARAM;
257
wake_up(&copp->wait);
258
kref_put(&copp->refcount, q6adm_free_copp);
259
break;
260
}
261
copp->result.opcode = hdr->opcode;
262
copp->id = open->copp_id;
263
wake_up(&copp->wait);
264
kref_put(&copp->refcount, q6adm_free_copp);
265
}
266
break;
267
default:
268
dev_err(&adev->dev, "Unknown cmd:0x%x\n",
269
hdr->opcode);
270
break;
271
}
272
273
return 0;
274
}
275
276
static struct q6copp *q6adm_alloc_copp(struct q6adm *adm, int port_idx)
277
{
278
struct q6copp *c;
279
int idx;
280
281
idx = find_first_zero_bit(&adm->copp_bitmap[port_idx],
282
MAX_COPPS_PER_PORT);
283
284
if (idx >= MAX_COPPS_PER_PORT)
285
return ERR_PTR(-EBUSY);
286
287
c = kzalloc(sizeof(*c), GFP_ATOMIC);
288
if (!c)
289
return ERR_PTR(-ENOMEM);
290
291
set_bit(idx, &adm->copp_bitmap[port_idx]);
292
c->copp_idx = idx;
293
c->afe_port = port_idx;
294
c->adm = adm;
295
296
init_waitqueue_head(&c->wait);
297
298
return c;
299
}
300
301
static struct q6copp *q6adm_find_matching_copp(struct q6adm *adm,
302
int port_id, int topology,
303
int mode, int rate,
304
int channel_mode, int bit_width,
305
int app_type)
306
{
307
struct q6copp *c;
308
struct q6copp *ret = NULL;
309
unsigned long flags;
310
311
spin_lock_irqsave(&adm->copps_list_lock, flags);
312
313
list_for_each_entry(c, &adm->copps_list, node) {
314
if ((port_id == c->afe_port) && (topology == c->topology) &&
315
(mode == c->mode) && (rate == c->rate) &&
316
(bit_width == c->bit_width) && (app_type == c->app_type)) {
317
ret = c;
318
kref_get(&c->refcount);
319
}
320
}
321
spin_unlock_irqrestore(&adm->copps_list_lock, flags);
322
323
return ret;
324
}
325
326
static int q6adm_device_open(struct q6adm *adm, struct q6copp *copp,
327
int port_id, int path, int topology,
328
int channel_mode, int bit_width, int rate)
329
{
330
struct q6adm_cmd_device_open_v5 *open;
331
int afe_port = q6afe_get_port_id(port_id);
332
struct apr_pkt *pkt;
333
int ret, pkt_size = APR_HDR_SIZE + sizeof(*open);
334
335
void *p __free(kfree) = kzalloc(pkt_size, GFP_KERNEL);
336
if (!p)
337
return -ENOMEM;
338
339
pkt = p;
340
open = p + APR_HDR_SIZE;
341
pkt->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
342
APR_HDR_LEN(APR_HDR_SIZE),
343
APR_PKT_VER);
344
pkt->hdr.pkt_size = pkt_size;
345
pkt->hdr.src_port = afe_port;
346
pkt->hdr.dest_port = afe_port;
347
pkt->hdr.token = port_id << 16 | copp->copp_idx;
348
pkt->hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
349
open->flags = ADM_LEGACY_DEVICE_SESSION;
350
open->mode_of_operation = path;
351
open->endpoint_id_1 = afe_port;
352
open->topology_id = topology;
353
open->dev_num_channel = channel_mode & 0x00FF;
354
open->bit_width = bit_width;
355
open->sample_rate = rate;
356
357
ret = q6dsp_map_channels(&open->dev_channel_mapping[0],
358
channel_mode);
359
if (ret)
360
return ret;
361
362
return q6adm_apr_send_copp_pkt(adm, copp, pkt, ADM_CMDRSP_DEVICE_OPEN_V5);
363
}
364
365
/**
366
* q6adm_open() - open adm and grab a free copp
367
*
368
* @dev: Pointer to adm child device.
369
* @port_id: port id
370
* @path: playback or capture path.
371
* @rate: rate at which copp is required.
372
* @channel_mode: channel mode
373
* @topology: adm topology id
374
* @perf_mode: performace mode.
375
* @bit_width: audio sample bit width
376
* @app_type: Application type.
377
* @acdb_id: ACDB id
378
*
379
* Return: Will be an negative on error or a valid copp pointer on success.
380
*/
381
struct q6copp *q6adm_open(struct device *dev, int port_id, int path, int rate,
382
int channel_mode, int topology, int perf_mode,
383
uint16_t bit_width, int app_type, int acdb_id)
384
{
385
struct q6adm *adm = dev_get_drvdata(dev->parent);
386
struct q6copp *copp;
387
unsigned long flags;
388
int ret = 0;
389
390
if (port_id < 0) {
391
dev_err(dev, "Invalid port_id %d\n", port_id);
392
return ERR_PTR(-EINVAL);
393
}
394
395
copp = q6adm_find_matching_copp(adm, port_id, topology, perf_mode,
396
rate, channel_mode, bit_width, app_type);
397
if (copp) {
398
dev_err(dev, "Found Matching Copp 0x%x\n", copp->copp_idx);
399
return copp;
400
}
401
402
spin_lock_irqsave(&adm->copps_list_lock, flags);
403
copp = q6adm_alloc_copp(adm, port_id);
404
if (IS_ERR(copp)) {
405
spin_unlock_irqrestore(&adm->copps_list_lock, flags);
406
return ERR_CAST(copp);
407
}
408
409
list_add_tail(&copp->node, &adm->copps_list);
410
spin_unlock_irqrestore(&adm->copps_list_lock, flags);
411
412
kref_init(&copp->refcount);
413
copp->topology = topology;
414
copp->mode = perf_mode;
415
copp->rate = rate;
416
copp->channels = channel_mode;
417
copp->bit_width = bit_width;
418
copp->app_type = app_type;
419
420
ret = q6adm_device_open(adm, copp, port_id, path, topology,
421
channel_mode, bit_width, rate);
422
if (ret < 0) {
423
kref_put(&copp->refcount, q6adm_free_copp);
424
return ERR_PTR(ret);
425
}
426
427
return copp;
428
}
429
EXPORT_SYMBOL_GPL(q6adm_open);
430
431
/**
432
* q6adm_get_copp_id() - get copp index
433
*
434
* @copp: Pointer to valid copp
435
*
436
* Return: Will be an negative on error or a valid copp index on success.
437
**/
438
int q6adm_get_copp_id(struct q6copp *copp)
439
{
440
if (!copp)
441
return -EINVAL;
442
443
return copp->copp_idx;
444
}
445
EXPORT_SYMBOL_GPL(q6adm_get_copp_id);
446
447
/**
448
* q6adm_matrix_map() - Map asm streams and afe ports using payload
449
*
450
* @dev: Pointer to adm child device.
451
* @path: playback or capture path.
452
* @payload_map: map between session id and afe ports.
453
* @perf_mode: Performace mode.
454
*
455
* Return: Will be an negative on error or a zero on success.
456
*/
457
int q6adm_matrix_map(struct device *dev, int path,
458
struct route_payload payload_map, int perf_mode)
459
{
460
struct q6adm *adm = dev_get_drvdata(dev->parent);
461
struct q6adm_cmd_matrix_map_routings_v5 *route;
462
struct q6adm_session_map_node_v5 *node;
463
struct apr_pkt *pkt;
464
uint16_t *copps_list;
465
int ret, i, copp_idx;
466
/* Assumes port_ids have already been validated during adm_open */
467
struct q6copp *copp;
468
int pkt_size = (APR_HDR_SIZE + sizeof(*route) + sizeof(*node) +
469
(sizeof(uint32_t) * payload_map.num_copps));
470
471
void *matrix_map __free(kfree) = kzalloc(pkt_size, GFP_KERNEL);
472
if (!matrix_map)
473
return -ENOMEM;
474
475
pkt = matrix_map;
476
route = matrix_map + APR_HDR_SIZE;
477
node = matrix_map + APR_HDR_SIZE + sizeof(*route);
478
copps_list = matrix_map + APR_HDR_SIZE + sizeof(*route) + sizeof(*node);
479
480
pkt->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
481
APR_HDR_LEN(APR_HDR_SIZE),
482
APR_PKT_VER);
483
pkt->hdr.pkt_size = pkt_size;
484
pkt->hdr.token = 0;
485
pkt->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
486
route->num_sessions = 1;
487
488
switch (path) {
489
case ADM_PATH_PLAYBACK:
490
route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
491
break;
492
case ADM_PATH_LIVE_REC:
493
route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
494
break;
495
default:
496
dev_err(dev, "Wrong path set[%d]\n", path);
497
break;
498
}
499
500
node->session_id = payload_map.session_id;
501
node->num_copps = payload_map.num_copps;
502
503
for (i = 0; i < payload_map.num_copps; i++) {
504
int port_idx = payload_map.port_id[i];
505
506
if (port_idx < 0) {
507
dev_err(dev, "Invalid port_id %d\n",
508
payload_map.port_id[i]);
509
return -EINVAL;
510
}
511
copp_idx = payload_map.copp_idx[i];
512
513
copp = q6adm_find_copp(adm, port_idx, copp_idx);
514
if (!copp)
515
return -EINVAL;
516
517
copps_list[i] = copp->id;
518
kref_put(&copp->refcount, q6adm_free_copp);
519
}
520
521
mutex_lock(&adm->lock);
522
adm->result.status = 0;
523
adm->result.opcode = 0;
524
525
ret = apr_send_pkt(adm->apr, pkt);
526
if (ret < 0) {
527
dev_err(dev, "routing for stream %d failed ret %d\n",
528
payload_map.session_id, ret);
529
goto fail_cmd;
530
}
531
ret = wait_event_timeout(adm->matrix_map_wait,
532
adm->result.opcode == pkt->hdr.opcode,
533
msecs_to_jiffies(TIMEOUT_MS));
534
if (!ret) {
535
dev_err(dev, "routing for stream %d failed\n",
536
payload_map.session_id);
537
ret = -ETIMEDOUT;
538
goto fail_cmd;
539
} else if (adm->result.status > 0) {
540
dev_err(dev, "DSP returned error[%d]\n",
541
adm->result.status);
542
ret = -EINVAL;
543
goto fail_cmd;
544
}
545
546
fail_cmd:
547
mutex_unlock(&adm->lock);
548
return ret;
549
}
550
EXPORT_SYMBOL_GPL(q6adm_matrix_map);
551
552
/**
553
* q6adm_close() - Close adm copp
554
*
555
* @dev: Pointer to adm child device.
556
* @copp: pointer to previously opened copp
557
*
558
* Return: Will be an negative on error or a zero on success.
559
*/
560
int q6adm_close(struct device *dev, struct q6copp *copp)
561
{
562
kref_put(&copp->refcount, q6adm_free_copp);
563
564
return 0;
565
}
566
EXPORT_SYMBOL_GPL(q6adm_close);
567
568
static int q6adm_probe(struct apr_device *adev)
569
{
570
struct device *dev = &adev->dev;
571
struct q6adm *adm;
572
573
adm = devm_kzalloc(dev, sizeof(*adm), GFP_KERNEL);
574
if (!adm)
575
return -ENOMEM;
576
577
adm->apr = adev;
578
dev_set_drvdata(dev, adm);
579
adm->dev = dev;
580
q6core_get_svc_api_info(adev->svc_id, &adm->ainfo);
581
mutex_init(&adm->lock);
582
init_waitqueue_head(&adm->matrix_map_wait);
583
584
INIT_LIST_HEAD(&adm->copps_list);
585
spin_lock_init(&adm->copps_list_lock);
586
587
return devm_of_platform_populate(dev);
588
}
589
590
#ifdef CONFIG_OF
591
static const struct of_device_id q6adm_device_id[] = {
592
{ .compatible = "qcom,q6adm" },
593
{},
594
};
595
MODULE_DEVICE_TABLE(of, q6adm_device_id);
596
#endif
597
598
static struct apr_driver qcom_q6adm_driver = {
599
.probe = q6adm_probe,
600
.callback = q6adm_callback,
601
.driver = {
602
.name = "qcom-q6adm",
603
.of_match_table = of_match_ptr(q6adm_device_id),
604
},
605
};
606
607
module_apr_driver(qcom_q6adm_driver);
608
MODULE_DESCRIPTION("Q6 Audio Device Manager");
609
MODULE_LICENSE("GPL v2");
610
611