Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/wait.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/jiffies.h> |
| 17 | #include <linux/uaccess.h> |
| 18 | #include <linux/atomic.h> |
| 19 | #include <linux/bitops.h> |
| 20 | #include <mach/qdsp6v2/rtac.h> |
| 21 | #include <mach/qdsp6v2/audio_acdb.h> |
| 22 | #include <sound/apr_audio.h> |
| 23 | #include <sound/q6afe.h> |
| 24 | #include <mach/qdsp6v2/audio_dev_ctl.h> |
| 25 | |
| 26 | #define TIMEOUT_MS 1000 |
| 27 | #define AUDIO_RX 0x0 |
| 28 | #define AUDIO_TX 0x1 |
| 29 | #define ASM_MAX_SESSION 0x8 /* To do: define in a header */ |
| 30 | #define RESET_COPP_ID 99 |
| 31 | #define INVALID_COPP_ID 0xFF |
| 32 | |
| 33 | struct adm_ctl { |
| 34 | void *apr; |
| 35 | atomic_t copp_id[AFE_MAX_PORTS]; |
| 36 | atomic_t copp_cnt[AFE_MAX_PORTS]; |
| 37 | atomic_t copp_stat[AFE_MAX_PORTS]; |
| 38 | unsigned long sessions[AFE_MAX_PORTS]; |
| 39 | wait_queue_head_t wait; |
| 40 | }; |
| 41 | |
| 42 | static struct adm_ctl this_adm; |
| 43 | |
| 44 | static int32_t adm_callback(struct apr_client_data *data, void *priv) |
| 45 | { |
| 46 | uint32_t *payload; |
| 47 | int i, index; |
| 48 | payload = data->payload; |
| 49 | |
| 50 | if (data->opcode == RESET_EVENTS) { |
| 51 | pr_debug("adm_callback: Reset event is received: %d %d apr[%p]\n", |
| 52 | data->reset_event, data->reset_proc, |
| 53 | this_adm.apr); |
| 54 | if (this_adm.apr) { |
| 55 | apr_reset(this_adm.apr); |
| 56 | for (i = 0; i < AFE_MAX_PORTS; i++) { |
| 57 | atomic_set(&this_adm.copp_id[i], |
| 58 | RESET_COPP_ID); |
| 59 | atomic_set(&this_adm.copp_cnt[i], 0); |
| 60 | atomic_set(&this_adm.copp_stat[i], 0); |
| 61 | } |
| 62 | this_adm.apr = NULL; |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | pr_debug("%s: code = 0x%x %x %x size = %d\n", __func__, |
| 68 | data->opcode, payload[0], payload[1], |
| 69 | data->payload_size); |
| 70 | |
| 71 | if (data->payload_size) { |
| 72 | index = afe_get_port_index(data->token); |
| 73 | pr_debug("%s: Port ID %d, index %d\n", __func__, |
| 74 | data->token, index); |
| 75 | |
| 76 | if (data->opcode == APR_BASIC_RSP_RESULT) { |
| 77 | pr_debug("APR_BASIC_RSP_RESULT\n"); |
| 78 | switch (payload[0]) { |
| 79 | case ADM_CMD_SET_PARAMS: |
| 80 | #ifdef CONFIG_MSM8X60_RTAC |
| 81 | if (rtac_make_adm_callback(payload, |
| 82 | data->payload_size)) |
| 83 | break; |
| 84 | #endif |
| 85 | case ADM_CMD_COPP_CLOSE: |
| 86 | case ADM_CMD_MEMORY_MAP: |
| 87 | case ADM_CMD_MEMORY_UNMAP: |
| 88 | case ADM_CMD_MEMORY_MAP_REGIONS: |
| 89 | case ADM_CMD_MEMORY_UNMAP_REGIONS: |
| 90 | case ADM_CMD_MATRIX_MAP_ROUTINGS: |
| 91 | pr_debug("ADM_CMD_MATRIX_MAP_ROUTINGS\n"); |
| 92 | atomic_set(&this_adm.copp_stat[index], 1); |
| 93 | wake_up(&this_adm.wait); |
| 94 | break; |
| 95 | default: |
| 96 | pr_err("%s: Unknown Cmd: 0x%x\n", __func__, |
| 97 | payload[0]); |
| 98 | break; |
| 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | switch (data->opcode) { |
| 104 | case ADM_CMDRSP_COPP_OPEN: { |
| 105 | struct adm_copp_open_respond *open = data->payload; |
| 106 | if (open->copp_id == INVALID_COPP_ID) { |
| 107 | pr_err("%s: invalid coppid rxed %d\n", |
| 108 | __func__, open->copp_id); |
| 109 | atomic_set(&this_adm.copp_stat[index], 1); |
| 110 | wake_up(&this_adm.wait); |
| 111 | break; |
| 112 | } |
| 113 | atomic_set(&this_adm.copp_id[index], open->copp_id); |
| 114 | atomic_set(&this_adm.copp_stat[index], 1); |
| 115 | pr_debug("%s: coppid rxed=%d\n", __func__, |
| 116 | open->copp_id); |
| 117 | wake_up(&this_adm.wait); |
| 118 | } |
| 119 | break; |
| 120 | #ifdef CONFIG_MSM8X60_RTAC |
| 121 | case ADM_CMDRSP_GET_PARAMS: |
Swaminathan Sathappan | 88163a7 | 2011-08-01 16:01:14 -0700 | [diff] [blame^] | 122 | pr_debug("%s: ADM_CMDRSP_GET_PARAMS\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 123 | rtac_make_adm_callback(payload, |
| 124 | data->payload_size); |
| 125 | break; |
| 126 | #endif |
| 127 | default: |
| 128 | pr_err("%s: Unknown cmd:0x%x\n", __func__, |
| 129 | data->opcode); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | void send_cal(int port_id, struct acdb_cal_block *aud_cal) |
| 137 | { |
| 138 | s32 result; |
| 139 | struct adm_set_params_command adm_params; |
| 140 | int index = afe_get_port_index(port_id); |
| 141 | |
| 142 | pr_debug("%s: Port id %d, index %d\n", __func__, port_id, index); |
| 143 | |
| 144 | if (!aud_cal || aud_cal->cal_size == 0) { |
| 145 | pr_err("%s: No calibration data to send!\n", __func__); |
| 146 | goto done; |
| 147 | } |
| 148 | |
| 149 | adm_params.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 150 | APR_HDR_LEN(20), APR_PKT_VER); |
| 151 | adm_params.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE, |
| 152 | sizeof(adm_params)); |
| 153 | adm_params.hdr.src_svc = APR_SVC_ADM; |
| 154 | adm_params.hdr.src_domain = APR_DOMAIN_APPS; |
| 155 | adm_params.hdr.src_port = port_id; |
| 156 | adm_params.hdr.dest_svc = APR_SVC_ADM; |
| 157 | adm_params.hdr.dest_domain = APR_DOMAIN_ADSP; |
| 158 | adm_params.hdr.dest_port = atomic_read(&this_adm.copp_id[index]); |
| 159 | adm_params.hdr.token = port_id; |
| 160 | adm_params.hdr.opcode = ADM_CMD_SET_PARAMS; |
| 161 | adm_params.payload = aud_cal->cal_paddr; |
| 162 | adm_params.payload_size = aud_cal->cal_size; |
| 163 | |
| 164 | atomic_set(&this_adm.copp_stat[index], 0); |
| 165 | pr_debug("%s: Sending SET_PARAMS payload = 0x%x, size = %d\n", |
| 166 | __func__, adm_params.payload, adm_params.payload_size); |
| 167 | result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_params); |
| 168 | if (result < 0) { |
| 169 | pr_err("%s: Set params failed port = %d payload = 0x%x\n", |
| 170 | __func__, port_id, aud_cal->cal_paddr); |
| 171 | goto done; |
| 172 | } |
| 173 | /* Wait for the callback */ |
| 174 | result = wait_event_timeout(this_adm.wait, |
| 175 | atomic_read(&this_adm.copp_stat[index]), |
| 176 | msecs_to_jiffies(TIMEOUT_MS)); |
| 177 | if (!result) |
| 178 | pr_err("%s: Set params timed out port = %d, payload = 0x%x\n", |
| 179 | __func__, port_id, aud_cal->cal_paddr); |
| 180 | done: |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | void send_adm_cal(int port_id, int path) |
| 185 | { |
| 186 | s32 acdb_path; |
| 187 | struct acdb_cal_block aud_cal; |
| 188 | |
| 189 | pr_debug("%s\n", __func__); |
| 190 | |
| 191 | /* Maps audio_dev_ctrl path definition to ACDB definition */ |
| 192 | acdb_path = path - 1; |
| 193 | if ((acdb_path >= NUM_AUDPROC_BUFFERS) || |
| 194 | (acdb_path < 0)) { |
| 195 | pr_err("%s: Path is not RX or TX, path = %d\n", |
| 196 | __func__, path); |
| 197 | goto done; |
| 198 | } |
| 199 | |
| 200 | pr_debug("%s: Sending audproc cal\n", __func__); |
| 201 | get_audproc_cal(acdb_path, &aud_cal); |
| 202 | send_cal(port_id, &aud_cal); |
| 203 | |
| 204 | pr_debug("%s: Sending audvol cal\n", __func__); |
| 205 | get_audvol_cal(acdb_path, &aud_cal); |
| 206 | send_cal(port_id, &aud_cal); |
| 207 | done: |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | /* This function issues routing command of ASM stream |
| 212 | * to ADM mixer associated with a particular AFE port |
| 213 | */ |
| 214 | int adm_cmd_map(int port_id, int session_id) |
| 215 | { |
| 216 | struct adm_routings_command route; |
| 217 | int ret = 0; |
| 218 | int index = afe_get_port_index(port_id); |
| 219 | |
| 220 | pr_debug("%s: port %x session %x\n", __func__, port_id, session_id); |
| 221 | |
| 222 | if (!atomic_read(&this_adm.copp_cnt[index])) |
| 223 | return 0; |
| 224 | |
| 225 | route.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 226 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 227 | route.hdr.pkt_size = sizeof(route); |
| 228 | route.hdr.src_svc = 0; |
| 229 | route.hdr.src_domain = APR_DOMAIN_APPS; |
| 230 | route.hdr.src_port = port_id; |
| 231 | route.hdr.dest_svc = APR_SVC_ADM; |
| 232 | route.hdr.dest_domain = APR_DOMAIN_ADSP; |
| 233 | route.hdr.dest_port = atomic_read(&this_adm.copp_id[index]); |
| 234 | route.hdr.token = port_id; |
| 235 | route.hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS; |
| 236 | route.num_sessions = 1; |
| 237 | route.session[0].id = session_id; |
| 238 | route.session[0].num_copps = 1; |
| 239 | route.session[0].copp_id[0] = |
| 240 | atomic_read(&this_adm.copp_id[index]); |
| 241 | |
| 242 | /* This rule can change */ |
| 243 | if ((port_id & 0x1)) |
| 244 | route.path = AUDIO_TX; |
| 245 | else |
| 246 | route.path = AUDIO_RX; |
| 247 | |
| 248 | atomic_set(&this_adm.copp_stat[index], 0); |
| 249 | |
| 250 | ret = apr_send_pkt(this_adm.apr, (uint32_t *)&route); |
| 251 | if (ret < 0) { |
| 252 | pr_err("%s: ADM routing for port %d failed\n", |
| 253 | __func__, port_id); |
| 254 | ret = -EINVAL; |
| 255 | goto fail_cmd; |
| 256 | } |
| 257 | ret = wait_event_timeout(this_adm.wait, |
| 258 | atomic_read(&this_adm.copp_stat[index]), |
| 259 | msecs_to_jiffies(TIMEOUT_MS)); |
| 260 | if (!ret) { |
| 261 | pr_err("%s: ADM cmd Route failed for port %d\n", |
| 262 | __func__, port_id); |
| 263 | ret = -EINVAL; |
| 264 | } |
| 265 | |
| 266 | fail_cmd: |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | /* This function establish routing of ASM stream to a particular |
| 271 | * ADM mixer that is routed to a particular hardware port |
| 272 | * session id must be in range of 0 ~ 31. |
| 273 | */ |
| 274 | int adm_route_session(int port_id, uint session_id, int set) |
| 275 | { |
| 276 | int rc = 0; |
| 277 | int index; |
| 278 | |
| 279 | pr_debug("%s: port %x session %x set %x\n", __func__, |
| 280 | port_id, session_id, set); |
| 281 | |
| 282 | index = afe_get_port_index(port_id); |
| 283 | |
| 284 | if (index >= AFE_MAX_PORTS) { |
| 285 | pr_err("%s port idi[%d] out of limit[%d]\n", __func__, |
| 286 | port_id, AFE_MAX_PORTS); |
| 287 | return -ENODEV; |
| 288 | } |
| 289 | |
| 290 | if (set) { |
| 291 | set_bit(session_id, &this_adm.sessions[index]); |
| 292 | rc = adm_cmd_map(port_id, session_id); /* not thread safe */ |
| 293 | } else /* Not sure how to deroute yet */ |
| 294 | clear_bit(session_id, &this_adm.sessions[index]); |
| 295 | |
| 296 | return rc; |
| 297 | } |
| 298 | |
| 299 | /* This function instantiates a mixer in QDSP6 audio path for |
| 300 | * given audio hardware port. Topology should be made part |
| 301 | * of audio calibration |
| 302 | */ |
| 303 | int adm_open_mixer(int port_id, int path, int rate, |
| 304 | int channel_mode, int topology) { |
| 305 | struct adm_copp_open_command open; |
| 306 | int ret = 0; |
| 307 | u32 i; |
| 308 | int index; |
| 309 | |
| 310 | pr_debug("%s: port %d path:%d rate:%d mode:%d\n", __func__, |
| 311 | port_id, path, rate, channel_mode); |
| 312 | |
| 313 | if (afe_validate_port(port_id) < 0) { |
| 314 | pr_err("%s port idi[%d] is invalid\n", __func__, port_id); |
| 315 | return -ENODEV; |
| 316 | } |
| 317 | |
| 318 | index = afe_get_port_index(port_id); |
| 319 | if (this_adm.apr == NULL) { |
| 320 | this_adm.apr = apr_register("ADSP", "ADM", adm_callback, |
| 321 | 0xFFFFFFFF, &this_adm); |
| 322 | if (this_adm.apr == NULL) { |
| 323 | pr_err("%s: Unable to register ADM\n", __func__); |
| 324 | ret = -ENODEV; |
| 325 | return ret; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (atomic_read(&this_adm.copp_cnt[index]) == 0) { |
| 330 | |
| 331 | open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 332 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 333 | open.hdr.pkt_size = sizeof(open); |
| 334 | open.hdr.src_svc = APR_SVC_ADM; |
| 335 | open.hdr.src_domain = APR_DOMAIN_APPS; |
| 336 | open.hdr.src_port = port_id; |
| 337 | open.hdr.dest_svc = APR_SVC_ADM; |
| 338 | open.hdr.dest_domain = APR_DOMAIN_ADSP; |
| 339 | open.hdr.dest_port = port_id; |
| 340 | open.hdr.token = port_id; |
| 341 | open.hdr.opcode = ADM_CMD_COPP_OPEN; |
| 342 | |
| 343 | open.mode = path; |
| 344 | open.endpoint_id1 = port_id; |
| 345 | open.endpoint_id2 = 0xFFFF; |
| 346 | |
Ben Romberger | c49b85d | 2011-07-15 18:55:34 -0700 | [diff] [blame] | 347 | /* convert path to acdb path */ |
| 348 | if (path == PLAYBACK) |
| 349 | open.topology_id = get_adm_rx_topology(); |
Jay Wang | 4fa2ee4 | 2011-07-18 00:21:22 -0700 | [diff] [blame] | 350 | else { |
Ben Romberger | c49b85d | 2011-07-15 18:55:34 -0700 | [diff] [blame] | 351 | open.topology_id = get_adm_tx_topology(); |
Jay Wang | 4fa2ee4 | 2011-07-18 00:21:22 -0700 | [diff] [blame] | 352 | if ((open.topology_id == |
| 353 | VPM_TX_SM_ECNS_COPP_TOPOLOGY) || |
| 354 | (open.topology_id == |
| 355 | VPM_TX_DM_FLUENCE_COPP_TOPOLOGY)) |
| 356 | rate = 16000; |
| 357 | } |
Ben Romberger | c49b85d | 2011-07-15 18:55:34 -0700 | [diff] [blame] | 358 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 359 | if (open.topology_id == 0) |
| 360 | open.topology_id = topology; |
| 361 | |
| 362 | open.channel_config = channel_mode & 0x00FF; |
| 363 | open.rate = rate; |
| 364 | |
| 365 | pr_debug("%s: channel_config=%d port_id=%d rate=%d\ |
| 366 | topology_id=0x%X\n", __func__, open.channel_config,\ |
| 367 | open.endpoint_id1, open.rate,\ |
| 368 | open.topology_id); |
| 369 | |
| 370 | atomic_set(&this_adm.copp_stat[index], 0); |
| 371 | |
| 372 | ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open); |
| 373 | if (ret < 0) { |
| 374 | pr_err("%s:ADM enable for port %d failed\n", |
| 375 | __func__, port_id); |
| 376 | ret = -EINVAL; |
| 377 | goto fail_cmd; |
| 378 | } |
| 379 | /* Wait for the callback with copp id */ |
| 380 | ret = wait_event_timeout(this_adm.wait, |
| 381 | atomic_read(&this_adm.copp_stat[index]), |
| 382 | msecs_to_jiffies(TIMEOUT_MS)); |
| 383 | if (!ret) { |
| 384 | pr_err("%s ADM open failed for port %d\n", __func__, |
| 385 | port_id); |
| 386 | ret = -EINVAL; |
| 387 | goto fail_cmd; |
| 388 | } |
| 389 | } |
| 390 | atomic_inc(&this_adm.copp_cnt[index]); |
| 391 | |
| 392 | /* Set up routing for cached session */ |
| 393 | for (i = find_first_bit(&this_adm.sessions[index], ASM_MAX_SESSION); |
| 394 | i < ASM_MAX_SESSION; i = find_next_bit(&this_adm.sessions[index], |
| 395 | ASM_MAX_SESSION, i + 1)) |
| 396 | adm_cmd_map(port_id, i); /* Not thread safe */ |
| 397 | |
| 398 | fail_cmd: |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | int adm_open(int port_id, int path, int rate, int channel_mode, int topology) |
| 403 | { |
| 404 | struct adm_copp_open_command open; |
| 405 | int ret = 0; |
| 406 | int index; |
| 407 | |
| 408 | pr_debug("%s: port %d path:%d rate:%d mode:%d\n", __func__, |
| 409 | port_id, path, rate, channel_mode); |
| 410 | |
| 411 | if (afe_validate_port(port_id) < 0) { |
| 412 | pr_err("%s port idi[%d] is invalid\n", __func__, port_id); |
| 413 | return -ENODEV; |
| 414 | } |
| 415 | |
| 416 | index = afe_get_port_index(port_id); |
| 417 | pr_debug("%s: Port ID %d, index %d\n", __func__, port_id, index); |
| 418 | |
| 419 | if (this_adm.apr == NULL) { |
| 420 | this_adm.apr = apr_register("ADSP", "ADM", adm_callback, |
| 421 | 0xFFFFFFFF, &this_adm); |
| 422 | if (this_adm.apr == NULL) { |
| 423 | pr_err("%s: Unable to register ADM\n", __func__); |
| 424 | ret = -ENODEV; |
| 425 | return ret; |
| 426 | } |
| 427 | #ifdef CONFIG_MSM8X60_RTAC |
| 428 | rtac_set_adm_handle(this_adm.apr); |
| 429 | #endif |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /* Create a COPP if port id are not enabled */ |
| 434 | if (atomic_read(&this_adm.copp_cnt[index]) == 0) { |
| 435 | |
| 436 | open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 437 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 438 | open.hdr.pkt_size = sizeof(open); |
| 439 | open.hdr.src_svc = APR_SVC_ADM; |
| 440 | open.hdr.src_domain = APR_DOMAIN_APPS; |
| 441 | open.hdr.src_port = port_id; |
| 442 | open.hdr.dest_svc = APR_SVC_ADM; |
| 443 | open.hdr.dest_domain = APR_DOMAIN_ADSP; |
| 444 | open.hdr.dest_port = port_id; |
| 445 | open.hdr.token = port_id; |
| 446 | open.hdr.opcode = ADM_CMD_COPP_OPEN; |
| 447 | |
| 448 | open.mode = path; |
| 449 | open.endpoint_id1 = port_id; |
| 450 | open.endpoint_id2 = 0xFFFF; |
| 451 | |
Ben Romberger | c49b85d | 2011-07-15 18:55:34 -0700 | [diff] [blame] | 452 | /* convert path to acdb path */ |
| 453 | if (path == PLAYBACK) |
| 454 | open.topology_id = get_adm_rx_topology(); |
Jay Wang | 4fa2ee4 | 2011-07-18 00:21:22 -0700 | [diff] [blame] | 455 | else { |
Ben Romberger | c49b85d | 2011-07-15 18:55:34 -0700 | [diff] [blame] | 456 | open.topology_id = get_adm_tx_topology(); |
Jay Wang | 4fa2ee4 | 2011-07-18 00:21:22 -0700 | [diff] [blame] | 457 | if ((open.topology_id == |
| 458 | VPM_TX_SM_ECNS_COPP_TOPOLOGY) || |
| 459 | (open.topology_id == |
| 460 | VPM_TX_DM_FLUENCE_COPP_TOPOLOGY)) |
| 461 | rate = 16000; |
| 462 | } |
Ben Romberger | c49b85d | 2011-07-15 18:55:34 -0700 | [diff] [blame] | 463 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 464 | if (open.topology_id == 0) |
| 465 | open.topology_id = topology; |
| 466 | |
| 467 | open.channel_config = channel_mode & 0x00FF; |
| 468 | open.rate = rate; |
| 469 | |
| 470 | pr_debug("%s: channel_config=%d port_id=%d rate=%d\ |
| 471 | topology_id=0x%X\n", __func__, open.channel_config,\ |
| 472 | open.endpoint_id1, open.rate,\ |
| 473 | open.topology_id); |
| 474 | |
| 475 | atomic_set(&this_adm.copp_stat[index], 0); |
| 476 | |
| 477 | ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open); |
| 478 | if (ret < 0) { |
| 479 | pr_err("%s:ADM enable for port %d failed\n", |
| 480 | __func__, port_id); |
| 481 | ret = -EINVAL; |
| 482 | goto fail_cmd; |
| 483 | } |
| 484 | /* Wait for the callback with copp id */ |
| 485 | ret = wait_event_timeout(this_adm.wait, |
| 486 | atomic_read(&this_adm.copp_stat[index]), |
| 487 | msecs_to_jiffies(TIMEOUT_MS)); |
| 488 | if (!ret) { |
| 489 | pr_err("%s ADM open failed for port %d\n", __func__, |
| 490 | port_id); |
| 491 | ret = -EINVAL; |
| 492 | goto fail_cmd; |
| 493 | } |
| 494 | } |
| 495 | atomic_inc(&this_adm.copp_cnt[index]); |
| 496 | return 0; |
| 497 | |
| 498 | fail_cmd: |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | |
| 503 | int adm_matrix_map(int session_id, int path, int num_copps, |
| 504 | unsigned int *port_id, int copp_id) |
| 505 | { |
| 506 | struct adm_routings_command route; |
| 507 | int ret = 0, i = 0; |
| 508 | /* Assumes port_ids have already been validated during adm_open */ |
| 509 | int index = afe_get_port_index(copp_id); |
| 510 | |
| 511 | pr_debug("%s: session 0x%x path:%d num_copps:%d port_id[0]:%d\n", |
| 512 | __func__, session_id, path, num_copps, port_id[0]); |
| 513 | |
| 514 | route.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 515 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 516 | route.hdr.pkt_size = sizeof(route); |
| 517 | route.hdr.src_svc = 0; |
| 518 | route.hdr.src_domain = APR_DOMAIN_APPS; |
| 519 | route.hdr.src_port = copp_id; |
| 520 | route.hdr.dest_svc = APR_SVC_ADM; |
| 521 | route.hdr.dest_domain = APR_DOMAIN_ADSP; |
| 522 | route.hdr.dest_port = atomic_read(&this_adm.copp_id[index]); |
| 523 | route.hdr.token = copp_id; |
| 524 | route.hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS; |
| 525 | route.num_sessions = 1; |
| 526 | route.session[0].id = session_id; |
| 527 | route.session[0].num_copps = num_copps; |
| 528 | |
| 529 | for (i = 0; i < num_copps; i++) { |
| 530 | int tmp; |
| 531 | tmp = afe_get_port_index(port_id[i]); |
| 532 | |
| 533 | pr_debug("%s: port_id[%d]: %d, index: %d\n", __func__, i, |
| 534 | port_id[i], tmp); |
| 535 | |
| 536 | route.session[0].copp_id[i] = |
| 537 | atomic_read(&this_adm.copp_id[tmp]); |
| 538 | } |
| 539 | if (num_copps % 2) |
| 540 | route.session[0].copp_id[i] = 0; |
| 541 | |
| 542 | switch (path) { |
| 543 | case 0x1: |
| 544 | route.path = AUDIO_RX; |
| 545 | break; |
| 546 | case 0x2: |
| 547 | case 0x3: |
| 548 | route.path = AUDIO_TX; |
| 549 | break; |
| 550 | default: |
| 551 | pr_err("%s: Wrong path set[%d]\n", __func__, path); |
| 552 | break; |
| 553 | } |
| 554 | atomic_set(&this_adm.copp_stat[index], 0); |
| 555 | |
| 556 | ret = apr_send_pkt(this_adm.apr, (uint32_t *)&route); |
| 557 | if (ret < 0) { |
| 558 | pr_err("%s: ADM routing for port %d failed\n", |
| 559 | __func__, port_id[0]); |
| 560 | ret = -EINVAL; |
| 561 | goto fail_cmd; |
| 562 | } |
| 563 | ret = wait_event_timeout(this_adm.wait, |
| 564 | atomic_read(&this_adm.copp_stat[index]), |
| 565 | msecs_to_jiffies(TIMEOUT_MS)); |
| 566 | if (!ret) { |
| 567 | pr_err("%s: ADM cmd Route failed for port %d\n", |
| 568 | __func__, port_id[0]); |
| 569 | ret = -EINVAL; |
| 570 | goto fail_cmd; |
| 571 | } |
| 572 | |
| 573 | for (i = 0; i < num_copps; i++) |
| 574 | send_adm_cal(port_id[i], path); |
| 575 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 576 | return 0; |
| 577 | |
| 578 | fail_cmd: |
| 579 | |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | int adm_memory_map_regions(uint32_t *buf_add, uint32_t mempool_id, |
| 584 | uint32_t *bufsz, uint32_t bufcnt) |
| 585 | { |
| 586 | struct adm_cmd_memory_map_regions *mmap_regions = NULL; |
| 587 | struct adm_memory_map_regions *mregions = NULL; |
| 588 | void *mmap_region_cmd = NULL; |
| 589 | void *payload = NULL; |
| 590 | int ret = 0; |
| 591 | int i = 0; |
| 592 | int cmd_size = 0; |
| 593 | |
| 594 | pr_info("%s\n", __func__); |
| 595 | if (this_adm.apr == NULL) { |
| 596 | this_adm.apr = apr_register("ADSP", "ADM", adm_callback, |
| 597 | 0xFFFFFFFF, &this_adm); |
| 598 | if (this_adm.apr == NULL) { |
| 599 | pr_err("%s: Unable to register ADM\n", __func__); |
| 600 | ret = -ENODEV; |
| 601 | return ret; |
| 602 | } |
| 603 | #ifdef CONFIG_MSM8X60_RTAC |
| 604 | rtac_set_adm_handle(this_adm.apr); |
| 605 | #endif |
| 606 | } |
| 607 | |
| 608 | cmd_size = sizeof(struct adm_cmd_memory_map_regions) |
| 609 | + sizeof(struct adm_memory_map_regions) * bufcnt; |
| 610 | |
| 611 | mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL); |
| 612 | if (!mmap_region_cmd) { |
| 613 | pr_err("%s: allocate mmap_region_cmd failed\n", __func__); |
| 614 | return -ENOMEM; |
| 615 | } |
| 616 | mmap_regions = (struct adm_cmd_memory_map_regions *)mmap_region_cmd; |
| 617 | mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 618 | APR_HDR_LEN(APR_HDR_SIZE), |
| 619 | APR_PKT_VER); |
| 620 | mmap_regions->hdr.pkt_size = cmd_size; |
| 621 | mmap_regions->hdr.src_port = 0; |
| 622 | mmap_regions->hdr.dest_port = 0; |
| 623 | mmap_regions->hdr.token = 0; |
| 624 | mmap_regions->hdr.opcode = ADM_CMD_MEMORY_MAP_REGIONS; |
| 625 | mmap_regions->mempool_id = mempool_id & 0x00ff; |
| 626 | mmap_regions->nregions = bufcnt & 0x00ff; |
| 627 | pr_debug("%s: map_regions->nregions = %d\n", __func__, |
| 628 | mmap_regions->nregions); |
| 629 | payload = ((u8 *) mmap_region_cmd + |
| 630 | sizeof(struct adm_cmd_memory_map_regions)); |
| 631 | mregions = (struct adm_memory_map_regions *)payload; |
| 632 | |
| 633 | for (i = 0; i < bufcnt; i++) { |
| 634 | mregions->phys = buf_add[i]; |
| 635 | mregions->buf_size = bufsz[i]; |
| 636 | ++mregions; |
| 637 | } |
| 638 | |
| 639 | atomic_set(&this_adm.copp_stat[0], 0); |
| 640 | ret = apr_send_pkt(this_adm.apr, (uint32_t *) mmap_region_cmd); |
| 641 | if (ret < 0) { |
| 642 | pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__, |
| 643 | mmap_regions->hdr.opcode, ret); |
| 644 | ret = -EINVAL; |
| 645 | goto fail_cmd; |
| 646 | } |
| 647 | |
| 648 | ret = wait_event_timeout(this_adm.wait, |
| 649 | atomic_read(&this_adm.copp_stat[0]), 5 * HZ); |
| 650 | if (!ret) { |
| 651 | pr_err("%s: timeout. waited for memory_map\n", __func__); |
| 652 | ret = -EINVAL; |
| 653 | goto fail_cmd; |
| 654 | } |
| 655 | fail_cmd: |
| 656 | kfree(mmap_region_cmd); |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | int adm_memory_unmap_regions(uint32_t *buf_add, uint32_t *bufsz, |
| 661 | uint32_t bufcnt) |
| 662 | { |
| 663 | struct adm_cmd_memory_unmap_regions *unmap_regions = NULL; |
| 664 | struct adm_memory_unmap_regions *mregions = NULL; |
| 665 | void *unmap_region_cmd = NULL; |
| 666 | void *payload = NULL; |
| 667 | int ret = 0; |
| 668 | int i = 0; |
| 669 | int cmd_size = 0; |
| 670 | |
| 671 | pr_info("%s\n", __func__); |
| 672 | |
| 673 | if (this_adm.apr == NULL) { |
| 674 | pr_err("%s APR handle NULL\n", __func__); |
| 675 | return -EINVAL; |
| 676 | } |
| 677 | |
| 678 | cmd_size = sizeof(struct adm_cmd_memory_unmap_regions) |
| 679 | + sizeof(struct adm_memory_unmap_regions) * bufcnt; |
| 680 | |
| 681 | unmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL); |
| 682 | if (!unmap_region_cmd) { |
| 683 | pr_err("%s: allocate unmap_region_cmd failed\n", __func__); |
| 684 | return -ENOMEM; |
| 685 | } |
| 686 | unmap_regions = (struct adm_cmd_memory_unmap_regions *) |
| 687 | unmap_region_cmd; |
| 688 | unmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 689 | APR_HDR_LEN(APR_HDR_SIZE), |
| 690 | APR_PKT_VER); |
| 691 | unmap_regions->hdr.pkt_size = cmd_size; |
| 692 | unmap_regions->hdr.src_port = 0; |
| 693 | unmap_regions->hdr.dest_port = 0; |
| 694 | unmap_regions->hdr.token = 0; |
| 695 | unmap_regions->hdr.opcode = ADM_CMD_MEMORY_UNMAP_REGIONS; |
| 696 | unmap_regions->nregions = bufcnt & 0x00ff; |
| 697 | unmap_regions->reserved = 0; |
| 698 | pr_debug("%s: unmap_regions->nregions = %d\n", __func__, |
| 699 | unmap_regions->nregions); |
| 700 | payload = ((u8 *) unmap_region_cmd + |
| 701 | sizeof(struct adm_cmd_memory_unmap_regions)); |
| 702 | mregions = (struct adm_memory_unmap_regions *)payload; |
| 703 | |
| 704 | for (i = 0; i < bufcnt; i++) { |
| 705 | mregions->phys = buf_add[i]; |
| 706 | ++mregions; |
| 707 | } |
| 708 | atomic_set(&this_adm.copp_stat[0], 0); |
| 709 | ret = apr_send_pkt(this_adm.apr, (uint32_t *) unmap_region_cmd); |
| 710 | if (ret < 0) { |
| 711 | pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__, |
| 712 | unmap_regions->hdr.opcode, ret); |
| 713 | ret = -EINVAL; |
| 714 | goto fail_cmd; |
| 715 | } |
| 716 | |
| 717 | ret = wait_event_timeout(this_adm.wait, |
| 718 | atomic_read(&this_adm.copp_stat[0]), 5 * HZ); |
| 719 | if (!ret) { |
| 720 | pr_err("%s: timeout. waited for memory_unmap\n", __func__); |
| 721 | ret = -EINVAL; |
| 722 | goto fail_cmd; |
| 723 | } |
| 724 | fail_cmd: |
| 725 | kfree(unmap_region_cmd); |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | #ifdef CONFIG_MSM8X60_RTAC |
| 730 | int adm_get_copp_id(int port_id) |
| 731 | { |
| 732 | pr_debug("%s\n", __func__); |
| 733 | |
| 734 | if (port_id < 0) { |
| 735 | pr_err("%s: invalid port_id = %d\n", __func__, port_id); |
| 736 | return -EINVAL; |
| 737 | } |
| 738 | |
| 739 | return atomic_read(&this_adm.copp_id[port_id]); |
| 740 | } |
| 741 | #endif |
| 742 | |
| 743 | int adm_close(int port_id) |
| 744 | { |
| 745 | struct apr_hdr close; |
| 746 | |
| 747 | int ret = 0; |
| 748 | int index = afe_get_port_index(port_id); |
Bharath Ramachandramurthy | 51a8621 | 2011-07-29 12:43:43 -0700 | [diff] [blame] | 749 | if (afe_validate_port(port_id) < 0) |
| 750 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 751 | |
| 752 | pr_info("%s port_id=%d index %d\n", __func__, port_id, index); |
| 753 | |
| 754 | if (!(atomic_read(&this_adm.copp_cnt[index]))) { |
| 755 | pr_err("%s: copp count for port[%d]is 0\n", __func__, port_id); |
| 756 | |
| 757 | goto fail_cmd; |
| 758 | } |
| 759 | atomic_dec(&this_adm.copp_cnt[index]); |
| 760 | if (!(atomic_read(&this_adm.copp_cnt[index]))) { |
| 761 | |
| 762 | close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 763 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 764 | close.pkt_size = sizeof(close); |
| 765 | close.src_svc = APR_SVC_ADM; |
| 766 | close.src_domain = APR_DOMAIN_APPS; |
| 767 | close.src_port = port_id; |
| 768 | close.dest_svc = APR_SVC_ADM; |
| 769 | close.dest_domain = APR_DOMAIN_ADSP; |
| 770 | close.dest_port = atomic_read(&this_adm.copp_id[index]); |
| 771 | close.token = port_id; |
| 772 | close.opcode = ADM_CMD_COPP_CLOSE; |
| 773 | |
| 774 | atomic_set(&this_adm.copp_id[index], RESET_COPP_ID); |
| 775 | atomic_set(&this_adm.copp_stat[index], 0); |
| 776 | |
| 777 | |
| 778 | pr_debug("%s:coppid %d portid=%d index=%d coppcnt=%d\n", |
| 779 | __func__, |
| 780 | atomic_read(&this_adm.copp_id[index]), |
| 781 | port_id, index, |
| 782 | atomic_read(&this_adm.copp_cnt[index])); |
| 783 | |
| 784 | ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close); |
| 785 | if (ret < 0) { |
| 786 | pr_err("%s ADM close failed\n", __func__); |
| 787 | ret = -EINVAL; |
| 788 | goto fail_cmd; |
| 789 | } |
| 790 | |
| 791 | ret = wait_event_timeout(this_adm.wait, |
| 792 | atomic_read(&this_adm.copp_stat[index]), |
| 793 | msecs_to_jiffies(TIMEOUT_MS)); |
| 794 | if (!ret) { |
| 795 | pr_err("%s: ADM cmd Route failed for port %d\n", |
| 796 | __func__, port_id); |
| 797 | ret = -EINVAL; |
| 798 | goto fail_cmd; |
| 799 | } |
| 800 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | fail_cmd: |
| 804 | return ret; |
| 805 | } |
| 806 | |
| 807 | static int __init adm_init(void) |
| 808 | { |
| 809 | int i = 0; |
| 810 | init_waitqueue_head(&this_adm.wait); |
| 811 | this_adm.apr = NULL; |
| 812 | |
| 813 | for (i = 0; i < AFE_MAX_PORTS; i++) { |
| 814 | atomic_set(&this_adm.copp_id[i], RESET_COPP_ID); |
| 815 | atomic_set(&this_adm.copp_cnt[i], 0); |
| 816 | atomic_set(&this_adm.copp_stat[i], 0); |
| 817 | } |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | device_initcall(adm_init); |