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/debugfs.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/kthread.h> |
| 16 | #include <linux/uaccess.h> |
| 17 | #include <linux/wait.h> |
| 18 | #include <linux/jiffies.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <sound/apr_audio.h> |
| 21 | #include <sound/q6afe.h> |
| 22 | |
| 23 | struct afe_ctl { |
| 24 | void *apr; |
| 25 | atomic_t state; |
| 26 | atomic_t status; |
| 27 | wait_queue_head_t wait; |
| 28 | struct task_struct *task; |
| 29 | }; |
| 30 | |
| 31 | static struct afe_ctl this_afe; |
| 32 | |
| 33 | #define TIMEOUT_MS 1000 |
| 34 | #define Q6AFE_MAX_VOLUME 0x3FFF |
| 35 | |
| 36 | #define SIZEOF_CFG_CMD(y) \ |
| 37 | (sizeof(struct apr_hdr) + sizeof(u16) + (sizeof(struct y))) |
| 38 | |
| 39 | static int32_t afe_callback(struct apr_client_data *data, void *priv) |
| 40 | { |
| 41 | if (data->opcode == RESET_EVENTS) { |
| 42 | pr_debug("q6afe: reset event = %d %d apr[%p]\n", |
| 43 | data->reset_event, data->reset_proc, this_afe.apr); |
| 44 | if (this_afe.apr) { |
| 45 | apr_reset(this_afe.apr); |
| 46 | atomic_set(&this_afe.state, 0); |
| 47 | this_afe.apr = NULL; |
| 48 | } |
| 49 | /* send info to user */ |
| 50 | pr_debug("task_name = %s pid = %d\n", |
| 51 | this_afe.task->comm, this_afe.task->pid); |
| 52 | send_sig(SIGUSR1, this_afe.task, 0); |
| 53 | } |
| 54 | if (data->payload_size) { |
| 55 | uint32_t *payload; |
| 56 | payload = data->payload; |
| 57 | pr_debug("%s: cmd = 0x%x status = 0x%x\n", __func__, |
| 58 | payload[0], payload[1]); |
| 59 | /* payload[1] contains the error status for response */ |
| 60 | if (payload[1] != 0) { |
| 61 | atomic_set(&this_afe.status, -1); |
| 62 | pr_err("%s: cmd = 0x%x returned error = 0x%x\n", |
| 63 | __func__, payload[0], payload[1]); |
| 64 | } |
| 65 | if (data->opcode == APR_BASIC_RSP_RESULT) { |
| 66 | switch (payload[0]) { |
| 67 | case AFE_PORT_AUDIO_IF_CONFIG: |
| 68 | case AFE_PORT_CMD_STOP: |
| 69 | case AFE_PORT_CMD_START: |
| 70 | case AFE_PORT_CMD_LOOPBACK: |
| 71 | case AFE_PORT_CMD_SIDETONE_CTL: |
| 72 | case AFE_PORT_CMD_SET_PARAM: |
| 73 | case AFE_PSEUDOPORT_CMD_START: |
| 74 | case AFE_PSEUDOPORT_CMD_STOP: |
| 75 | atomic_set(&this_afe.state, 0); |
| 76 | wake_up(&this_afe.wait); |
| 77 | break; |
| 78 | default: |
| 79 | pr_err("Unknown cmd 0x%x\n", |
| 80 | payload[0]); |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int afe_validate_port(u16 port_id) |
| 89 | { |
| 90 | int ret; |
| 91 | |
| 92 | switch (port_id) { |
| 93 | case PRIMARY_I2S_RX: |
| 94 | case PRIMARY_I2S_TX: |
| 95 | case PCM_RX: |
| 96 | case PCM_TX: |
| 97 | case SECONDARY_I2S_RX: |
| 98 | case SECONDARY_I2S_TX: |
| 99 | case MI2S_RX: |
| 100 | case MI2S_TX: |
| 101 | case HDMI_RX: |
| 102 | case RSVD_2: |
| 103 | case RSVD_3: |
| 104 | case DIGI_MIC_TX: |
| 105 | case VOICE_RECORD_RX: |
| 106 | case VOICE_RECORD_TX: |
| 107 | case VOICE_PLAYBACK_TX: |
| 108 | case SLIMBUS_0_RX: |
| 109 | case SLIMBUS_0_TX: |
| 110 | case INT_BT_SCO_RX: |
| 111 | case INT_BT_SCO_TX: |
| 112 | case INT_BT_A2DP_RX: |
| 113 | case INT_FM_RX: |
| 114 | case INT_FM_TX: |
| 115 | { |
| 116 | ret = 0; |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | default: |
| 121 | ret = -EINVAL; |
| 122 | } |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | int afe_get_port_index(u16 port_id) |
| 128 | { |
| 129 | switch (port_id) { |
| 130 | case PRIMARY_I2S_RX: return IDX_PRIMARY_I2S_RX; |
| 131 | case PRIMARY_I2S_TX: return IDX_PRIMARY_I2S_TX; |
| 132 | case PCM_RX: return IDX_PCM_RX; |
| 133 | case PCM_TX: return IDX_PCM_TX; |
| 134 | case SECONDARY_I2S_RX: return IDX_SECONDARY_I2S_RX; |
| 135 | case SECONDARY_I2S_TX: return IDX_SECONDARY_I2S_TX; |
| 136 | case MI2S_RX: return IDX_MI2S_RX; |
| 137 | case MI2S_TX: return IDX_MI2S_TX; |
| 138 | case HDMI_RX: return IDX_HDMI_RX; |
| 139 | case RSVD_2: return IDX_RSVD_2; |
| 140 | case RSVD_3: return IDX_RSVD_3; |
| 141 | case DIGI_MIC_TX: return IDX_DIGI_MIC_TX; |
| 142 | case VOICE_RECORD_RX: return IDX_VOICE_RECORD_RX; |
| 143 | case VOICE_RECORD_TX: return IDX_VOICE_RECORD_TX; |
| 144 | case VOICE_PLAYBACK_TX: return IDX_VOICE_PLAYBACK_TX; |
| 145 | case SLIMBUS_0_RX: return IDX_SLIMBUS_0_RX; |
| 146 | case SLIMBUS_0_TX: return IDX_SLIMBUS_0_TX; |
| 147 | case INT_BT_SCO_RX: return IDX_INT_BT_SCO_RX; |
| 148 | case INT_BT_SCO_TX: return IDX_INT_BT_SCO_TX; |
| 149 | case INT_BT_A2DP_RX: return IDX_INT_BT_A2DP_RX; |
| 150 | case INT_FM_RX: return IDX_INT_FM_RX; |
| 151 | case INT_FM_TX: return IDX_INT_FM_TX; |
| 152 | |
| 153 | default: return -EINVAL; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | int afe_sizeof_cfg_cmd(u16 port_id) |
| 158 | { |
| 159 | int ret_size; |
| 160 | switch (port_id) { |
| 161 | case PRIMARY_I2S_RX: |
| 162 | case PRIMARY_I2S_TX: |
| 163 | case SECONDARY_I2S_RX: |
| 164 | case SECONDARY_I2S_TX: |
| 165 | case MI2S_RX: |
| 166 | case MI2S_TX: |
| 167 | ret_size = SIZEOF_CFG_CMD(afe_port_mi2s_cfg); |
| 168 | break; |
| 169 | case HDMI_RX: |
| 170 | ret_size = SIZEOF_CFG_CMD(afe_port_hdmi_cfg); |
| 171 | break; |
| 172 | case SLIMBUS_0_RX: |
| 173 | case SLIMBUS_0_TX: |
| 174 | ret_size = SIZEOF_CFG_CMD(afe_port_slimbus_cfg); |
| 175 | break; |
| 176 | case PCM_RX: |
| 177 | case PCM_TX: |
| 178 | default: |
| 179 | ret_size = SIZEOF_CFG_CMD(afe_port_pcm_cfg); |
| 180 | break; |
| 181 | } |
| 182 | return ret_size; |
| 183 | } |
| 184 | |
| 185 | int afe_port_start_nowait(u16 port_id, union afe_port_config *afe_config, |
| 186 | u32 rate) /* This function is no blocking */ |
| 187 | { |
| 188 | struct afe_port_start_command start; |
| 189 | struct afe_audioif_config_command config; |
| 190 | int ret; |
| 191 | |
| 192 | if (!afe_config) { |
| 193 | pr_err("%s: Error, no configuration data\n", __func__); |
| 194 | ret = -EINVAL; |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | pr_info("%s: %d %d\n", __func__, port_id, rate); |
| 199 | |
| 200 | if (this_afe.apr == NULL) { |
| 201 | this_afe.apr = apr_register("ADSP", "AFE", afe_callback, |
| 202 | 0xFFFFFFFF, &this_afe); |
| 203 | pr_info("%s: Register AFE\n", __func__); |
| 204 | if (this_afe.apr == NULL) { |
| 205 | pr_err("%s: Unable to register AFE\n", __func__); |
| 206 | ret = -ENODEV; |
| 207 | return ret; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 212 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 213 | config.hdr.pkt_size = afe_sizeof_cfg_cmd(port_id); |
| 214 | config.hdr.src_port = 0; |
| 215 | config.hdr.dest_port = 0; |
| 216 | config.hdr.token = 0; |
| 217 | config.hdr.opcode = AFE_PORT_AUDIO_IF_CONFIG; |
| 218 | |
| 219 | if (afe_validate_port(port_id) < 0) { |
| 220 | |
| 221 | pr_err("%s: Failed : Invalid Port id = %d\n", __func__, |
| 222 | port_id); |
| 223 | ret = -EINVAL; |
| 224 | goto fail_cmd; |
| 225 | } |
| 226 | |
| 227 | config.port_id = port_id; |
| 228 | config.port = *afe_config; |
| 229 | |
| 230 | ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config); |
| 231 | if (ret < 0) { |
| 232 | pr_err("%s: AFE enable for port %d failed\n", __func__, |
| 233 | port_id); |
| 234 | ret = -EINVAL; |
| 235 | goto fail_cmd; |
| 236 | } |
| 237 | |
| 238 | start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 239 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 240 | start.hdr.pkt_size = sizeof(start); |
| 241 | start.hdr.src_port = 0; |
| 242 | start.hdr.dest_port = 0; |
| 243 | start.hdr.token = 0; |
| 244 | start.hdr.opcode = AFE_PORT_CMD_START; |
| 245 | start.port_id = port_id; |
| 246 | start.gain = 0x2000; |
| 247 | start.sample_rate = rate; |
| 248 | |
| 249 | ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start); |
| 250 | |
| 251 | if (IS_ERR_VALUE(ret)) { |
| 252 | pr_err("%s: AFE enable for port %d failed\n", __func__, |
| 253 | port_id); |
| 254 | ret = -EINVAL; |
| 255 | goto fail_cmd; |
| 256 | } |
| 257 | |
| 258 | if (this_afe.task != current) |
| 259 | this_afe.task = current; |
| 260 | |
| 261 | pr_debug("task_name = %s pid = %d\n", |
| 262 | this_afe.task->comm, this_afe.task->pid); |
| 263 | return 0; |
| 264 | |
| 265 | fail_cmd: |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | int afe_open(u16 port_id, union afe_port_config *afe_config, int rate) |
| 270 | { |
| 271 | struct afe_port_start_command start; |
| 272 | struct afe_audioif_config_command config; |
| 273 | int ret = 0; |
| 274 | |
| 275 | if (!afe_config) { |
| 276 | pr_err("%s: Error, no configuration data\n", __func__); |
| 277 | ret = -EINVAL; |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | pr_info("%s: %d %d\n", __func__, port_id, rate); |
| 282 | |
| 283 | if (this_afe.apr == NULL) { |
| 284 | this_afe.apr = apr_register("ADSP", "AFE", afe_callback, |
| 285 | 0xFFFFFFFF, &this_afe); |
| 286 | pr_info("%s: Register AFE\n", __func__); |
| 287 | if (this_afe.apr == NULL) { |
| 288 | pr_err("%s: Unable to register AFE\n", __func__); |
| 289 | ret = -ENODEV; |
| 290 | return ret; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 295 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 296 | config.hdr.pkt_size = afe_sizeof_cfg_cmd(port_id); |
| 297 | config.hdr.src_port = 0; |
| 298 | config.hdr.dest_port = 0; |
| 299 | config.hdr.token = 0; |
| 300 | config.hdr.opcode = AFE_PORT_AUDIO_IF_CONFIG; |
| 301 | |
| 302 | if (afe_validate_port(port_id) < 0) { |
| 303 | |
| 304 | pr_err("%s: Failed : Invalid Port id = %d\n", __func__, |
| 305 | port_id); |
| 306 | ret = -EINVAL; |
| 307 | goto fail_cmd; |
| 308 | } |
| 309 | |
| 310 | config.port_id = port_id; |
| 311 | config.port = *afe_config; |
| 312 | |
| 313 | atomic_set(&this_afe.state, 1); |
| 314 | atomic_set(&this_afe.status, 0); |
| 315 | ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config); |
| 316 | if (ret < 0) { |
| 317 | pr_err("%s: AFE enable for port %d failed\n", __func__, |
| 318 | port_id); |
| 319 | ret = -EINVAL; |
| 320 | goto fail_cmd; |
| 321 | } |
| 322 | |
| 323 | ret = wait_event_timeout(this_afe.wait, |
| 324 | (atomic_read(&this_afe.state) == 0), |
| 325 | msecs_to_jiffies(TIMEOUT_MS)); |
| 326 | if (!ret) { |
| 327 | pr_err("%s: wait_event timeout\n", __func__); |
| 328 | ret = -EINVAL; |
| 329 | goto fail_cmd; |
| 330 | } |
| 331 | if (atomic_read(&this_afe.status) != 0) { |
| 332 | pr_err("%s: config cmd failed\n", __func__); |
| 333 | ret = -EINVAL; |
| 334 | goto fail_cmd; |
| 335 | } |
| 336 | start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 337 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 338 | start.hdr.pkt_size = sizeof(start); |
| 339 | start.hdr.src_port = 0; |
| 340 | start.hdr.dest_port = 0; |
| 341 | start.hdr.token = 0; |
| 342 | start.hdr.opcode = AFE_PORT_CMD_START; |
| 343 | start.port_id = port_id; |
| 344 | start.gain = 0x2000; |
| 345 | start.sample_rate = rate; |
| 346 | |
| 347 | atomic_set(&this_afe.state, 1); |
| 348 | ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start); |
| 349 | if (ret < 0) { |
| 350 | pr_err("%s: AFE enable for port %d failed\n", __func__, |
| 351 | port_id); |
| 352 | ret = -EINVAL; |
| 353 | goto fail_cmd; |
| 354 | } |
| 355 | ret = wait_event_timeout(this_afe.wait, |
| 356 | (atomic_read(&this_afe.state) == 0), |
| 357 | msecs_to_jiffies(TIMEOUT_MS)); |
| 358 | if (!ret) { |
| 359 | pr_err("%s: wait_event timeout\n", __func__); |
| 360 | ret = -EINVAL; |
| 361 | goto fail_cmd; |
| 362 | } |
| 363 | |
| 364 | if (this_afe.task != current) |
| 365 | this_afe.task = current; |
| 366 | |
| 367 | pr_debug("task_name = %s pid = %d\n", |
| 368 | this_afe.task->comm, this_afe.task->pid); |
| 369 | return 0; |
| 370 | fail_cmd: |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | int afe_loopback(u16 enable, u16 rx_port, u16 tx_port) |
| 375 | { |
| 376 | struct afe_loopback_command lb_cmd; |
| 377 | int ret = 0; |
| 378 | if (this_afe.apr == NULL) { |
| 379 | this_afe.apr = apr_register("ADSP", "AFE", afe_callback, |
| 380 | 0xFFFFFFFF, &this_afe); |
| 381 | pr_info("%s: Register AFE\n", __func__); |
| 382 | if (this_afe.apr == NULL) { |
| 383 | pr_err("%s: Unable to register AFE\n", __func__); |
| 384 | ret = -ENODEV; |
| 385 | return ret; |
| 386 | } |
| 387 | } |
| 388 | lb_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 389 | APR_HDR_LEN(20), APR_PKT_VER); |
| 390 | lb_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE, |
| 391 | sizeof(lb_cmd) - APR_HDR_SIZE); |
| 392 | lb_cmd.hdr.src_port = 0; |
| 393 | lb_cmd.hdr.dest_port = 0; |
| 394 | lb_cmd.hdr.token = 0; |
| 395 | lb_cmd.hdr.opcode = AFE_PORT_CMD_LOOPBACK; |
| 396 | lb_cmd.tx_port_id = tx_port; |
| 397 | lb_cmd.rx_port_id = rx_port; |
| 398 | lb_cmd.mode = 0xFFFF; |
| 399 | lb_cmd.enable = (enable ? 1 : 0); |
| 400 | atomic_set(&this_afe.state, 1); |
| 401 | |
| 402 | ret = apr_send_pkt(this_afe.apr, (uint32_t *) &lb_cmd); |
| 403 | if (ret < 0) { |
| 404 | pr_err("%s: AFE loopback failed\n", __func__); |
| 405 | ret = -EINVAL; |
| 406 | goto done; |
| 407 | } |
| 408 | ret = wait_event_timeout(this_afe.wait, |
| 409 | (atomic_read(&this_afe.state) == 0), |
| 410 | msecs_to_jiffies(TIMEOUT_MS)); |
| 411 | if (!ret) { |
| 412 | pr_err("%s: wait_event timeout\n", __func__); |
| 413 | ret = -EINVAL; |
| 414 | } |
| 415 | done: |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | |
| 420 | int afe_loopback_gain(u16 port_id, u16 volume) |
| 421 | { |
| 422 | struct afe_port_cmd_set_param set_param; |
| 423 | int ret = 0; |
| 424 | |
| 425 | if (this_afe.apr == NULL) { |
| 426 | pr_err("%s: AFE is not opened\n", __func__); |
| 427 | ret = -EPERM; |
| 428 | goto fail_cmd; |
| 429 | } |
| 430 | |
| 431 | if (afe_validate_port(port_id) < 0) { |
| 432 | |
| 433 | pr_err("%s: Failed : Invalid Port id = %d\n", __func__, |
| 434 | port_id); |
| 435 | ret = -EINVAL; |
| 436 | goto fail_cmd; |
| 437 | } |
| 438 | |
| 439 | /* RX ports numbers are even .TX ports numbers are odd. */ |
| 440 | if (port_id % 2 == 0) { |
| 441 | pr_err("%s: Failed : afe loopback gain only for TX ports." |
| 442 | " port_id %d\n", __func__, port_id); |
| 443 | ret = -EINVAL; |
| 444 | goto fail_cmd; |
| 445 | } |
| 446 | |
| 447 | pr_debug("%s: %d %hX\n", __func__, port_id, volume); |
| 448 | |
| 449 | set_param.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 450 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 451 | set_param.hdr.pkt_size = sizeof(set_param); |
| 452 | set_param.hdr.src_port = 0; |
| 453 | set_param.hdr.dest_port = 0; |
| 454 | set_param.hdr.token = 0; |
| 455 | set_param.hdr.opcode = AFE_PORT_CMD_SET_PARAM; |
| 456 | |
| 457 | set_param.port_id = port_id; |
| 458 | set_param.payload_size = sizeof(struct afe_param_payload); |
| 459 | set_param.payload_address = 0; |
| 460 | |
| 461 | set_param.payload.module_id = AFE_MODULE_ID_PORT_INFO; |
| 462 | set_param.payload.param_id = AFE_PARAM_ID_LOOPBACK_GAIN; |
| 463 | set_param.payload.param_size = sizeof(struct afe_param_loopback_gain); |
| 464 | set_param.payload.reserved = 0; |
| 465 | |
| 466 | set_param.payload.param.loopback_gain.gain = volume; |
| 467 | set_param.payload.param.loopback_gain.reserved = 0; |
| 468 | |
| 469 | atomic_set(&this_afe.state, 1); |
| 470 | ret = apr_send_pkt(this_afe.apr, (uint32_t *) &set_param); |
| 471 | if (ret < 0) { |
| 472 | pr_err("%s: AFE param set failed for port %d\n", |
| 473 | __func__, port_id); |
| 474 | ret = -EINVAL; |
| 475 | goto fail_cmd; |
| 476 | } |
| 477 | |
| 478 | ret = wait_event_timeout(this_afe.wait, |
| 479 | (atomic_read(&this_afe.state) == 0), |
| 480 | msecs_to_jiffies(TIMEOUT_MS)); |
| 481 | if (ret < 0) { |
| 482 | pr_err("%s: wait_event timeout\n", __func__); |
| 483 | ret = -EINVAL; |
| 484 | goto fail_cmd; |
| 485 | } |
| 486 | return 0; |
| 487 | fail_cmd: |
| 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | int afe_start_pseudo_port(u16 port_id) |
| 492 | { |
| 493 | int ret = 0; |
| 494 | struct afe_pseudoport_start_command start; |
| 495 | |
| 496 | pr_info("%s: port_id=%d\n", __func__, port_id); |
| 497 | |
| 498 | if (this_afe.apr == NULL) { |
| 499 | this_afe.apr = apr_register("ADSP", "AFE", afe_callback, |
| 500 | 0xFFFFFFFF, &this_afe); |
| 501 | pr_info("%s: Register AFE\n", __func__); |
| 502 | if (this_afe.apr == NULL) { |
| 503 | pr_err("%s: Unable to register AFE\n", __func__); |
| 504 | ret = -ENODEV; |
| 505 | return ret; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 510 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 511 | start.hdr.pkt_size = sizeof(start); |
| 512 | start.hdr.src_port = 0; |
| 513 | start.hdr.dest_port = 0; |
| 514 | start.hdr.token = 0; |
| 515 | start.hdr.opcode = AFE_PSEUDOPORT_CMD_START; |
| 516 | start.port_id = port_id; |
| 517 | start.timing = 1; |
| 518 | |
| 519 | atomic_set(&this_afe.state, 1); |
| 520 | ret = apr_send_pkt(this_afe.apr, (uint32_t *) &start); |
| 521 | if (ret < 0) { |
| 522 | pr_err("%s: AFE enable for port %d failed %d\n", |
| 523 | __func__, port_id, ret); |
| 524 | ret = -EINVAL; |
| 525 | return ret; |
| 526 | } |
| 527 | |
| 528 | ret = wait_event_timeout(this_afe.wait, |
| 529 | (atomic_read(&this_afe.state) == 0), |
| 530 | msecs_to_jiffies(TIMEOUT_MS)); |
| 531 | if (!ret) { |
| 532 | pr_err("%s: wait_event timeout\n", __func__); |
| 533 | ret = -EINVAL; |
| 534 | return ret; |
| 535 | } |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | int afe_stop_pseudo_port(u16 port_id) |
| 541 | { |
| 542 | int ret = 0; |
| 543 | struct afe_pseudoport_stop_command stop; |
| 544 | |
| 545 | pr_info("%s: port_id=%d\n", __func__, port_id); |
| 546 | |
| 547 | if (this_afe.apr == NULL) { |
| 548 | pr_err("%s: AFE is already closed\n", __func__); |
| 549 | ret = -EINVAL; |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 554 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 555 | stop.hdr.pkt_size = sizeof(stop); |
| 556 | stop.hdr.src_port = 0; |
| 557 | stop.hdr.dest_port = 0; |
| 558 | stop.hdr.token = 0; |
| 559 | stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP; |
| 560 | stop.port_id = port_id; |
| 561 | stop.reserved = 0; |
| 562 | |
| 563 | atomic_set(&this_afe.state, 1); |
| 564 | ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop); |
| 565 | if (ret < 0) { |
| 566 | pr_err("%s: AFE close failed %d\n", __func__, ret); |
| 567 | ret = -EINVAL; |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | ret = wait_event_timeout(this_afe.wait, |
| 572 | (atomic_read(&this_afe.state) == 0), |
| 573 | msecs_to_jiffies(TIMEOUT_MS)); |
| 574 | if (!ret) { |
| 575 | pr_err("%s: wait_event timeout\n", __func__); |
| 576 | ret = -EINVAL; |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | #ifdef CONFIG_DEBUG_FS |
| 584 | static struct dentry *debugfs_afelb; |
| 585 | static struct dentry *debugfs_afelb_gain; |
| 586 | |
| 587 | static int afe_debug_open(struct inode *inode, struct file *file) |
| 588 | { |
| 589 | file->private_data = inode->i_private; |
| 590 | pr_info("debug intf %s\n", (char *) file->private_data); |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static int afe_get_parameters(char *buf, long int *param1, int num_of_par) |
| 595 | { |
| 596 | char *token; |
| 597 | int base, cnt; |
| 598 | |
| 599 | token = strsep(&buf, " "); |
| 600 | |
| 601 | for (cnt = 0; cnt < num_of_par; cnt++) { |
| 602 | if (token != NULL) { |
| 603 | if ((token[1] == 'x') || (token[1] == 'X')) |
| 604 | base = 16; |
| 605 | else |
| 606 | base = 10; |
| 607 | |
| 608 | if (strict_strtoul(token, base, ¶m1[cnt]) != 0) |
| 609 | return -EINVAL; |
| 610 | |
| 611 | token = strsep(&buf, " "); |
| 612 | } else |
| 613 | return -EINVAL; |
| 614 | } |
| 615 | return 0; |
| 616 | } |
| 617 | #define AFE_LOOPBACK_ON (1) |
| 618 | #define AFE_LOOPBACK_OFF (0) |
| 619 | static ssize_t afe_debug_write(struct file *filp, |
| 620 | const char __user *ubuf, size_t cnt, loff_t *ppos) |
| 621 | { |
| 622 | char *lb_str = filp->private_data; |
| 623 | char lbuf[32]; |
| 624 | int rc; |
| 625 | unsigned long param[5]; |
| 626 | |
| 627 | if (cnt > sizeof(lbuf) - 1) |
| 628 | return -EINVAL; |
| 629 | |
| 630 | rc = copy_from_user(lbuf, ubuf, cnt); |
| 631 | if (rc) |
| 632 | return -EFAULT; |
| 633 | |
| 634 | lbuf[cnt] = '\0'; |
| 635 | |
| 636 | if (!strcmp(lb_str, "afe_loopback")) { |
| 637 | rc = afe_get_parameters(lbuf, param, 3); |
| 638 | if (!rc) { |
| 639 | pr_info("%s %lu %lu %lu\n", lb_str, param[0], param[1], |
| 640 | param[2]); |
| 641 | |
| 642 | if ((param[0] != AFE_LOOPBACK_ON) && (param[0] != |
| 643 | AFE_LOOPBACK_OFF)) { |
| 644 | pr_err("%s: Error, parameter 0 incorrect\n", |
| 645 | __func__); |
| 646 | rc = -EINVAL; |
| 647 | goto afe_error; |
| 648 | } |
| 649 | if ((afe_validate_port(param[1]) < 0) || |
| 650 | (afe_validate_port(param[2])) < 0) { |
| 651 | pr_err("%s: Error, invalid afe port\n", |
| 652 | __func__); |
| 653 | } |
| 654 | if (this_afe.apr == NULL) { |
| 655 | pr_err("%s: Error, AFE not opened\n", __func__); |
| 656 | rc = -EINVAL; |
| 657 | } else { |
| 658 | rc = afe_loopback(param[0], param[1], param[2]); |
| 659 | } |
| 660 | } else { |
| 661 | pr_err("%s: Error, invalid parameters\n", __func__); |
| 662 | rc = -EINVAL; |
| 663 | } |
| 664 | |
| 665 | } else if (!strcmp(lb_str, "afe_loopback_gain")) { |
| 666 | rc = afe_get_parameters(lbuf, param, 2); |
| 667 | if (!rc) { |
| 668 | pr_info("%s %lu %lu\n", lb_str, param[0], param[1]); |
| 669 | |
| 670 | if (afe_validate_port(param[0]) < 0) { |
| 671 | pr_err("%s: Error, invalid afe port\n", |
| 672 | __func__); |
| 673 | rc = -EINVAL; |
| 674 | goto afe_error; |
| 675 | } |
| 676 | |
| 677 | if (param[1] < 0 || param[1] > 100) { |
| 678 | pr_err("%s: Error, volume shoud be 0 to 100" |
| 679 | " percentage param = %lu\n", |
| 680 | __func__, param[1]); |
| 681 | rc = -EINVAL; |
| 682 | goto afe_error; |
| 683 | } |
| 684 | |
| 685 | param[1] = (Q6AFE_MAX_VOLUME * param[1]) / 100; |
| 686 | |
| 687 | if (this_afe.apr == NULL) { |
| 688 | pr_err("%s: Error, AFE not opened\n", __func__); |
| 689 | rc = -EINVAL; |
| 690 | } else { |
| 691 | rc = afe_loopback_gain(param[0], param[1]); |
| 692 | } |
| 693 | } else { |
| 694 | pr_err("%s: Error, invalid parameters\n", __func__); |
| 695 | rc = -EINVAL; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | afe_error: |
| 700 | if (rc == 0) |
| 701 | rc = cnt; |
| 702 | else |
| 703 | pr_err("%s: rc = %d\n", __func__, rc); |
| 704 | |
| 705 | return rc; |
| 706 | } |
| 707 | |
| 708 | static const struct file_operations afe_debug_fops = { |
| 709 | .open = afe_debug_open, |
| 710 | .write = afe_debug_write |
| 711 | }; |
| 712 | #endif |
| 713 | int afe_sidetone(u16 tx_port_id, u16 rx_port_id, u16 enable, uint16_t gain) |
| 714 | { |
| 715 | struct afe_port_sidetone_command cmd_sidetone; |
| 716 | int ret = 0; |
| 717 | |
| 718 | pr_info("%s: tx_port_id:%d rx_port_id:%d enable:%d gain:%d\n", __func__, |
| 719 | tx_port_id, rx_port_id, enable, gain); |
| 720 | cmd_sidetone.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 721 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 722 | cmd_sidetone.hdr.pkt_size = sizeof(cmd_sidetone); |
| 723 | cmd_sidetone.hdr.src_port = 0; |
| 724 | cmd_sidetone.hdr.dest_port = 0; |
| 725 | cmd_sidetone.hdr.token = 0; |
| 726 | cmd_sidetone.hdr.opcode = AFE_PORT_CMD_SIDETONE_CTL; |
| 727 | cmd_sidetone.tx_port_id = tx_port_id; |
| 728 | cmd_sidetone.rx_port_id = rx_port_id; |
| 729 | cmd_sidetone.gain = gain; |
| 730 | cmd_sidetone.enable = enable; |
| 731 | |
| 732 | atomic_set(&this_afe.state, 1); |
| 733 | ret = apr_send_pkt(this_afe.apr, (uint32_t *) &cmd_sidetone); |
| 734 | if (ret < 0) { |
| 735 | pr_err("%s: AFE sidetone failed for tx_port:%d rx_port:%d\n", |
| 736 | __func__, tx_port_id, rx_port_id); |
| 737 | ret = -EINVAL; |
| 738 | goto fail_cmd; |
| 739 | } |
| 740 | |
| 741 | ret = wait_event_timeout(this_afe.wait, |
| 742 | (atomic_read(&this_afe.state) == 0), |
| 743 | msecs_to_jiffies(TIMEOUT_MS)); |
| 744 | if (ret < 0) { |
| 745 | pr_err("%s: wait_event timeout\n", __func__); |
| 746 | ret = -EINVAL; |
| 747 | goto fail_cmd; |
| 748 | } |
| 749 | return 0; |
| 750 | fail_cmd: |
| 751 | return ret; |
| 752 | } |
| 753 | |
| 754 | int afe_port_stop_nowait(int port_id) |
| 755 | { |
| 756 | struct afe_port_stop_command stop; |
| 757 | int ret = 0; |
| 758 | |
| 759 | if (this_afe.apr == NULL) { |
| 760 | pr_err("AFE is already closed\n"); |
| 761 | ret = -EINVAL; |
| 762 | goto fail_cmd; |
| 763 | } |
| 764 | pr_info("%s: port_id=%d\n", __func__, port_id); |
| 765 | stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 766 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 767 | stop.hdr.pkt_size = sizeof(stop); |
| 768 | stop.hdr.src_port = 0; |
| 769 | stop.hdr.dest_port = 0; |
| 770 | stop.hdr.token = 0; |
| 771 | stop.hdr.opcode = AFE_PORT_CMD_STOP; |
| 772 | stop.port_id = port_id; |
| 773 | stop.reserved = 0; |
| 774 | |
| 775 | ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop); |
| 776 | |
| 777 | if (ret == -ENETRESET) { |
| 778 | pr_info("%s: Need to reset, calling APR deregister", __func__); |
| 779 | return apr_deregister(this_afe.apr); |
| 780 | } else if (IS_ERR_VALUE(ret)) { |
| 781 | pr_err("%s: AFE close failed\n", __func__); |
| 782 | ret = -EINVAL; |
| 783 | } |
| 784 | |
| 785 | fail_cmd: |
| 786 | return ret; |
| 787 | |
| 788 | } |
| 789 | |
| 790 | int afe_close(int port_id) |
| 791 | { |
| 792 | struct afe_port_stop_command stop; |
| 793 | int ret = 0; |
| 794 | |
| 795 | if (this_afe.apr == NULL) { |
| 796 | pr_err("AFE is already closed\n"); |
| 797 | ret = -EINVAL; |
| 798 | goto fail_cmd; |
| 799 | } |
| 800 | pr_info("%s: port_id=%d\n", __func__, port_id); |
| 801 | stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, |
| 802 | APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER); |
| 803 | stop.hdr.pkt_size = sizeof(stop); |
| 804 | stop.hdr.src_port = 0; |
| 805 | stop.hdr.dest_port = 0; |
| 806 | stop.hdr.token = 0; |
| 807 | stop.hdr.opcode = AFE_PORT_CMD_STOP; |
| 808 | stop.port_id = port_id; |
| 809 | stop.reserved = 0; |
| 810 | |
| 811 | atomic_set(&this_afe.state, 1); |
| 812 | ret = apr_send_pkt(this_afe.apr, (uint32_t *) &stop); |
| 813 | |
| 814 | if (ret == -ENETRESET) { |
| 815 | pr_info("%s: Need to reset, calling APR deregister", __func__); |
| 816 | return apr_deregister(this_afe.apr); |
| 817 | } |
| 818 | |
| 819 | if (ret < 0) { |
| 820 | pr_err("%s: AFE close failed\n", __func__); |
| 821 | ret = -EINVAL; |
| 822 | goto fail_cmd; |
| 823 | } |
| 824 | |
| 825 | ret = wait_event_timeout(this_afe.wait, |
| 826 | (atomic_read(&this_afe.state) == 0), |
| 827 | msecs_to_jiffies(TIMEOUT_MS)); |
| 828 | if (!ret) { |
| 829 | pr_err("%s: wait_event timeout\n", __func__); |
| 830 | ret = -EINVAL; |
| 831 | goto fail_cmd; |
| 832 | } |
| 833 | fail_cmd: |
| 834 | return ret; |
| 835 | } |
| 836 | |
| 837 | static int __init afe_init(void) |
| 838 | { |
| 839 | init_waitqueue_head(&this_afe.wait); |
| 840 | atomic_set(&this_afe.state, 0); |
| 841 | atomic_set(&this_afe.status, 0); |
| 842 | this_afe.apr = NULL; |
| 843 | #ifdef CONFIG_DEBUG_FS |
| 844 | debugfs_afelb = debugfs_create_file("afe_loopback", |
| 845 | S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback", |
| 846 | &afe_debug_fops); |
| 847 | |
| 848 | debugfs_afelb_gain = debugfs_create_file("afe_loopback_gain", |
| 849 | S_IFREG | S_IWUGO, NULL, (void *) "afe_loopback_gain", |
| 850 | &afe_debug_fops); |
| 851 | |
| 852 | |
| 853 | #endif |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | static void __exit afe_exit(void) |
| 858 | { |
| 859 | #ifdef CONFIG_DEBUG_FS |
| 860 | if (debugfs_afelb) |
| 861 | debugfs_remove(debugfs_afelb); |
| 862 | if (debugfs_afelb_gain) |
| 863 | debugfs_remove(debugfs_afelb_gain); |
| 864 | #endif |
| 865 | } |
| 866 | |
| 867 | device_initcall(afe_init); |
| 868 | __exitcall(afe_exit); |