1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
3 // Copyright (c) 2018, Linaro Limited
5 #include <linux/slab.h>
6 #include <linux/wait.h>
7 #include <linux/kernel.h>
8 #include <linux/device.h>
9 #include <linux/module.h>
10 #include <linux/sched.h>
11 #include <linux/jiffies.h>
13 #include <linux/of_platform.h>
14 #include <linux/kref.h>
15 #include <linux/wait.h>
16 #include <linux/soc/qcom/apr.h>
17 #include <linux/platform_device.h>
18 #include <sound/asound.h>
22 #include "q6dsp-errno.h"
23 #include "q6dsp-common.h"
25 #define ADM_CMD_DEVICE_OPEN_V5 0x00010326
26 #define ADM_CMDRSP_DEVICE_OPEN_V5 0x00010329
27 #define ADM_CMD_DEVICE_CLOSE_V5 0x00010327
28 #define ADM_CMD_MATRIX_MAP_ROUTINGS_V5 0x00010325
30 #define TIMEOUT_MS 1000
31 #define RESET_COPP_ID 99
32 #define INVALID_COPP_ID 0xFF
33 /* Definition for a legacy device session. */
34 #define ADM_LEGACY_DEVICE_SESSION 0
35 #define ADM_MATRIX_ID_AUDIO_RX 0
36 #define ADM_MATRIX_ID_AUDIO_TX 1
50 struct aprv2_ibasic_rsp_result_t result;
52 wait_queue_head_t wait;
53 struct list_head node;
58 struct apr_device *apr;
60 struct q6core_svc_api_info ainfo;
61 unsigned long copp_bitmap[AFE_MAX_PORTS];
62 struct list_head copps_list;
63 spinlock_t copps_list_lock;
64 struct aprv2_ibasic_rsp_result_t result;
66 wait_queue_head_t matrix_map_wait;
69 struct q6adm_cmd_device_open_v5 {
71 u16 mode_of_operation;
78 u8 dev_channel_mapping[8];
81 struct q6adm_cmd_matrix_map_routings_v5 {
86 struct q6adm_session_map_node_v5 {
91 static struct q6copp *q6adm_find_copp(struct q6adm *adm, int port_idx,
94 struct q6copp *c = NULL;
95 struct q6copp *ret = NULL;
98 spin_lock_irqsave(&adm->copps_list_lock, flags);
99 list_for_each_entry(c, &adm->copps_list, node) {
100 if ((port_idx == c->afe_port) && (copp_idx == c->copp_idx)) {
102 kref_get(&c->refcount);
107 spin_unlock_irqrestore(&adm->copps_list_lock, flags);
113 static void q6adm_free_copp(struct kref *ref)
115 struct q6copp *c = container_of(ref, struct q6copp, refcount);
116 struct q6adm *adm = c->adm;
119 spin_lock_irqsave(&adm->copps_list_lock, flags);
120 clear_bit(c->copp_idx, &adm->copp_bitmap[c->afe_port]);
122 spin_unlock_irqrestore(&adm->copps_list_lock, flags);
126 static int q6adm_callback(struct apr_device *adev, struct apr_resp_pkt *data)
128 struct aprv2_ibasic_rsp_result_t *result = data->payload;
129 int port_idx, copp_idx;
130 struct apr_hdr *hdr = &data->hdr;
132 struct q6adm *adm = dev_get_drvdata(&adev->dev);
134 if (!data->payload_size)
137 copp_idx = (hdr->token) & 0XFF;
138 port_idx = ((hdr->token) >> 16) & 0xFF;
139 if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
140 dev_err(&adev->dev, "Invalid port idx %d token %d\n",
141 port_idx, hdr->token);
144 if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
145 dev_err(&adev->dev, "Invalid copp idx %d token %d\n",
146 copp_idx, hdr->token);
150 switch (hdr->opcode) {
151 case APR_BASIC_RSP_RESULT: {
152 if (result->status != 0) {
153 dev_err(&adev->dev, "cmd = 0x%x return error = 0x%x\n",
154 result->opcode, result->status);
156 switch (result->opcode) {
157 case ADM_CMD_DEVICE_OPEN_V5:
158 case ADM_CMD_DEVICE_CLOSE_V5:
159 copp = q6adm_find_copp(adm, port_idx, copp_idx);
163 copp->result = *result;
164 wake_up(&copp->wait);
165 kref_put(&copp->refcount, q6adm_free_copp);
167 case ADM_CMD_MATRIX_MAP_ROUTINGS_V5:
168 adm->result = *result;
169 wake_up(&adm->matrix_map_wait);
173 dev_err(&adev->dev, "Unknown Cmd: 0x%x\n",
179 case ADM_CMDRSP_DEVICE_OPEN_V5: {
180 struct adm_cmd_rsp_device_open_v5 {
184 } __packed * open = data->payload;
186 copp = q6adm_find_copp(adm, port_idx, copp_idx);
190 if (open->copp_id == INVALID_COPP_ID) {
191 dev_err(&adev->dev, "Invalid coppid rxed %d\n",
193 copp->result.status = ADSP_EBADPARAM;
194 wake_up(&copp->wait);
195 kref_put(&copp->refcount, q6adm_free_copp);
198 copp->result.opcode = hdr->opcode;
199 copp->id = open->copp_id;
200 wake_up(&copp->wait);
201 kref_put(&copp->refcount, q6adm_free_copp);
205 dev_err(&adev->dev, "Unknown cmd:0x%x\n",
213 static struct q6copp *q6adm_alloc_copp(struct q6adm *adm, int port_idx)
218 idx = find_first_zero_bit(&adm->copp_bitmap[port_idx],
221 if (idx > MAX_COPPS_PER_PORT)
222 return ERR_PTR(-EBUSY);
224 c = kzalloc(sizeof(*c), GFP_ATOMIC);
226 return ERR_PTR(-ENOMEM);
228 set_bit(idx, &adm->copp_bitmap[port_idx]);
230 c->afe_port = port_idx;
233 init_waitqueue_head(&c->wait);
238 static int q6adm_apr_send_copp_pkt(struct q6adm *adm, struct q6copp *copp,
239 struct apr_pkt *pkt, uint32_t rsp_opcode)
241 struct device *dev = adm->dev;
242 uint32_t opcode = pkt->hdr.opcode;
245 mutex_lock(&adm->lock);
246 copp->result.opcode = 0;
247 copp->result.status = 0;
248 ret = apr_send_pkt(adm->apr, pkt);
250 dev_err(dev, "Failed to send APR packet\n");
255 /* Wait for the callback with copp id */
257 ret = wait_event_timeout(copp->wait,
258 (copp->result.opcode == opcode) ||
259 (copp->result.opcode == rsp_opcode),
260 msecs_to_jiffies(TIMEOUT_MS));
262 ret = wait_event_timeout(copp->wait,
263 (copp->result.opcode == opcode),
264 msecs_to_jiffies(TIMEOUT_MS));
267 dev_err(dev, "ADM copp cmd timedout\n");
269 } else if (copp->result.status > 0) {
270 dev_err(dev, "DSP returned error[%d]\n",
271 copp->result.status);
276 mutex_unlock(&adm->lock);
280 static int q6adm_device_close(struct q6adm *adm, struct q6copp *copp,
281 int port_id, int copp_idx)
283 struct apr_pkt close;
285 close.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
286 APR_HDR_LEN(APR_HDR_SIZE),
288 close.hdr.pkt_size = sizeof(close);
289 close.hdr.src_port = port_id;
290 close.hdr.dest_port = copp->id;
291 close.hdr.token = port_id << 16 | copp_idx;
292 close.hdr.opcode = ADM_CMD_DEVICE_CLOSE_V5;
294 return q6adm_apr_send_copp_pkt(adm, copp, &close, 0);
297 static struct q6copp *q6adm_find_matching_copp(struct q6adm *adm,
298 int port_id, int topology,
300 int channel_mode, int bit_width,
303 struct q6copp *c = NULL;
304 struct q6copp *ret = NULL;
307 spin_lock_irqsave(&adm->copps_list_lock, flags);
309 list_for_each_entry(c, &adm->copps_list, node) {
310 if ((port_id == c->afe_port) && (topology == c->topology) &&
311 (mode == c->mode) && (rate == c->rate) &&
312 (bit_width == c->bit_width) && (app_type == c->app_type)) {
314 kref_get(&c->refcount);
317 spin_unlock_irqrestore(&adm->copps_list_lock, flags);
322 static int q6adm_device_open(struct q6adm *adm, struct q6copp *copp,
323 int port_id, int path, int topology,
324 int channel_mode, int bit_width, int rate)
326 struct q6adm_cmd_device_open_v5 *open;
327 int afe_port = q6afe_get_port_id(port_id);
332 pkt_size = APR_HDR_SIZE + sizeof(*open);
333 p = kzalloc(pkt_size, GFP_KERNEL);
338 open = p + APR_HDR_SIZE;
339 pkt->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
340 APR_HDR_LEN(APR_HDR_SIZE),
342 pkt->hdr.pkt_size = pkt_size;
343 pkt->hdr.src_port = afe_port;
344 pkt->hdr.dest_port = afe_port;
345 pkt->hdr.token = port_id << 16 | copp->copp_idx;
346 pkt->hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
347 open->flags = ADM_LEGACY_DEVICE_SESSION;
348 open->mode_of_operation = path;
349 open->endpoint_id_1 = afe_port;
350 open->topology_id = topology;
351 open->dev_num_channel = channel_mode & 0x00FF;
352 open->bit_width = bit_width;
353 open->sample_rate = rate;
355 ret = q6dsp_map_channels(&open->dev_channel_mapping[0],
360 ret = q6adm_apr_send_copp_pkt(adm, copp, pkt,
361 ADM_CMDRSP_DEVICE_OPEN_V5);
369 * q6adm_open() - open adm and grab a free copp
371 * @dev: Pointer to adm child device.
373 * @path: playback or capture path.
374 * @rate: rate at which copp is required.
375 * @channel_mode: channel mode
376 * @topology: adm topology id
377 * @perf_mode: performace mode.
378 * @bit_width: audio sample bit width
379 * @app_type: Application type.
382 * Return: Will be an negative on error or a valid copp pointer on success.
384 struct q6copp *q6adm_open(struct device *dev, int port_id, int path, int rate,
385 int channel_mode, int topology, int perf_mode,
386 uint16_t bit_width, int app_type, int acdb_id)
388 struct q6adm *adm = dev_get_drvdata(dev->parent);
394 dev_err(dev, "Invalid port_id 0x%x\n", port_id);
395 return ERR_PTR(-EINVAL);
398 copp = q6adm_find_matching_copp(adm, port_id, topology, perf_mode,
399 rate, channel_mode, bit_width, app_type);
401 dev_err(dev, "Found Matching Copp 0x%x\n", copp->copp_idx);
405 spin_lock_irqsave(&adm->copps_list_lock, flags);
406 copp = q6adm_alloc_copp(adm, port_id);
407 if (IS_ERR_OR_NULL(copp)) {
408 spin_unlock_irqrestore(&adm->copps_list_lock, flags);
409 return ERR_CAST(copp);
412 list_add_tail(&copp->node, &adm->copps_list);
413 spin_unlock_irqrestore(&adm->copps_list_lock, flags);
415 kref_init(&copp->refcount);
416 copp->topology = topology;
417 copp->mode = perf_mode;
419 copp->channels = channel_mode;
420 copp->bit_width = bit_width;
421 copp->app_type = app_type;
424 ret = q6adm_device_open(adm, copp, port_id, path, topology,
425 channel_mode, bit_width, rate);
427 kref_put(&copp->refcount, q6adm_free_copp);
433 EXPORT_SYMBOL_GPL(q6adm_open);
436 * q6adm_get_copp_id() - get copp index
438 * @copp: Pointer to valid copp
440 * Return: Will be an negative on error or a valid copp index on success.
442 int q6adm_get_copp_id(struct q6copp *copp)
447 return copp->copp_idx;
449 EXPORT_SYMBOL_GPL(q6adm_get_copp_id);
452 * q6adm_matrix_map() - Map asm streams and afe ports using payload
454 * @dev: Pointer to adm child device.
455 * @path: playback or capture path.
456 * @payload_map: map between session id and afe ports.
457 * @perf_mode: Performace mode.
459 * Return: Will be an negative on error or a zero on success.
461 int q6adm_matrix_map(struct device *dev, int path,
462 struct route_payload payload_map, int perf_mode)
464 struct q6adm *adm = dev_get_drvdata(dev->parent);
465 struct q6adm_cmd_matrix_map_routings_v5 *route;
466 struct q6adm_session_map_node_v5 *node;
468 uint16_t *copps_list;
469 int pkt_size, ret, i, copp_idx;
470 void *matrix_map = NULL;
473 /* Assumes port_ids have already been validated during adm_open */
474 pkt_size = (APR_HDR_SIZE + sizeof(*route) + sizeof(*node) +
475 (sizeof(uint32_t) * payload_map.num_copps));
477 matrix_map = kzalloc(pkt_size, GFP_KERNEL);
482 route = matrix_map + APR_HDR_SIZE;
483 node = matrix_map + APR_HDR_SIZE + sizeof(*route);
484 copps_list = matrix_map + APR_HDR_SIZE + sizeof(*route) + sizeof(*node);
486 pkt->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
487 APR_HDR_LEN(APR_HDR_SIZE),
489 pkt->hdr.pkt_size = pkt_size;
491 pkt->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
492 route->num_sessions = 1;
495 case ADM_PATH_PLAYBACK:
496 route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
498 case ADM_PATH_LIVE_REC:
499 route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
502 dev_err(dev, "Wrong path set[%d]\n", path);
506 node->session_id = payload_map.session_id;
507 node->num_copps = payload_map.num_copps;
509 for (i = 0; i < payload_map.num_copps; i++) {
510 int port_idx = payload_map.port_id[i];
513 dev_err(dev, "Invalid port_id 0x%x\n",
514 payload_map.port_id[i]);
518 copp_idx = payload_map.copp_idx[i];
520 copp = q6adm_find_copp(adm, port_idx, copp_idx);
526 copps_list[i] = copp->id;
527 kref_put(&copp->refcount, q6adm_free_copp);
530 mutex_lock(&adm->lock);
531 adm->result.status = 0;
532 adm->result.opcode = 0;
534 ret = apr_send_pkt(adm->apr, pkt);
536 dev_err(dev, "routing for stream %d failed ret %d\n",
537 payload_map.session_id, ret);
540 ret = wait_event_timeout(adm->matrix_map_wait,
541 adm->result.opcode == pkt->hdr.opcode,
542 msecs_to_jiffies(TIMEOUT_MS));
544 dev_err(dev, "routing for stream %d failed\n",
545 payload_map.session_id);
548 } else if (adm->result.status > 0) {
549 dev_err(dev, "DSP returned error[%d]\n",
556 mutex_unlock(&adm->lock);
560 EXPORT_SYMBOL_GPL(q6adm_matrix_map);
563 * q6adm_close() - Close adm copp
565 * @dev: Pointer to adm child device.
566 * @copp: pointer to previously opened copp
568 * Return: Will be an negative on error or a zero on success.
570 int q6adm_close(struct device *dev, struct q6copp *copp)
572 struct q6adm *adm = dev_get_drvdata(dev->parent);
575 ret = q6adm_device_close(adm, copp, copp->afe_port, copp->copp_idx);
577 dev_err(adm->dev, "Failed to close copp %d\n", ret);
581 kref_put(&copp->refcount, q6adm_free_copp);
585 EXPORT_SYMBOL_GPL(q6adm_close);
587 static int q6adm_probe(struct apr_device *adev)
589 struct device *dev = &adev->dev;
592 adm = devm_kzalloc(&adev->dev, sizeof(*adm), GFP_KERNEL);
597 dev_set_drvdata(&adev->dev, adm);
599 q6core_get_svc_api_info(adev->svc_id, &adm->ainfo);
600 mutex_init(&adm->lock);
601 init_waitqueue_head(&adm->matrix_map_wait);
603 INIT_LIST_HEAD(&adm->copps_list);
604 spin_lock_init(&adm->copps_list_lock);
606 return of_platform_populate(dev->of_node, NULL, NULL, dev);
609 static int q6adm_remove(struct apr_device *adev)
611 of_platform_depopulate(&adev->dev);
616 static const struct of_device_id q6adm_device_id[] = {
617 { .compatible = "qcom,q6adm" },
620 MODULE_DEVICE_TABLE(of, q6adm_device_id);
622 static struct apr_driver qcom_q6adm_driver = {
623 .probe = q6adm_probe,
624 .remove = q6adm_remove,
625 .callback = q6adm_callback,
627 .name = "qcom-q6adm",
628 .of_match_table = of_match_ptr(q6adm_device_id),
632 module_apr_driver(qcom_q6adm_driver);
633 MODULE_DESCRIPTION("Q6 Audio Device Manager");
634 MODULE_LICENSE("GPL v2");