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