Asish Bhattacharya | 305d175 | 2011-11-01 20:38:26 +0530 | [diff] [blame] | 1 | /* Copyright (c) 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 | |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/moduleparam.h> |
| 18 | #include <linux/time.h> |
| 19 | #include <linux/wait.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <sound/core.h> |
| 23 | #include <sound/soc.h> |
| 24 | #include <sound/soc-dapm.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include <sound/initval.h> |
| 27 | #include <sound/control.h> |
| 28 | #include <asm/dma.h> |
| 29 | #include <linux/dma-mapping.h> |
| 30 | #include <linux/android_pmem.h> |
| 31 | |
| 32 | #include "msm-compr-q6.h" |
| 33 | #include "msm-pcm-routing.h" |
| 34 | |
| 35 | static struct audio_locks the_locks; |
| 36 | |
| 37 | static struct snd_pcm_hardware msm_compr_hardware_playback = { |
| 38 | .info = (SNDRV_PCM_INFO_MMAP | |
| 39 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 40 | SNDRV_PCM_INFO_MMAP_VALID | |
| 41 | SNDRV_PCM_INFO_INTERLEAVED | |
| 42 | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME), |
| 43 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 44 | .rates = SNDRV_PCM_RATE_8000_48000, |
| 45 | .rate_min = 8000, |
| 46 | .rate_max = 48000, |
| 47 | .channels_min = 1, |
| 48 | .channels_max = 2, |
| 49 | .buffer_bytes_max = 1200 * 1024 * 2, |
Krishnankutty Kolathappilly | a966f45 | 2012-01-12 12:28:36 -0800 | [diff] [blame] | 50 | .period_bytes_min = 4800, |
Asish Bhattacharya | 305d175 | 2011-11-01 20:38:26 +0530 | [diff] [blame] | 51 | .period_bytes_max = 1200 * 1024, |
| 52 | .periods_min = 2, |
Krishnankutty Kolathappilly | a966f45 | 2012-01-12 12:28:36 -0800 | [diff] [blame] | 53 | .periods_max = 512, |
Asish Bhattacharya | 305d175 | 2011-11-01 20:38:26 +0530 | [diff] [blame] | 54 | .fifo_size = 0, |
| 55 | }; |
| 56 | |
| 57 | /* Conventional and unconventional sample rate supported */ |
| 58 | static unsigned int supported_sample_rates[] = { |
| 59 | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 |
| 60 | }; |
| 61 | |
| 62 | static struct snd_pcm_hw_constraint_list constraints_sample_rates = { |
| 63 | .count = ARRAY_SIZE(supported_sample_rates), |
| 64 | .list = supported_sample_rates, |
| 65 | .mask = 0, |
| 66 | }; |
| 67 | |
| 68 | static void compr_event_handler(uint32_t opcode, |
| 69 | uint32_t token, uint32_t *payload, void *priv) |
| 70 | { |
| 71 | struct compr_audio *compr = priv; |
| 72 | struct msm_audio *prtd = &compr->prtd; |
| 73 | struct snd_pcm_substream *substream = prtd->substream; |
| 74 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 75 | struct audio_aio_write_param param; |
| 76 | struct audio_buffer *buf = NULL; |
| 77 | int i = 0; |
| 78 | |
| 79 | pr_debug("%s opcode =%08x\n", __func__, opcode); |
| 80 | switch (opcode) { |
| 81 | case ASM_DATA_EVENT_WRITE_DONE: { |
| 82 | uint32_t *ptrmem = (uint32_t *)¶m; |
| 83 | pr_debug("ASM_DATA_EVENT_WRITE_DONE\n"); |
| 84 | pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem); |
| 85 | prtd->pcm_irq_pos += prtd->pcm_count; |
| 86 | if (atomic_read(&prtd->start)) |
| 87 | snd_pcm_period_elapsed(substream); |
| 88 | atomic_inc(&prtd->out_count); |
| 89 | wake_up(&the_locks.write_wait); |
| 90 | if (!atomic_read(&prtd->start)) { |
Asish Bhattacharya | 31bd80d | 2012-01-04 02:30:13 +0530 | [diff] [blame] | 91 | atomic_set(&prtd->pending_buffer, 1); |
Asish Bhattacharya | 305d175 | 2011-11-01 20:38:26 +0530 | [diff] [blame] | 92 | break; |
| 93 | } else |
Asish Bhattacharya | 31bd80d | 2012-01-04 02:30:13 +0530 | [diff] [blame] | 94 | atomic_set(&prtd->pending_buffer, 0); |
Asish Bhattacharya | 305d175 | 2011-11-01 20:38:26 +0530 | [diff] [blame] | 95 | |
| 96 | if (runtime->status->hw_ptr >= runtime->control->appl_ptr) |
| 97 | break; |
| 98 | buf = prtd->audio_client->port[IN].buf; |
| 99 | pr_debug("%s:writing %d bytes of buffer[%d] to dsp 2\n", |
| 100 | __func__, prtd->pcm_count, prtd->out_head); |
| 101 | pr_debug("%s:writing buffer[%d] from 0x%08x\n", |
| 102 | __func__, prtd->out_head, |
| 103 | ((unsigned int)buf[0].phys |
| 104 | + (prtd->out_head * prtd->pcm_count))); |
| 105 | |
| 106 | param.paddr = (unsigned long)buf[0].phys |
| 107 | + (prtd->out_head * prtd->pcm_count); |
| 108 | param.len = prtd->pcm_count; |
| 109 | param.msw_ts = 0; |
| 110 | param.lsw_ts = 0; |
| 111 | param.flags = NO_TIMESTAMP; |
| 112 | param.uid = (unsigned long)buf[0].phys |
| 113 | + (prtd->out_head * prtd->pcm_count); |
| 114 | for (i = 0; i < sizeof(struct audio_aio_write_param)/4; |
| 115 | i++, ++ptrmem) |
| 116 | pr_debug("cmd[%d]=0x%08x\n", i, *ptrmem); |
| 117 | if (q6asm_async_write(prtd->audio_client, |
| 118 | ¶m) < 0) |
| 119 | pr_err("%s:q6asm_async_write failed\n", |
| 120 | __func__); |
| 121 | else |
| 122 | prtd->out_head = |
| 123 | (prtd->out_head + 1) & (runtime->periods - 1); |
| 124 | break; |
| 125 | } |
| 126 | case ASM_DATA_CMDRSP_EOS: |
| 127 | pr_debug("ASM_DATA_CMDRSP_EOS\n"); |
| 128 | prtd->cmd_ack = 1; |
| 129 | wake_up(&the_locks.eos_wait); |
| 130 | break; |
| 131 | case APR_BASIC_RSP_RESULT: { |
| 132 | switch (payload[0]) { |
| 133 | case ASM_SESSION_CMD_RUN: { |
Asish Bhattacharya | 31bd80d | 2012-01-04 02:30:13 +0530 | [diff] [blame] | 134 | if (!atomic_read(&prtd->pending_buffer)) |
Asish Bhattacharya | 305d175 | 2011-11-01 20:38:26 +0530 | [diff] [blame] | 135 | break; |
| 136 | pr_debug("%s:writing %d bytes" |
| 137 | " of buffer[%d] to dsp\n", |
| 138 | __func__, prtd->pcm_count, prtd->out_head); |
| 139 | buf = prtd->audio_client->port[IN].buf; |
| 140 | pr_debug("%s:writing buffer[%d] from 0x%08x\n", |
| 141 | __func__, prtd->out_head, |
| 142 | ((unsigned int)buf[0].phys |
| 143 | + (prtd->out_head * prtd->pcm_count))); |
| 144 | param.paddr = (unsigned long)buf[prtd->out_head].phys; |
| 145 | param.len = prtd->pcm_count; |
| 146 | param.msw_ts = 0; |
| 147 | param.lsw_ts = 0; |
| 148 | param.flags = NO_TIMESTAMP; |
| 149 | param.uid = (unsigned long)buf[prtd->out_head].phys; |
| 150 | if (q6asm_async_write(prtd->audio_client, |
| 151 | ¶m) < 0) |
| 152 | pr_err("%s:q6asm_async_write failed\n", |
| 153 | __func__); |
| 154 | else |
| 155 | prtd->out_head = |
| 156 | (prtd->out_head + 1) |
| 157 | & (runtime->periods - 1); |
Asish Bhattacharya | 31bd80d | 2012-01-04 02:30:13 +0530 | [diff] [blame] | 158 | atomic_set(&prtd->pending_buffer, 0); |
Asish Bhattacharya | 305d175 | 2011-11-01 20:38:26 +0530 | [diff] [blame] | 159 | } |
| 160 | break; |
| 161 | case ASM_STREAM_CMD_FLUSH: |
| 162 | pr_debug("ASM_STREAM_CMD_FLUSH\n"); |
| 163 | prtd->cmd_ack = 1; |
| 164 | wake_up(&the_locks.eos_wait); |
| 165 | break; |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | break; |
| 170 | } |
| 171 | default: |
| 172 | pr_debug("Not Supported Event opcode[0x%x]\n", opcode); |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static int msm_compr_playback_prepare(struct snd_pcm_substream *substream) |
| 178 | { |
| 179 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 180 | struct compr_audio *compr = runtime->private_data; |
| 181 | struct msm_audio *prtd = &compr->prtd; |
| 182 | int ret; |
| 183 | |
| 184 | pr_debug("%s\n", __func__); |
| 185 | prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream); |
| 186 | prtd->pcm_count = snd_pcm_lib_period_bytes(substream); |
| 187 | prtd->pcm_irq_pos = 0; |
| 188 | /* rate and channels are sent to audio driver */ |
| 189 | prtd->samp_rate = runtime->rate; |
| 190 | prtd->channel_mode = runtime->channels; |
| 191 | prtd->out_head = 0; |
| 192 | if (prtd->enabled) |
| 193 | return 0; |
| 194 | |
| 195 | ret = q6asm_media_format_block(prtd->audio_client, compr->codec); |
| 196 | if (ret < 0) |
| 197 | pr_info("%s: CMD Format block failed\n", __func__); |
| 198 | |
| 199 | atomic_set(&prtd->out_count, runtime->periods); |
| 200 | |
| 201 | prtd->enabled = 1; |
| 202 | prtd->cmd_ack = 0; |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int msm_compr_trigger(struct snd_pcm_substream *substream, int cmd) |
| 208 | { |
| 209 | int ret = 0; |
| 210 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 211 | struct compr_audio *compr = runtime->private_data; |
| 212 | struct msm_audio *prtd = &compr->prtd; |
| 213 | |
| 214 | pr_debug("%s\n", __func__); |
| 215 | switch (cmd) { |
| 216 | case SNDRV_PCM_TRIGGER_START: |
| 217 | prtd->pcm_irq_pos = 0; |
| 218 | case SNDRV_PCM_TRIGGER_RESUME: |
| 219 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 220 | pr_debug("%s: Trigger start\n", __func__); |
| 221 | q6asm_run_nowait(prtd->audio_client, 0, 0, 0); |
| 222 | atomic_set(&prtd->start, 1); |
| 223 | break; |
| 224 | case SNDRV_PCM_TRIGGER_STOP: |
| 225 | pr_debug("SNDRV_PCM_TRIGGER_STOP\n"); |
| 226 | atomic_set(&prtd->start, 0); |
| 227 | break; |
| 228 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 229 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 230 | pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n"); |
| 231 | q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE); |
| 232 | atomic_set(&prtd->start, 0); |
| 233 | break; |
| 234 | default: |
| 235 | ret = -EINVAL; |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | static void populate_codec_list(struct compr_audio *compr, |
| 243 | struct snd_pcm_runtime *runtime) |
| 244 | { |
| 245 | pr_debug("%s\n", __func__); |
| 246 | /* MP3 Block */ |
| 247 | compr->info.compr_cap.num_codecs = 1; |
| 248 | compr->info.compr_cap.min_fragment_size = runtime->hw.period_bytes_min; |
| 249 | compr->info.compr_cap.max_fragment_size = runtime->hw.period_bytes_max; |
| 250 | compr->info.compr_cap.min_fragments = runtime->hw.periods_min; |
| 251 | compr->info.compr_cap.max_fragments = runtime->hw.periods_max; |
| 252 | compr->info.compr_cap.codecs[0] = SND_AUDIOCODEC_MP3; |
| 253 | /* Add new codecs here */ |
| 254 | } |
| 255 | |
| 256 | static int msm_compr_open(struct snd_pcm_substream *substream) |
| 257 | { |
| 258 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 259 | struct snd_soc_pcm_runtime *soc_prtd = substream->private_data; |
| 260 | struct compr_audio *compr; |
| 261 | struct msm_audio *prtd; |
| 262 | int ret = 0; |
| 263 | |
| 264 | /* Capture path */ |
| 265 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | pr_debug("%s\n", __func__); |
| 269 | compr = kzalloc(sizeof(struct compr_audio), GFP_KERNEL); |
| 270 | if (compr == NULL) { |
| 271 | pr_err("Failed to allocate memory for msm_audio\n"); |
| 272 | return -ENOMEM; |
| 273 | } |
| 274 | prtd = &compr->prtd; |
| 275 | prtd->substream = substream; |
| 276 | prtd->audio_client = q6asm_audio_client_alloc( |
| 277 | (app_cb)compr_event_handler, compr); |
| 278 | if (!prtd->audio_client) { |
| 279 | pr_info("%s: Could not allocate memory\n", __func__); |
| 280 | kfree(prtd); |
| 281 | return -ENOMEM; |
| 282 | } |
| 283 | runtime->hw = msm_compr_hardware_playback; |
| 284 | |
| 285 | pr_info("%s: session ID %d\n", __func__, prtd->audio_client->session); |
| 286 | |
| 287 | prtd->session_id = prtd->audio_client->session; |
| 288 | msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->be_id, |
| 289 | prtd->session_id, substream->stream); |
| 290 | |
| 291 | prtd->cmd_ack = 1; |
| 292 | |
| 293 | ret = snd_pcm_hw_constraint_list(runtime, 0, |
| 294 | SNDRV_PCM_HW_PARAM_RATE, |
| 295 | &constraints_sample_rates); |
| 296 | if (ret < 0) |
| 297 | pr_info("snd_pcm_hw_constraint_list failed\n"); |
| 298 | /* Ensure that buffer size is a multiple of period size */ |
| 299 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 300 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 301 | if (ret < 0) |
| 302 | pr_info("snd_pcm_hw_constraint_integer failed\n"); |
| 303 | |
| 304 | prtd->dsp_cnt = 0; |
Asish Bhattacharya | 31bd80d | 2012-01-04 02:30:13 +0530 | [diff] [blame] | 305 | atomic_set(&prtd->pending_buffer, 1); |
Asish Bhattacharya | 305d175 | 2011-11-01 20:38:26 +0530 | [diff] [blame] | 306 | compr->codec = FORMAT_MP3; |
| 307 | populate_codec_list(compr, runtime); |
| 308 | runtime->private_data = compr; |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int msm_compr_playback_close(struct snd_pcm_substream *substream) |
| 314 | { |
| 315 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 316 | struct snd_soc_pcm_runtime *soc_prtd = substream->private_data; |
| 317 | struct compr_audio *compr = runtime->private_data; |
| 318 | struct msm_audio *prtd = &compr->prtd; |
| 319 | int dir = 0; |
| 320 | |
| 321 | pr_debug("%s\n", __func__); |
| 322 | |
| 323 | dir = IN; |
Asish Bhattacharya | 31bd80d | 2012-01-04 02:30:13 +0530 | [diff] [blame] | 324 | atomic_set(&prtd->pending_buffer, 0); |
Asish Bhattacharya | 305d175 | 2011-11-01 20:38:26 +0530 | [diff] [blame] | 325 | q6asm_cmd(prtd->audio_client, CMD_CLOSE); |
| 326 | q6asm_audio_client_buf_free_contiguous(dir, |
| 327 | prtd->audio_client); |
| 328 | |
| 329 | msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->be_id, |
| 330 | SNDRV_PCM_STREAM_PLAYBACK); |
| 331 | q6asm_audio_client_free(prtd->audio_client); |
| 332 | kfree(prtd); |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static int msm_compr_close(struct snd_pcm_substream *substream) |
| 337 | { |
| 338 | int ret = 0; |
| 339 | |
| 340 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 341 | ret = msm_compr_playback_close(substream); |
| 342 | else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 343 | ret = EINVAL; |
| 344 | return ret; |
| 345 | } |
| 346 | static int msm_compr_prepare(struct snd_pcm_substream *substream) |
| 347 | { |
| 348 | int ret = 0; |
| 349 | |
| 350 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 351 | ret = msm_compr_playback_prepare(substream); |
| 352 | else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 353 | ret = EINVAL; |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | static snd_pcm_uframes_t msm_compr_pointer(struct snd_pcm_substream *substream) |
| 358 | { |
| 359 | |
| 360 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 361 | struct compr_audio *compr = runtime->private_data; |
| 362 | struct msm_audio *prtd = &compr->prtd; |
| 363 | |
| 364 | if (prtd->pcm_irq_pos >= prtd->pcm_size) |
| 365 | prtd->pcm_irq_pos = 0; |
| 366 | |
| 367 | pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos); |
| 368 | return bytes_to_frames(runtime, (prtd->pcm_irq_pos)); |
| 369 | } |
| 370 | |
| 371 | static int msm_compr_mmap(struct snd_pcm_substream *substream, |
| 372 | struct vm_area_struct *vma) |
| 373 | { |
| 374 | int result = 0; |
| 375 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 376 | struct compr_audio *compr = runtime->private_data; |
| 377 | struct msm_audio *prtd = &compr->prtd; |
| 378 | |
| 379 | pr_debug("%s\n", __func__); |
| 380 | prtd->mmap_flag = 1; |
| 381 | if (runtime->dma_addr && runtime->dma_bytes) { |
| 382 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 383 | result = remap_pfn_range(vma, vma->vm_start, |
| 384 | runtime->dma_addr >> PAGE_SHIFT, |
| 385 | runtime->dma_bytes, |
| 386 | vma->vm_page_prot); |
| 387 | } else { |
| 388 | pr_err("Physical address or size of buf is NULL"); |
| 389 | return -EINVAL; |
| 390 | } |
| 391 | return result; |
| 392 | } |
| 393 | |
| 394 | static int msm_compr_hw_params(struct snd_pcm_substream *substream, |
| 395 | struct snd_pcm_hw_params *params) |
| 396 | { |
| 397 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 398 | struct compr_audio *compr = runtime->private_data; |
| 399 | struct msm_audio *prtd = &compr->prtd; |
| 400 | struct snd_dma_buffer *dma_buf = &substream->dma_buffer; |
| 401 | struct audio_buffer *buf; |
| 402 | int dir, ret; |
| 403 | |
| 404 | pr_debug("%s\n", __func__); |
| 405 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 406 | dir = IN; |
| 407 | else |
| 408 | return -EINVAL; |
| 409 | |
| 410 | ret = q6asm_open_write(prtd->audio_client, compr->codec); |
| 411 | if (ret < 0) { |
| 412 | pr_err("%s: Session out open failed\n", __func__); |
| 413 | return -ENOMEM; |
| 414 | } |
| 415 | ret = q6asm_set_io_mode(prtd->audio_client, ASYNC_IO_MODE); |
| 416 | if (ret < 0) { |
| 417 | pr_err("%s: Set IO mode failed\n", __func__); |
| 418 | return -ENOMEM; |
| 419 | } |
| 420 | |
| 421 | ret = q6asm_audio_client_buf_alloc_contiguous(dir, |
| 422 | prtd->audio_client, |
| 423 | runtime->hw.period_bytes_min, |
| 424 | runtime->hw.periods_max); |
| 425 | if (ret < 0) { |
| 426 | pr_err("Audio Start: Buffer Allocation failed " |
| 427 | "rc = %d\n", ret); |
| 428 | return -ENOMEM; |
| 429 | } |
| 430 | buf = prtd->audio_client->port[dir].buf; |
| 431 | |
| 432 | pr_debug("%s:buf = %p\n", __func__, buf); |
| 433 | dma_buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 434 | dma_buf->dev.dev = substream->pcm->card->dev; |
| 435 | dma_buf->private_data = NULL; |
| 436 | dma_buf->area = buf[0].data; |
| 437 | dma_buf->addr = buf[0].phys; |
| 438 | dma_buf->bytes = runtime->hw.buffer_bytes_max; |
| 439 | if (!dma_buf->area) |
| 440 | return -ENOMEM; |
| 441 | |
| 442 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int msm_compr_ioctl(struct snd_pcm_substream *substream, |
| 447 | unsigned int cmd, void *arg) |
| 448 | { |
| 449 | int rc = 0; |
| 450 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 451 | struct compr_audio *compr = runtime->private_data; |
| 452 | struct msm_audio *prtd = &compr->prtd; |
Asish Bhattacharya | 8664b25 | 2011-12-30 05:43:23 +0530 | [diff] [blame^] | 453 | uint64_t timestamp; |
| 454 | uint64_t temp; |
Asish Bhattacharya | 305d175 | 2011-11-01 20:38:26 +0530 | [diff] [blame] | 455 | |
| 456 | switch (cmd) { |
Asish Bhattacharya | 8664b25 | 2011-12-30 05:43:23 +0530 | [diff] [blame^] | 457 | case SNDRV_COMPRESS_TSTAMP: { |
| 458 | struct snd_compr_tstamp tstamp; |
| 459 | pr_debug("SNDRV_COMPRESS_TSTAMP\n"); |
| 460 | |
| 461 | memset(&tstamp, 0x0, sizeof(struct snd_compr_tstamp)); |
| 462 | timestamp = q6asm_get_session_time(prtd->audio_client); |
| 463 | if (timestamp < 0) { |
| 464 | pr_err("%s: Get Session Time return value =%lld\n", |
| 465 | __func__, timestamp); |
| 466 | return -EAGAIN; |
| 467 | } |
| 468 | temp = (timestamp * 2 * runtime->channels); |
| 469 | temp = temp * (runtime->rate/1000); |
| 470 | temp = div_u64(temp, 1000); |
| 471 | tstamp.sampling_rate = runtime->rate; |
| 472 | tstamp.rendered = (size_t)(temp & 0xFFFFFFFF); |
| 473 | tstamp.decoded = (size_t)((temp >> 32) & 0xFFFFFFFF); |
| 474 | tstamp.timestamp = timestamp; |
| 475 | pr_debug("%s: bytes_consumed:lsb = %d, msb = %d," |
| 476 | "timestamp = %lld,\n", |
| 477 | __func__, tstamp.rendered, tstamp.decoded, |
| 478 | tstamp.timestamp); |
| 479 | if (copy_to_user((void *) arg, &tstamp, |
| 480 | sizeof(struct snd_compr_tstamp))) |
| 481 | return -EFAULT; |
| 482 | return 0; |
| 483 | } |
Asish Bhattacharya | 305d175 | 2011-11-01 20:38:26 +0530 | [diff] [blame] | 484 | case SNDRV_COMPRESS_GET_CAPS: |
| 485 | pr_debug("SNDRV_COMPRESS_GET_CAPS\n"); |
| 486 | if (copy_to_user((void *) arg, &compr->info.compr_cap, |
| 487 | sizeof(struct snd_compr_caps))) { |
| 488 | rc = -EFAULT; |
| 489 | pr_err("%s: ERROR: copy to user\n", __func__); |
| 490 | return rc; |
| 491 | } |
| 492 | return 0; |
| 493 | case SNDRV_COMPRESS_SET_PARAMS: |
| 494 | pr_debug("SNDRV_COMPRESS_SET_PARAMS: "); |
| 495 | if (copy_from_user(&compr->info.codec_param, (void *) arg, |
| 496 | sizeof(struct snd_compr_params))) { |
| 497 | rc = -EFAULT; |
| 498 | pr_err("%s: ERROR: copy from user\n", __func__); |
| 499 | return rc; |
| 500 | } |
| 501 | switch (compr->info.codec_param.codec.id) { |
| 502 | case SND_AUDIOCODEC_MP3: |
| 503 | /* For MP3 we dont need any other parameter */ |
| 504 | pr_debug("SND_AUDIOCODEC_MP3\n"); |
| 505 | compr->codec = FORMAT_MP3; |
| 506 | break; |
| 507 | default: |
| 508 | pr_debug("FORMAT_LINEAR_PCM\n"); |
| 509 | compr->codec = FORMAT_LINEAR_PCM; |
| 510 | break; |
| 511 | } |
| 512 | return 0; |
| 513 | case SNDRV_PCM_IOCTL1_RESET: |
| 514 | prtd->cmd_ack = 0; |
| 515 | rc = q6asm_cmd(prtd->audio_client, CMD_FLUSH); |
| 516 | if (rc < 0) |
| 517 | pr_err("%s: flush cmd failed rc=%d\n", __func__, rc); |
| 518 | rc = wait_event_timeout(the_locks.eos_wait, |
| 519 | prtd->cmd_ack, 5 * HZ); |
| 520 | if (rc < 0) |
| 521 | pr_err("Flush cmd timeout\n"); |
| 522 | prtd->pcm_irq_pos = 0; |
| 523 | break; |
| 524 | default: |
| 525 | break; |
| 526 | } |
| 527 | return snd_pcm_lib_ioctl(substream, cmd, arg); |
| 528 | } |
| 529 | |
| 530 | static struct snd_pcm_ops msm_compr_ops = { |
| 531 | .open = msm_compr_open, |
| 532 | .hw_params = msm_compr_hw_params, |
| 533 | .close = msm_compr_close, |
| 534 | .ioctl = msm_compr_ioctl, |
| 535 | .prepare = msm_compr_prepare, |
| 536 | .trigger = msm_compr_trigger, |
| 537 | .pointer = msm_compr_pointer, |
| 538 | .mmap = msm_compr_mmap, |
| 539 | }; |
| 540 | |
| 541 | static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd) |
| 542 | { |
| 543 | struct snd_card *card = rtd->card->snd_card; |
| 544 | int ret = 0; |
| 545 | |
| 546 | if (!card->dev->coherent_dma_mask) |
| 547 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | static struct snd_soc_platform_driver msm_soc_platform = { |
| 552 | .ops = &msm_compr_ops, |
| 553 | .pcm_new = msm_asoc_pcm_new, |
| 554 | }; |
| 555 | |
| 556 | static __devinit int msm_compr_probe(struct platform_device *pdev) |
| 557 | { |
| 558 | pr_info("%s: dev name %s\n", __func__, dev_name(&pdev->dev)); |
| 559 | return snd_soc_register_platform(&pdev->dev, |
| 560 | &msm_soc_platform); |
| 561 | } |
| 562 | |
| 563 | static int msm_compr_remove(struct platform_device *pdev) |
| 564 | { |
| 565 | snd_soc_unregister_platform(&pdev->dev); |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static struct platform_driver msm_compr_driver = { |
| 570 | .driver = { |
| 571 | .name = "msm-compr-dsp", |
| 572 | .owner = THIS_MODULE, |
| 573 | }, |
| 574 | .probe = msm_compr_probe, |
| 575 | .remove = __devexit_p(msm_compr_remove), |
| 576 | }; |
| 577 | |
| 578 | static int __init msm_soc_platform_init(void) |
| 579 | { |
| 580 | init_waitqueue_head(&the_locks.enable_wait); |
| 581 | init_waitqueue_head(&the_locks.eos_wait); |
| 582 | init_waitqueue_head(&the_locks.write_wait); |
| 583 | init_waitqueue_head(&the_locks.read_wait); |
| 584 | |
| 585 | return platform_driver_register(&msm_compr_driver); |
| 586 | } |
| 587 | module_init(msm_soc_platform_init); |
| 588 | |
| 589 | static void __exit msm_soc_platform_exit(void) |
| 590 | { |
| 591 | platform_driver_unregister(&msm_compr_driver); |
| 592 | } |
| 593 | module_exit(msm_soc_platform_exit); |
| 594 | |
| 595 | MODULE_DESCRIPTION("PCM module platform driver"); |
| 596 | MODULE_LICENSE("GPL v2"); |