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