Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * All source code in this file is licensed under the following license except |
| 4 | * where indicated. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * |
| 14 | * See the GNU General Public License for more details. |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, you can find it at http://www.fsf.org. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/moduleparam.h> |
| 23 | #include <linux/time.h> |
| 24 | #include <linux/wait.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/mutex.h> |
| 27 | #include <linux/uaccess.h> |
| 28 | #include <linux/wakelock.h> |
| 29 | #include <linux/dma-mapping.h> |
| 30 | #include <sound/core.h> |
| 31 | #include <sound/soc.h> |
| 32 | #include <sound/pcm.h> |
| 33 | #include <sound/initval.h> |
| 34 | #include <sound/control.h> |
| 35 | #include <sound/q6asm.h> |
| 36 | #include <sound/apr_audio.h> |
| 37 | #include <mach/msm_rpcrouter.h> |
| 38 | #include <mach/qdsp6v2/q6voice.h> |
| 39 | #include <mach/qdsp6v2/audio_dev_ctl.h> |
| 40 | #include "msm_audio_mvs.h" |
| 41 | |
| 42 | |
| 43 | static struct audio_voip_info_type audio_voip_info; |
| 44 | static void audio_mvs_process_ul_pkt(uint8_t *voc_pkt, |
| 45 | uint32_t pkt_len, |
| 46 | void *private_data); |
| 47 | static void audio_mvs_process_dl_pkt(uint8_t *voc_pkt, |
| 48 | uint32_t *pkt_len, |
| 49 | void *private_data); |
| 50 | |
| 51 | struct msm_audio_mvs_frame { |
| 52 | uint32_t frame_type; |
| 53 | uint32_t len; |
| 54 | uint8_t voc_pkt[MVS_MAX_VOC_PKT_SIZE]; |
| 55 | }; |
| 56 | |
| 57 | struct audio_mvs_buf_node { |
| 58 | struct list_head list; |
| 59 | struct msm_audio_mvs_frame frame; |
| 60 | }; |
| 61 | |
| 62 | static struct snd_pcm_hardware msm_pcm_hardware = { |
| 63 | .info = SNDRV_PCM_INFO_INTERLEAVED, |
| 64 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 65 | .rates = (SNDRV_PCM_RATE_8000), |
| 66 | .rate_min = 8000, |
| 67 | .rate_max = 8000, |
| 68 | .channels_min = 1, |
| 69 | .channels_max = 2, |
| 70 | .buffer_bytes_max = MVS_MAX_VOC_PKT_SIZE * VOIP_MAX_Q_LEN, |
| 71 | .period_bytes_min = MVS_MAX_VOC_PKT_SIZE, |
| 72 | .period_bytes_max = MVS_MAX_VOC_PKT_SIZE, |
| 73 | .periods_min = VOIP_MAX_Q_LEN, |
| 74 | .periods_max = VOIP_MAX_Q_LEN, |
| 75 | .fifo_size = 0, |
| 76 | }; |
| 77 | |
| 78 | static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 79 | { |
| 80 | |
| 81 | struct audio_voip_info_type *audio = &audio_voip_info; |
| 82 | pr_debug("%s\n", __func__); |
| 83 | |
| 84 | switch (cmd) { |
| 85 | case SNDRV_PCM_TRIGGER_START: |
| 86 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 87 | audio->playback_start = 1; |
| 88 | else |
| 89 | audio->capture_start = 1; |
| 90 | break; |
| 91 | case SNDRV_PCM_TRIGGER_STOP: |
| 92 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 93 | audio->playback_start = 0; |
| 94 | else |
| 95 | audio->capture_start = 0; |
| 96 | break; |
| 97 | default: |
| 98 | break; |
| 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int msm_pcm_close(struct snd_pcm_substream *substream) |
| 104 | { |
| 105 | int rc = 0; |
| 106 | struct audio_voip_info_type *audio = &audio_voip_info; |
| 107 | struct audio_mvs_release_msg release_msg; |
| 108 | |
| 109 | pr_debug("%s\n", __func__); |
| 110 | memset(&release_msg, 0, sizeof(release_msg)); |
| 111 | mutex_lock(&audio->lock); |
| 112 | |
| 113 | audio->instance--; |
| 114 | wake_up(&audio->out_wait); |
| 115 | |
| 116 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 117 | audio->playback_state = AUDIO_MVS_CLOSED; |
| 118 | else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 119 | audio->capture_state = AUDIO_MVS_CLOSED; |
| 120 | if (!audio->instance) { |
| 121 | /* Release MVS. */ |
| 122 | release_msg.client_id = cpu_to_be32(MVS_CLIENT_ID_VOIP); |
| 123 | /* Derigstering the callbacks with voice driver */ |
| 124 | voice_register_mvs_cb(NULL, NULL, audio); |
| 125 | } else if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 126 | voice_register_mvs_cb(audio_mvs_process_ul_pkt, |
| 127 | NULL, audio); |
| 128 | } else { |
| 129 | voice_register_mvs_cb(NULL, audio_mvs_process_dl_pkt, |
| 130 | audio); |
| 131 | } |
| 132 | |
| 133 | mutex_unlock(&audio->lock); |
| 134 | |
| 135 | wake_unlock(&audio->suspend_lock); |
Stephen Boyd | f04955c | 2012-05-31 16:45:49 -0700 | [diff] [blame] | 136 | pm_qos_update_request(&audio->pm_qos_req, PM_QOS_DEFAULT_VALUE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 137 | /* Release the IO buffers. */ |
| 138 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 139 | audio->in_write = 0; |
| 140 | audio->in_read = 0; |
| 141 | memset(audio->in[0].voc_pkt, 0, |
| 142 | MVS_MAX_VOC_PKT_SIZE * VOIP_MAX_Q_LEN); |
| 143 | audio->playback_substream = NULL; |
| 144 | } else { |
| 145 | audio->out_write = 0; |
| 146 | audio->out_read = 0; |
| 147 | memset(audio->out[0].voc_pkt, 0, |
| 148 | MVS_MAX_VOC_PKT_SIZE * VOIP_MAX_Q_LEN); |
| 149 | audio->capture_substream = NULL; |
| 150 | } |
| 151 | return rc; |
| 152 | } |
| 153 | |
| 154 | static int msm_pcm_open(struct snd_pcm_substream *substream) |
| 155 | { |
| 156 | int ret = 0; |
| 157 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 158 | struct audio_voip_info_type *audio = &audio_voip_info; |
| 159 | |
| 160 | pr_debug("%s\n", __func__); |
| 161 | mutex_lock(&audio->lock); |
| 162 | |
| 163 | if (audio->playback_substream == NULL || |
| 164 | audio->capture_substream == NULL) { |
| 165 | if (substream->stream == |
| 166 | SNDRV_PCM_STREAM_PLAYBACK) { |
| 167 | audio->playback_substream = substream; |
| 168 | runtime->hw = msm_pcm_hardware; |
| 169 | audio_voip_info.in_read = 0; |
| 170 | audio_voip_info.in_write = 0; |
| 171 | if (audio->playback_state < AUDIO_MVS_OPENED) |
| 172 | audio->playback_state = AUDIO_MVS_OPENED; |
| 173 | } else if (substream->stream == |
| 174 | SNDRV_PCM_STREAM_CAPTURE) { |
| 175 | audio->capture_substream = substream; |
| 176 | runtime->hw = msm_pcm_hardware; |
| 177 | audio_voip_info.out_read = 0; |
| 178 | audio_voip_info.out_write = 0; |
| 179 | if (audio->capture_state < AUDIO_MVS_OPENED) |
| 180 | audio->capture_state = AUDIO_MVS_OPENED; |
| 181 | } |
| 182 | } else { |
| 183 | ret = -EPERM; |
| 184 | goto err; |
| 185 | } |
| 186 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 187 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 188 | if (ret < 0) { |
| 189 | pr_debug("%s:snd_pcm_hw_constraint_integer failed\n", __func__); |
| 190 | goto err; |
| 191 | } |
| 192 | audio->instance++; |
| 193 | |
| 194 | err: |
| 195 | mutex_unlock(&audio->lock); |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a, |
| 200 | snd_pcm_uframes_t hwoff, void __user *buf, |
| 201 | snd_pcm_uframes_t frames) |
| 202 | { |
| 203 | int rc = 0; |
| 204 | int count = 0; |
| 205 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 206 | struct audio_voip_info_type *audio = &audio_voip_info; |
| 207 | uint32_t index; |
| 208 | pr_debug("%s\n", __func__); |
| 209 | |
| 210 | rc = wait_event_timeout(audio->in_wait, |
| 211 | (audio->in_write - audio->in_read <= VOIP_MAX_Q_LEN-1), |
| 212 | 1 * HZ); |
| 213 | if (rc < 0) { |
| 214 | pr_debug("%s: write was interrupted\n", __func__); |
| 215 | return -ERESTARTSYS; |
| 216 | } |
| 217 | |
| 218 | if (audio->playback_state == AUDIO_MVS_ENABLED) { |
| 219 | index = audio->in_write % VOIP_MAX_Q_LEN; |
| 220 | count = frames_to_bytes(runtime, frames); |
| 221 | if (count == MVS_MAX_VOC_PKT_SIZE) { |
| 222 | pr_debug("%s:write index = %d\n", __func__, index); |
| 223 | rc = copy_from_user(audio->in[index].voc_pkt, buf, |
| 224 | count); |
| 225 | if (!rc) { |
| 226 | audio->in[index].len = count; |
| 227 | audio->in_write++; |
| 228 | } else { |
| 229 | pr_debug("%s:Copy from user returned %d\n", |
| 230 | __func__, rc); |
| 231 | rc = -EFAULT; |
| 232 | } |
| 233 | } else |
| 234 | rc = -ENOMEM; |
| 235 | |
| 236 | } else { |
| 237 | pr_debug("%s:Write performed in invalid state %d\n", |
| 238 | __func__, audio->playback_state); |
| 239 | rc = -EINVAL; |
| 240 | } |
| 241 | return rc; |
| 242 | } |
| 243 | |
| 244 | static int msm_pcm_capture_copy(struct snd_pcm_substream *substream, |
| 245 | int channel, snd_pcm_uframes_t hwoff, |
| 246 | void __user *buf, snd_pcm_uframes_t frames) |
| 247 | { |
| 248 | int rc = 0; |
| 249 | int count = 0; |
| 250 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 251 | struct audio_voip_info_type *audio = &audio_voip_info; |
| 252 | uint32_t index = 0; |
| 253 | |
| 254 | pr_debug("%s\n", __func__); |
| 255 | |
| 256 | /* Ensure the driver has been enabled. */ |
| 257 | if (audio->capture_state != AUDIO_MVS_ENABLED) { |
| 258 | pr_debug("%s:Read performed in invalid state %d\n", |
| 259 | __func__, audio->capture_state); |
| 260 | return -EPERM; |
| 261 | } |
| 262 | rc = wait_event_timeout(audio->out_wait, |
| 263 | ((audio->out_read < audio->out_write) || |
| 264 | (audio->capture_state == AUDIO_MVS_CLOSING) || |
| 265 | (audio->capture_state == AUDIO_MVS_CLOSED)), |
| 266 | 1 * HZ); |
| 267 | |
| 268 | if (rc < 0) { |
| 269 | pr_debug("%s: Read was interrupted\n", __func__); |
| 270 | return -ERESTARTSYS; |
| 271 | } |
| 272 | |
| 273 | if (audio->capture_state == AUDIO_MVS_CLOSING |
| 274 | || audio->capture_state == AUDIO_MVS_CLOSED) { |
| 275 | pr_debug("%s:EBUSY STATE\n", __func__); |
| 276 | rc = -EBUSY; |
| 277 | } else { |
| 278 | count = frames_to_bytes(runtime, frames); |
| 279 | index = audio->out_read % VOIP_MAX_Q_LEN; |
| 280 | pr_debug("%s:index=%d\n", __func__, index); |
| 281 | if (audio->out[index].len <= count) { |
| 282 | rc = copy_to_user(buf, |
| 283 | audio->out[index].voc_pkt, |
| 284 | audio->out[index].len); |
| 285 | if (rc) { |
| 286 | pr_debug("%s:Copy to user %d\n", |
| 287 | __func__, rc); |
| 288 | rc = -EFAULT; |
| 289 | } else |
| 290 | audio->out_read++; |
| 291 | } else { |
| 292 | pr_debug("%s:returning ENOMEM\n", __func__); |
| 293 | rc = -ENOMEM; |
| 294 | } |
| 295 | } |
| 296 | return rc; |
| 297 | } |
| 298 | |
| 299 | static int msm_pcm_copy(struct snd_pcm_substream *substream, int a, |
| 300 | snd_pcm_uframes_t hwoff, void __user *buf, |
| 301 | snd_pcm_uframes_t frames) |
| 302 | { |
| 303 | int ret = 0; |
| 304 | pr_debug("%s\n", __func__); |
| 305 | |
| 306 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 307 | ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames); |
| 308 | else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 309 | ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames); |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | /* Capture path */ |
| 314 | static void audio_mvs_process_ul_pkt(uint8_t *voc_pkt, |
| 315 | uint32_t pkt_len, |
| 316 | void *private_data) |
| 317 | { |
| 318 | struct audio_voip_info_type *audio = private_data; |
| 319 | uint32_t index; |
| 320 | static int i; |
| 321 | pr_debug("%s\n", __func__); |
| 322 | |
| 323 | if (audio->capture_substream == NULL) |
| 324 | return; |
| 325 | index = audio->out_write % VOIP_MAX_Q_LEN; |
| 326 | memcpy(audio->out[index].voc_pkt, voc_pkt, pkt_len); |
| 327 | audio->out[index].len = pkt_len; |
| 328 | audio->out_write++; |
| 329 | wake_up(&audio->out_wait); |
| 330 | i++; |
| 331 | if (audio->capture_start) { |
| 332 | audio->pcm_capture_irq_pos += audio->pcm_count; |
| 333 | if (!(i % 2)) |
| 334 | snd_pcm_period_elapsed(audio->capture_substream); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /* Playback path */ |
| 339 | static void audio_mvs_process_dl_pkt(uint8_t *voc_pkt, |
| 340 | uint32_t *pkt_len, |
| 341 | void *private_data) |
| 342 | { |
| 343 | struct audio_voip_info_type *audio = private_data; |
| 344 | uint32_t index; |
| 345 | static int i; |
| 346 | pr_debug("%s\n", __func__); |
| 347 | |
| 348 | if (audio->playback_substream == NULL) |
| 349 | return; |
| 350 | if ((audio->in_write - audio->in_read >= 0) |
| 351 | && (audio->playback_start)) { |
| 352 | index = audio->in_read % VOIP_MAX_Q_LEN; |
| 353 | *pkt_len = audio->pcm_count; |
| 354 | memcpy(voc_pkt, audio->in[index].voc_pkt, *pkt_len); |
| 355 | audio->in_read++; |
| 356 | wake_up(&audio->in_wait); |
| 357 | i++; |
| 358 | audio->pcm_playback_irq_pos += audio->pcm_count; |
| 359 | if (!(i%2)) |
| 360 | snd_pcm_period_elapsed(audio->playback_substream); |
| 361 | pr_debug("%s:read_index=%d\n", __func__, index); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | static int msm_pcm_prepare(struct snd_pcm_substream *substream) |
| 366 | { |
| 367 | int rc = 0; |
| 368 | struct audio_voip_info_type *prtd = &audio_voip_info; |
| 369 | pr_debug("%s\n", __func__); |
| 370 | prtd->pcm_playback_size = snd_pcm_lib_buffer_bytes(substream); |
| 371 | prtd->pcm_count = snd_pcm_lib_period_bytes(substream); |
| 372 | pr_debug("%s:prtd->pcm_playback_size:%d\n", |
| 373 | __func__, prtd->pcm_playback_size); |
| 374 | pr_debug("%s:prtd->pcm_count:%d\n", __func__, prtd->pcm_count); |
| 375 | |
| 376 | mutex_lock(&prtd->prepare_lock); |
| 377 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 378 | if (prtd->playback_state == AUDIO_MVS_ENABLED) |
| 379 | goto enabled; |
| 380 | } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 381 | if (prtd->capture_state == AUDIO_MVS_ENABLED) |
| 382 | goto enabled; |
| 383 | } |
| 384 | |
| 385 | pr_debug("%s:Register cbs with voice driver check audio_mvs_driver\n", |
| 386 | __func__); |
| 387 | if (prtd->instance == 2) { |
| 388 | voice_register_mvs_cb(audio_mvs_process_ul_pkt, |
| 389 | audio_mvs_process_dl_pkt, |
| 390 | prtd); |
| 391 | } else { |
| 392 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 393 | voice_register_mvs_cb(NULL, |
| 394 | audio_mvs_process_dl_pkt, |
| 395 | prtd); |
| 396 | } else { |
| 397 | voice_register_mvs_cb(audio_mvs_process_ul_pkt, |
| 398 | NULL, |
| 399 | prtd); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | enabled: |
| 404 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 405 | prtd->playback_state = AUDIO_MVS_ENABLED; |
| 406 | prtd->pcm_playback_irq_pos = 0; |
| 407 | prtd->pcm_playback_buf_pos = 0; |
| 408 | /* rate and channels are sent to audio driver */ |
| 409 | } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 410 | prtd->capture_state = AUDIO_MVS_ENABLED; |
| 411 | prtd->pcm_capture_size = snd_pcm_lib_buffer_bytes(substream); |
| 412 | prtd->pcm_capture_count = snd_pcm_lib_period_bytes(substream); |
| 413 | prtd->pcm_capture_irq_pos = 0; |
| 414 | prtd->pcm_capture_buf_pos = 0; |
| 415 | } |
| 416 | mutex_unlock(&prtd->prepare_lock); |
| 417 | return rc; |
| 418 | } |
| 419 | |
| 420 | static snd_pcm_uframes_t |
| 421 | msm_pcm_playback_pointer(struct snd_pcm_substream *substream) |
| 422 | { |
| 423 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 424 | struct audio_voip_info_type *audio = &audio_voip_info; |
| 425 | |
| 426 | if (audio->pcm_playback_irq_pos >= audio->pcm_playback_size) |
| 427 | audio->pcm_playback_irq_pos = 0; |
| 428 | return bytes_to_frames(runtime, (audio->pcm_playback_irq_pos)); |
| 429 | } |
| 430 | |
| 431 | static snd_pcm_uframes_t |
| 432 | msm_pcm_capture_pointer(struct snd_pcm_substream *substream) |
| 433 | { |
| 434 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 435 | struct audio_voip_info_type *audio = &audio_voip_info; |
| 436 | |
| 437 | if (audio->pcm_capture_irq_pos >= audio->pcm_capture_size) |
| 438 | audio->pcm_capture_irq_pos = 0; |
| 439 | return bytes_to_frames(runtime, (audio->pcm_capture_irq_pos)); |
| 440 | } |
| 441 | |
| 442 | static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream) |
| 443 | { |
| 444 | snd_pcm_uframes_t ret = 0; |
| 445 | pr_debug("%s\n", __func__); |
| 446 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 447 | ret = msm_pcm_playback_pointer(substream); |
| 448 | else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 449 | ret = msm_pcm_capture_pointer(substream); |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | static struct snd_pcm_ops msm_mvs_pcm_ops = { |
| 454 | .open = msm_pcm_open, |
| 455 | .copy = msm_pcm_copy, |
| 456 | .close = msm_pcm_close, |
| 457 | .ioctl = snd_pcm_lib_ioctl, |
| 458 | .prepare = msm_pcm_prepare, |
| 459 | .trigger = msm_pcm_trigger, |
| 460 | .pointer = msm_pcm_pointer, |
| 461 | |
| 462 | }; |
| 463 | |
| 464 | static int msm_pcm_new(struct snd_soc_pcm_runtime *rtd) |
| 465 | { |
| 466 | int i, ret, offset = 0; |
| 467 | struct snd_pcm_substream *substream = NULL; |
| 468 | struct snd_dma_buffer *dma_buffer = NULL; |
| 469 | struct snd_card *card = rtd->card->snd_card; |
| 470 | struct snd_pcm *pcm = rtd->pcm; |
| 471 | |
| 472 | ret = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, 1); |
| 473 | if (ret) |
| 474 | return ret; |
| 475 | ret = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, 1); |
| 476 | if (ret) |
| 477 | return ret; |
| 478 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &msm_mvs_pcm_ops); |
| 479 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &msm_mvs_pcm_ops); |
| 480 | |
| 481 | if (!card->dev->coherent_dma_mask) |
| 482 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); |
| 483 | |
| 484 | substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; |
| 485 | if (!substream) |
| 486 | return -ENOMEM; |
| 487 | |
| 488 | dma_buffer = &substream->dma_buffer; |
| 489 | dma_buffer->dev.type = SNDRV_DMA_TYPE_DEV; |
| 490 | dma_buffer->dev.dev = card->dev; |
| 491 | dma_buffer->private_data = NULL; |
| 492 | dma_buffer->area = dma_alloc_coherent(card->dev, |
| 493 | (MVS_MAX_VOC_PKT_SIZE * VOIP_MAX_Q_LEN), |
| 494 | &dma_buffer->addr, GFP_KERNEL); |
| 495 | if (!dma_buffer->area) { |
| 496 | pr_err("%s:MSM VOIP dma_alloc failed\n", __func__); |
| 497 | return -ENOMEM; |
| 498 | } |
| 499 | dma_buffer->bytes = MVS_MAX_VOC_PKT_SIZE * VOIP_MAX_Q_LEN; |
| 500 | memset(dma_buffer->area, 0, MVS_MAX_VOC_PKT_SIZE * VOIP_MAX_Q_LEN); |
| 501 | audio_voip_info.in_read = 0; |
| 502 | audio_voip_info.in_write = 0; |
| 503 | audio_voip_info.out_read = 0; |
| 504 | audio_voip_info.out_write = 0; |
| 505 | for (i = 0; i < VOIP_MAX_Q_LEN; i++) { |
| 506 | audio_voip_info.in[i].voc_pkt = |
| 507 | dma_buffer->area + offset; |
| 508 | offset = offset + MVS_MAX_VOC_PKT_SIZE; |
| 509 | } |
| 510 | substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; |
| 511 | if (!substream) |
| 512 | return -ENOMEM; |
| 513 | |
| 514 | dma_buffer = &substream->dma_buffer; |
| 515 | dma_buffer->dev.type = SNDRV_DMA_TYPE_DEV; |
| 516 | dma_buffer->dev.dev = card->dev; |
| 517 | dma_buffer->private_data = NULL; |
| 518 | dma_buffer->area = dma_alloc_coherent(card->dev, |
| 519 | (MVS_MAX_VOC_PKT_SIZE * VOIP_MAX_Q_LEN), |
| 520 | &dma_buffer->addr, GFP_KERNEL); |
| 521 | if (!dma_buffer->area) { |
| 522 | pr_err("%s:MSM VOIP dma_alloc failed\n", __func__); |
| 523 | return -ENOMEM; |
| 524 | } |
| 525 | memset(dma_buffer->area, 0, MVS_MAX_VOC_PKT_SIZE * VOIP_MAX_Q_LEN); |
| 526 | dma_buffer->bytes = MVS_MAX_VOC_PKT_SIZE * VOIP_MAX_Q_LEN; |
| 527 | for (i = 0; i < VOIP_MAX_Q_LEN; i++) { |
| 528 | audio_voip_info.out[i].voc_pkt = |
| 529 | dma_buffer->area + offset; |
| 530 | offset = offset + MVS_MAX_VOC_PKT_SIZE; |
| 531 | } |
| 532 | audio_voip_info.playback_substream = NULL; |
| 533 | audio_voip_info.capture_substream = NULL; |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static void msm_pcm_free_buffers(struct snd_pcm *pcm) |
| 539 | { |
| 540 | struct snd_pcm_substream *substream; |
| 541 | struct snd_dma_buffer *buf; |
| 542 | int stream; |
| 543 | |
| 544 | for (stream = 0; stream < 2; stream++) { |
| 545 | substream = pcm->streams[stream].substream; |
| 546 | if (!substream) |
| 547 | continue; |
| 548 | |
| 549 | buf = &substream->dma_buffer; |
| 550 | if (!buf->area) |
| 551 | continue; |
| 552 | |
| 553 | dma_free_coherent(pcm->card->dev, buf->bytes, |
| 554 | buf->area, buf->addr); |
| 555 | buf->area = NULL; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | struct snd_soc_platform_driver msm_mvs_soc_platform = { |
| 560 | .ops = &msm_mvs_pcm_ops, |
| 561 | .pcm_new = msm_pcm_new, |
| 562 | .pcm_free = msm_pcm_free_buffers, |
| 563 | }; |
| 564 | EXPORT_SYMBOL(msm_mvs_soc_platform); |
| 565 | |
| 566 | static __devinit int msm_pcm_probe(struct platform_device *pdev) |
| 567 | { |
| 568 | dev_info(&pdev->dev, "%s: dev name %s\n", __func__, dev_name(&pdev->dev)); |
| 569 | return snd_soc_register_platform(&pdev->dev, |
| 570 | &msm_mvs_soc_platform); |
| 571 | } |
| 572 | |
| 573 | static int msm_pcm_remove(struct platform_device *pdev) |
| 574 | { |
| 575 | snd_soc_unregister_platform(&pdev->dev); |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static struct platform_driver msm_pcm_driver = { |
| 580 | .driver = { |
| 581 | .name = "msm-mvs-audio", |
| 582 | .owner = THIS_MODULE, |
| 583 | }, |
| 584 | .probe = msm_pcm_probe, |
| 585 | .remove = __devexit_p(msm_pcm_remove), |
| 586 | }; |
| 587 | |
| 588 | static int __init msm_mvs_soc_platform_init(void) |
| 589 | { |
| 590 | memset(&audio_voip_info, 0, sizeof(audio_voip_info)); |
| 591 | mutex_init(&audio_voip_info.lock); |
| 592 | mutex_init(&audio_voip_info.prepare_lock); |
| 593 | init_waitqueue_head(&audio_voip_info.out_wait); |
| 594 | init_waitqueue_head(&audio_voip_info.in_wait); |
| 595 | wake_lock_init(&audio_voip_info.suspend_lock, WAKE_LOCK_SUSPEND, |
| 596 | "audio_mvs_suspend"); |
Stephen Boyd | f04955c | 2012-05-31 16:45:49 -0700 | [diff] [blame] | 597 | pm_qos_add_request(&audio_voip_info.pm_qos_req, PM_QOS_CPU_DMA_LATENCY, |
| 598 | PM_QOS_DEFAULT_VALUE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 599 | return platform_driver_register(&msm_pcm_driver); |
| 600 | } |
| 601 | module_init(msm_mvs_soc_platform_init); |
| 602 | |
| 603 | static void __exit msm_mvs_soc_platform_exit(void) |
| 604 | { |
| 605 | platform_driver_unregister(&msm_pcm_driver); |
| 606 | } |
| 607 | module_exit(msm_mvs_soc_platform_exit); |
| 608 | |
| 609 | MODULE_DESCRIPTION("MVS PCM module platform driver"); |
| 610 | MODULE_LICENSE("GPL v2"); |