Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Common code to deal with the AUDPREPROC dsp task (audio preprocessing) |
| 3 | * |
Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 4 | * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 5 | * |
| 6 | * Based on the audpp layer in arch/arm/mach-msm/qdsp5/audpp.c |
| 7 | * |
| 8 | * Copyright (C) 2008 Google, Inc. |
| 9 | * |
| 10 | * This software is licensed under the terms of the GNU General Public |
| 11 | * License version 2, as published by the Free Software Foundation, and |
| 12 | * may be copied, distributed, and modified under those terms. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <mach/msm_adsp.h> |
| 23 | #include <mach/debug_mm.h> |
| 24 | #include <mach/qdsp5/qdsp5audpreproc.h> |
| 25 | #include <mach/qdsp5/qdsp5audreccmdi.h> |
Manish Dewangan | ca85972 | 2012-07-09 18:21:42 +0530 | [diff] [blame] | 26 | #include <mach/qdsp5v2/audio_acdbi.h> |
| 27 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 28 | |
| 29 | static DEFINE_MUTEX(audpreproc_lock); |
| 30 | |
| 31 | struct msm_adspenc_info { |
| 32 | const char *module_name; |
| 33 | unsigned module_queueids; |
| 34 | int module_encid; /* streamid */ |
| 35 | int enc_formats; /* supported formats */ |
| 36 | int nr_codec_support; /* number of codec suported */ |
| 37 | }; |
| 38 | |
| 39 | #define ENC_MODULE_INFO(name, queueids, encid, formats, nr_codec) \ |
| 40 | {.module_name = name, .module_queueids = queueids, \ |
| 41 | .module_encid = encid, .enc_formats = formats, \ |
| 42 | .nr_codec_support = nr_codec } |
| 43 | |
| 44 | #ifdef CONFIG_MSM7X27A_AUDIO |
| 45 | #define ENC0_FORMAT ((1<<AUDREC_CMD_TYPE_1_INDEX_SBC)| \ |
| 46 | (1<<AUDREC_CMD_TYPE_0_INDEX_AAC)| \ |
| 47 | (1<<AUDREC_CMD_TYPE_0_INDEX_AMRNB)| \ |
| 48 | (1<<AUDREC_CMD_TYPE_0_INDEX_EVRC)| \ |
| 49 | (1<<AUDREC_CMD_TYPE_0_INDEX_QCELP)) |
| 50 | |
| 51 | #define ENC1_FORMAT (1<<AUDREC_CMD_TYPE_0_INDEX_WAV) |
| 52 | #else |
| 53 | #define ENC0_FORMAT ((1<<AUDREC_CMD_TYPE_0_INDEX_WAV)| \ |
| 54 | (1<<AUDREC_CMD_TYPE_1_INDEX_SBC)| \ |
| 55 | (1<<AUDREC_CMD_TYPE_0_INDEX_AAC)| \ |
| 56 | (1<<AUDREC_CMD_TYPE_0_INDEX_AMRNB)| \ |
| 57 | (1<<AUDREC_CMD_TYPE_0_INDEX_EVRC)| \ |
| 58 | (1<<AUDREC_CMD_TYPE_0_INDEX_QCELP)) |
| 59 | #endif |
| 60 | |
Manish Dewangan | 221ac64 | 2012-07-09 14:26:26 +0530 | [diff] [blame] | 61 | #define MAX_ENC_COUNT 2 |
| 62 | #define MAX_EVENT_CALLBACK_CLIENTS 2 |
| 63 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 64 | struct msm_adspenc_database { |
| 65 | unsigned num_enc; |
| 66 | struct msm_adspenc_info *enc_info_list; |
| 67 | }; |
| 68 | |
| 69 | #ifdef CONFIG_MSM7X27A_AUDIO |
| 70 | static struct msm_adspenc_info enc_info_list[] = { |
| 71 | ENC_MODULE_INFO("AUDRECTASK", \ |
| 72 | ((QDSP_uPAudRecBitStreamQueue << 16)| \ |
| 73 | QDSP_uPAudRecCmdQueue), 0, \ |
| 74 | (ENC0_FORMAT | (1 << MSM_ADSP_ENC_MODE_TUNNEL) | \ |
| 75 | (1 << MSM_ADSP_ENC_MODE_NON_TUNNEL)), 5), |
| 76 | |
| 77 | ENC_MODULE_INFO("AUDREC1TASK", \ |
| 78 | ((QDSP_uPAudRec1BitStreamQueue << 16)| \ |
| 79 | QDSP_uPAudRec1CmdQueue), 1, \ |
| 80 | (ENC1_FORMAT | (1 << MSM_ADSP_ENC_MODE_TUNNEL)), 1), |
| 81 | }; |
| 82 | #else |
| 83 | static struct msm_adspenc_info enc_info_list[] = { |
| 84 | ENC_MODULE_INFO("AUDRECTASK", |
| 85 | ((QDSP_uPAudRecBitStreamQueue << 16)| \ |
| 86 | QDSP_uPAudRecCmdQueue), 0, \ |
| 87 | (ENC0_FORMAT | (1 << MSM_ADSP_ENC_MODE_TUNNEL)), 6), |
| 88 | }; |
| 89 | #endif |
| 90 | |
| 91 | static struct msm_adspenc_database msm_enc_database = { |
| 92 | .num_enc = ARRAY_SIZE(enc_info_list), |
| 93 | .enc_info_list = enc_info_list, |
| 94 | }; |
| 95 | |
| 96 | struct audpreproc_state { |
| 97 | struct msm_adsp_module *mod; |
Manish Dewangan | 221ac64 | 2012-07-09 14:26:26 +0530 | [diff] [blame] | 98 | audpreproc_event_func func[MAX_ENC_COUNT]; |
| 99 | void *private[MAX_ENC_COUNT]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 100 | struct mutex *lock; |
| 101 | unsigned open_count; |
| 102 | unsigned enc_inuse; |
Manish Dewangan | 221ac64 | 2012-07-09 14:26:26 +0530 | [diff] [blame] | 103 | struct audpreproc_event_callback *cb_tbl[MAX_EVENT_CALLBACK_CLIENTS]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
Manish Dewangan | 221ac64 | 2012-07-09 14:26:26 +0530 | [diff] [blame] | 106 | static struct audrec_session_info session_info; |
| 107 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 108 | static struct audpreproc_state the_audpreproc_state = { |
| 109 | .lock = &audpreproc_lock, |
| 110 | }; |
| 111 | |
Manish Dewangan | 221ac64 | 2012-07-09 14:26:26 +0530 | [diff] [blame] | 112 | /* DSP preproc event handler */ |
| 113 | static void audpreproc_dsp_event(void *data, unsigned id, size_t len, |
| 114 | void (*getevent)(void *ptr, size_t len)) |
| 115 | { |
| 116 | struct audpreproc_state *audpreproc = data; |
| 117 | uint16_t msg[2]; |
| 118 | MM_ERR("audpreproc_dsp_event %id", id); |
| 119 | |
| 120 | getevent(msg, sizeof(msg)); |
| 121 | |
| 122 | switch (id) { |
| 123 | case AUDPREPROC_MSG_CMD_CFG_DONE_MSG: |
| 124 | MM_DBG("type %d, status_flag %d\n", msg[0], msg[1]); |
| 125 | if (audpreproc->func[0]) |
| 126 | audpreproc->func[0]( |
| 127 | audpreproc->private[0], id, |
| 128 | &msg); |
| 129 | break; |
| 130 | case AUDPREPROC_MSG_ERROR_MSG_ID: |
| 131 | MM_INFO("err_index %d\n", msg[0]); |
| 132 | if (audpreproc->func[0]) |
| 133 | audpreproc->func[0]( |
| 134 | audpreproc->private[0], id, |
| 135 | &msg); |
| 136 | break; |
| 137 | case ADSP_MESSAGE_ID: |
| 138 | MM_DBG("Received ADSP event: module enable(audpreproctask)\n"); |
| 139 | if (audpreproc->func[0]) |
| 140 | audpreproc->func[0]( |
| 141 | audpreproc->private[0], id, |
| 142 | &msg); |
| 143 | break; |
Manish Dewangan | ca85972 | 2012-07-09 18:21:42 +0530 | [diff] [blame] | 144 | case AUDPREPROC_MSG_FEAT_QUERY_DM_DONE: |
| 145 | { |
| 146 | uint16_t msg[3]; |
| 147 | getevent(msg, sizeof(msg)); |
| 148 | MM_INFO("RTC ACK --> %x %x %x\n", msg[0], msg[1], msg[2]); |
| 149 | acdb_rtc_set_err(msg[2]); |
| 150 | } |
| 151 | break; |
Manish Dewangan | 221ac64 | 2012-07-09 14:26:26 +0530 | [diff] [blame] | 152 | default: |
| 153 | MM_ERR("unknown event %d\n", id); |
| 154 | } |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | static struct msm_adsp_ops adsp_ops = { |
| 159 | .event = audpreproc_dsp_event, |
| 160 | }; |
| 161 | |
| 162 | /* EXPORTED API's */ |
| 163 | int audpreproc_enable(int enc_id, audpreproc_event_func func, void *private) |
| 164 | { |
| 165 | struct audpreproc_state *audpreproc = &the_audpreproc_state; |
| 166 | int res = 0; |
| 167 | uint16_t msg[2]; |
| 168 | int n = 0; |
| 169 | MM_DBG("audpreproc_enable %d\n", enc_id); |
| 170 | |
| 171 | if (enc_id < 0 || enc_id > (MAX_ENC_COUNT - 1)) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | mutex_lock(audpreproc->lock); |
| 175 | if (audpreproc->func[enc_id]) { |
| 176 | res = -EBUSY; |
| 177 | goto out; |
| 178 | } |
| 179 | |
| 180 | audpreproc->func[enc_id] = func; |
| 181 | audpreproc->private[enc_id] = private; |
| 182 | |
| 183 | /* First client to enable preproc task */ |
| 184 | if (audpreproc->open_count++ == 0) { |
| 185 | MM_DBG("Get AUDPREPROCTASK\n"); |
| 186 | res = msm_adsp_get("AUDPREPROCTASK", &audpreproc->mod, |
| 187 | &adsp_ops, audpreproc); |
| 188 | if (res < 0) { |
| 189 | MM_ERR("Can not get AUDPREPROCTASK\n"); |
| 190 | audpreproc->open_count = 0; |
| 191 | audpreproc->func[enc_id] = NULL; |
| 192 | audpreproc->private[enc_id] = NULL; |
| 193 | goto out; |
| 194 | } |
| 195 | if (msm_adsp_enable(audpreproc->mod)) { |
| 196 | audpreproc->open_count = 0; |
| 197 | audpreproc->func[enc_id] = NULL; |
| 198 | audpreproc->private[enc_id] = NULL; |
| 199 | msm_adsp_put(audpreproc->mod); |
| 200 | audpreproc->mod = NULL; |
| 201 | res = -ENODEV; |
| 202 | goto out; |
| 203 | } |
| 204 | } |
| 205 | msg[0] = AUDPREPROC_MSG_STATUS_FLAG_ENA; |
| 206 | /* Generate audpre enabled message for registered clients */ |
| 207 | for (n = 0; n < MAX_EVENT_CALLBACK_CLIENTS; ++n) { |
| 208 | if (audpreproc->cb_tbl[n] && |
| 209 | audpreproc->cb_tbl[n]->fn) { |
| 210 | audpreproc->cb_tbl[n]->fn( \ |
| 211 | audpreproc->cb_tbl[n]->private,\ |
| 212 | AUDPREPROC_MSG_CMD_CFG_DONE_MSG, |
| 213 | (void *) &msg); |
| 214 | } |
| 215 | } |
| 216 | res = 0; |
| 217 | out: |
| 218 | mutex_unlock(audpreproc->lock); |
| 219 | return res; |
| 220 | } |
| 221 | EXPORT_SYMBOL(audpreproc_enable); |
| 222 | |
| 223 | |
| 224 | void audpreproc_disable(int enc_id, void *private) |
| 225 | { |
| 226 | struct audpreproc_state *audpreproc = &the_audpreproc_state; |
| 227 | uint16_t msg[2]; |
| 228 | int n = 0; |
| 229 | |
| 230 | if (enc_id < 0 || enc_id > (MAX_ENC_COUNT - 1)) |
| 231 | return; |
| 232 | |
| 233 | mutex_lock(audpreproc->lock); |
| 234 | if (!audpreproc->func[enc_id]) |
| 235 | goto out; |
| 236 | if (audpreproc->private[enc_id] != private) |
| 237 | goto out; |
| 238 | |
| 239 | audpreproc->func[enc_id] = NULL; |
| 240 | audpreproc->private[enc_id] = NULL; |
| 241 | |
| 242 | /* Last client then disable preproc task */ |
| 243 | if (--audpreproc->open_count == 0) { |
| 244 | msm_adsp_disable(audpreproc->mod); |
| 245 | MM_DBG("Put AUDPREPROCTASK\n"); |
| 246 | msm_adsp_put(audpreproc->mod); |
| 247 | audpreproc->mod = NULL; |
| 248 | } |
| 249 | msg[0] = AUDPREPROC_MSG_STATUS_FLAG_DIS; |
| 250 | /* Generate audpre enabled message for registered clients */ |
| 251 | for (n = 0; n < MAX_EVENT_CALLBACK_CLIENTS; ++n) { |
| 252 | if (audpreproc->cb_tbl[n] && |
| 253 | audpreproc->cb_tbl[n]->fn) { |
| 254 | audpreproc->cb_tbl[n]->fn( \ |
| 255 | audpreproc->cb_tbl[n]->private,\ |
| 256 | AUDPREPROC_MSG_CMD_CFG_DONE_MSG, |
| 257 | (void *) &msg); |
| 258 | } |
| 259 | } |
| 260 | out: |
| 261 | mutex_unlock(audpreproc->lock); |
| 262 | return; |
| 263 | } |
| 264 | EXPORT_SYMBOL(audpreproc_disable); |
| 265 | |
| 266 | int audpreproc_update_audrec_info( |
| 267 | struct audrec_session_info *audrec_session_info) |
| 268 | { |
| 269 | if (!audrec_session_info) { |
| 270 | MM_ERR("error in audrec session info address\n"); |
| 271 | return -EINVAL; |
| 272 | } |
| 273 | if (audrec_session_info->session_id < MAX_ENC_COUNT) { |
| 274 | memcpy(&session_info, |
| 275 | audrec_session_info, |
| 276 | sizeof(struct audrec_session_info)); |
| 277 | return 0; |
| 278 | } |
| 279 | return -EINVAL; |
| 280 | } |
| 281 | EXPORT_SYMBOL(audpreproc_update_audrec_info); |
| 282 | |
| 283 | int get_audrec_session_info(struct audrec_session_info *info) |
| 284 | { |
| 285 | if (!info) { |
| 286 | MM_ERR("error in audrec session info address\n"); |
| 287 | return -EINVAL; |
| 288 | } |
| 289 | |
| 290 | if (the_audpreproc_state.open_count == 0) { |
| 291 | MM_ERR("No aud pre session active\n"); |
| 292 | return -EINVAL; |
| 293 | } |
| 294 | |
| 295 | memcpy(info, &session_info, sizeof(struct audrec_session_info)); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | EXPORT_SYMBOL(get_audrec_session_info); |
| 300 | |
| 301 | int audpreproc_register_event_callback(struct audpreproc_event_callback *ecb) |
| 302 | { |
| 303 | struct audpreproc_state *audpreproc = &the_audpreproc_state; |
| 304 | int i; |
| 305 | |
| 306 | for (i = 0; i < MAX_EVENT_CALLBACK_CLIENTS; ++i) { |
| 307 | if (NULL == audpreproc->cb_tbl[i]) { |
| 308 | audpreproc->cb_tbl[i] = ecb; |
| 309 | return 0; |
| 310 | } |
| 311 | } |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | EXPORT_SYMBOL(audpreproc_register_event_callback); |
| 315 | |
| 316 | int audpreproc_unregister_event_callback(struct audpreproc_event_callback *ecb) |
| 317 | { |
| 318 | struct audpreproc_state *audpreproc = &the_audpreproc_state; |
| 319 | int i; |
| 320 | |
| 321 | for (i = 0; i < MAX_EVENT_CALLBACK_CLIENTS; ++i) { |
| 322 | if (ecb == audpreproc->cb_tbl[i]) { |
| 323 | audpreproc->cb_tbl[i] = NULL; |
| 324 | return 0; |
| 325 | } |
| 326 | } |
| 327 | return -EINVAL; |
| 328 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 329 | /* enc_type = supported encode format * |
| 330 | * like pcm, aac, sbc, evrc, qcelp, amrnb etc ... * |
| 331 | */ |
| 332 | int audpreproc_aenc_alloc(unsigned enc_type, const char **module_name, |
| 333 | unsigned *queue_ids) |
| 334 | { |
| 335 | struct audpreproc_state *audpreproc = &the_audpreproc_state; |
| 336 | int encid = -1, idx, lidx, mode, codec; |
| 337 | int codecs_supported, min_codecs_supported; |
| 338 | |
| 339 | mutex_lock(audpreproc->lock); |
| 340 | /* Represents in bit mask */ |
| 341 | mode = ((enc_type & AUDPREPROC_MODE_MASK) << 16); |
| 342 | codec = (1 << (enc_type & AUDPREPROC_CODEC_MASK)); |
| 343 | |
| 344 | lidx = msm_enc_database.num_enc; |
| 345 | min_codecs_supported = sizeof(unsigned int) * 8; |
| 346 | MM_DBG("mode = 0x%08x codec = 0x%08x\n", mode, codec); |
| 347 | |
| 348 | for (idx = lidx-1; idx >= 0; idx--) { |
| 349 | /* encoder free and supports the format */ |
| 350 | if (!(audpreproc->enc_inuse & (1 << (idx))) && |
| 351 | ((mode & msm_enc_database.enc_info_list[idx].enc_formats) |
| 352 | == mode) && ((codec & |
| 353 | msm_enc_database.enc_info_list[idx].enc_formats) |
| 354 | == codec)){ |
| 355 | /* Check supports minimum number codecs */ |
| 356 | codecs_supported = |
| 357 | msm_enc_database.enc_info_list[idx].nr_codec_support; |
| 358 | if (codecs_supported < min_codecs_supported) { |
| 359 | lidx = idx; |
| 360 | min_codecs_supported = codecs_supported; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if (lidx < msm_enc_database.num_enc) { |
| 366 | audpreproc->enc_inuse |= (1 << lidx); |
| 367 | *module_name = |
| 368 | msm_enc_database.enc_info_list[lidx].module_name; |
| 369 | *queue_ids = |
| 370 | msm_enc_database.enc_info_list[lidx].module_queueids; |
| 371 | encid = msm_enc_database.enc_info_list[lidx].module_encid; |
| 372 | } |
| 373 | |
| 374 | mutex_unlock(audpreproc->lock); |
| 375 | return encid; |
| 376 | } |
| 377 | EXPORT_SYMBOL(audpreproc_aenc_alloc); |
| 378 | |
| 379 | void audpreproc_aenc_free(int enc_id) |
| 380 | { |
| 381 | struct audpreproc_state *audpreproc = &the_audpreproc_state; |
| 382 | int idx; |
| 383 | |
| 384 | mutex_lock(audpreproc->lock); |
| 385 | for (idx = 0; idx < msm_enc_database.num_enc; idx++) { |
| 386 | if (msm_enc_database.enc_info_list[idx].module_encid == |
| 387 | enc_id) { |
| 388 | audpreproc->enc_inuse &= ~(1 << idx); |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | mutex_unlock(audpreproc->lock); |
| 393 | return; |
| 394 | |
| 395 | } |
| 396 | EXPORT_SYMBOL(audpreproc_aenc_free); |
Manish Dewangan | 221ac64 | 2012-07-09 14:26:26 +0530 | [diff] [blame] | 397 | |
| 398 | int audpreproc_dsp_set_agc( |
| 399 | audpreproc_cmd_cfg_agc_params *agc_cfg, |
| 400 | unsigned len) |
| 401 | { |
| 402 | return msm_adsp_write(the_audpreproc_state.mod, |
| 403 | QDSP_uPAudPreProcCmdQueue, agc_cfg, len); |
| 404 | } |
| 405 | EXPORT_SYMBOL(audpreproc_dsp_set_agc); |
| 406 | |
| 407 | int audpreproc_dsp_set_ns( |
| 408 | audpreproc_cmd_cfg_ns_params *ns_cfg, |
| 409 | unsigned len) |
| 410 | { |
| 411 | return msm_adsp_write(the_audpreproc_state.mod, |
| 412 | QDSP_uPAudPreProcCmdQueue, ns_cfg, len); |
| 413 | } |
| 414 | EXPORT_SYMBOL(audpreproc_dsp_set_ns); |
| 415 | |
| 416 | int audpreproc_dsp_set_iir( |
| 417 | audpreproc_cmd_cfg_iir_tuning_filter_params *iir_cfg, |
| 418 | unsigned len) |
| 419 | { |
| 420 | return msm_adsp_write(the_audpreproc_state.mod, |
| 421 | QDSP_uPAudPreProcCmdQueue, iir_cfg, len); |
| 422 | } |
| 423 | EXPORT_SYMBOL(audpreproc_dsp_set_iir); |
| 424 | |
| 425 | int audpreproc_send_preproccmdqueue(void *cmd, unsigned len) |
| 426 | { |
| 427 | return msm_adsp_write(the_audpreproc_state.mod, |
| 428 | QDSP_uPAudPreProcCmdQueue, cmd, len); |
| 429 | } |
| 430 | EXPORT_SYMBOL(audpreproc_send_preproccmdqueue); |