| Jayasena Sangaraboina | 12bf076 | 2012-02-23 20:26:04 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [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 | |
| 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 <linux/dma-mapping.h> |
| 23 | #include <linux/android_pmem.h> |
| 24 | #include <sound/core.h> |
| 25 | #include <sound/soc.h> |
| 26 | #include <sound/soc-dapm.h> |
| 27 | #include <sound/pcm.h> |
| 28 | #include <sound/initval.h> |
| 29 | #include <sound/control.h> |
| 30 | #include <sound/q6adm.h> |
| 31 | #include <asm/dma.h> |
| 32 | #include "msm-pcm-afe.h" |
| 33 | |
| 34 | #define MIN_PERIOD_SIZE (128 * 2) |
| 35 | #define MAX_PERIOD_SIZE (128 * 2 * 2 * 6) |
| 36 | static struct snd_pcm_hardware msm_afe_hardware = { |
| 37 | .info = (SNDRV_PCM_INFO_MMAP | |
| 38 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 39 | SNDRV_PCM_INFO_MMAP_VALID | |
| 40 | SNDRV_PCM_INFO_INTERLEAVED), |
| 41 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 42 | .rates = (SNDRV_PCM_RATE_8000 | |
| 43 | SNDRV_PCM_RATE_16000 | |
| 44 | SNDRV_PCM_RATE_48000), |
| 45 | .rate_min = 8000, |
| 46 | .rate_max = 48000, |
| 47 | .channels_min = 1, |
| 48 | .channels_max = 2, |
| 49 | .buffer_bytes_max = MAX_PERIOD_SIZE * 32, |
| 50 | .period_bytes_min = MIN_PERIOD_SIZE, |
| 51 | .period_bytes_max = MAX_PERIOD_SIZE, |
| 52 | .periods_min = 32, |
| 53 | .periods_max = 384, |
| 54 | .fifo_size = 0, |
| 55 | }; |
| 56 | static enum hrtimer_restart afe_hrtimer_callback(struct hrtimer *hrt); |
| 57 | static enum hrtimer_restart afe_hrtimer_rec_callback(struct hrtimer *hrt); |
| 58 | |
| 59 | static enum hrtimer_restart afe_hrtimer_callback(struct hrtimer *hrt) |
| 60 | { |
| 61 | struct pcm_afe_info *prtd = |
| 62 | container_of(hrt, struct pcm_afe_info, hrt); |
| 63 | struct snd_pcm_substream *substream = prtd->substream; |
| 64 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 65 | if (prtd->start) { |
| 66 | snd_pcm_period_elapsed(prtd->substream); |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 67 | pr_debug("sending frame to DSP: poll_time: %d\n", |
| 68 | prtd->poll_time); |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 69 | if (prtd->dsp_cnt == runtime->periods) |
| 70 | prtd->dsp_cnt = 0; |
| 71 | afe_rt_proxy_port_write( |
| 72 | (prtd->dma_addr + |
| 73 | (prtd->dsp_cnt * |
| 74 | snd_pcm_lib_period_bytes(prtd->substream))), |
| 75 | snd_pcm_lib_period_bytes(prtd->substream)); |
| 76 | prtd->dsp_cnt++; |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 77 | prtd->poll_time = ((unsigned long)(( |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 78 | snd_pcm_lib_period_bytes(prtd->substream) |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 79 | * 1000 * 1000)/(runtime->rate |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 80 | * runtime->channels * 2))); |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 81 | hrtimer_forward_now(hrt, ns_to_ktime(prtd->poll_time |
| 82 | * 1000)); |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 83 | |
| 84 | return HRTIMER_RESTART; |
| 85 | } else |
| 86 | return HRTIMER_NORESTART; |
| 87 | } |
| 88 | static enum hrtimer_restart afe_hrtimer_rec_callback(struct hrtimer *hrt) |
| 89 | { |
| 90 | struct pcm_afe_info *prtd = |
| 91 | container_of(hrt, struct pcm_afe_info, hrt); |
| 92 | struct snd_pcm_substream *substream = prtd->substream; |
| 93 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 94 | if (prtd->start) { |
| 95 | if (prtd->dsp_cnt == runtime->periods) |
| 96 | prtd->dsp_cnt = 0; |
| 97 | afe_rt_proxy_port_read( |
| 98 | (prtd->dma_addr + (prtd->dsp_cnt |
| 99 | * snd_pcm_lib_period_bytes(prtd->substream))), |
| 100 | snd_pcm_lib_period_bytes(prtd->substream)); |
| 101 | prtd->dsp_cnt++; |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 102 | prtd->poll_time = ((unsigned long)(( |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 103 | snd_pcm_lib_period_bytes(prtd->substream) |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 104 | * 1000 * 1000)/(runtime->rate |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 105 | * runtime->channels * 2))); |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 106 | pr_debug("sending frame rec to DSP: poll_time: %d\n", |
| 107 | prtd->poll_time); |
| 108 | hrtimer_forward_now(hrt, ns_to_ktime(prtd->poll_time |
| 109 | * 1000)); |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 110 | |
| 111 | return HRTIMER_RESTART; |
| 112 | } else |
| 113 | return HRTIMER_NORESTART; |
| 114 | } |
| 115 | static void pcm_afe_process_tx_pkt(uint32_t opcode, |
| 116 | uint32_t token, uint32_t *payload, |
| 117 | void *priv) |
| 118 | { |
| 119 | struct pcm_afe_info *prtd = priv; |
| 120 | unsigned long dsp_flags; |
| 121 | struct snd_pcm_substream *substream = NULL; |
| 122 | struct snd_pcm_runtime *runtime = NULL; |
| 123 | uint16_t event; |
| 124 | |
| 125 | if (prtd == NULL) |
| 126 | return; |
| 127 | substream = prtd->substream; |
| 128 | runtime = substream->runtime; |
| 129 | pr_debug("%s\n", __func__); |
| 130 | spin_lock_irqsave(&prtd->dsp_lock, dsp_flags); |
| 131 | switch (opcode) { |
| 132 | case AFE_EVENT_RT_PROXY_PORT_STATUS: { |
| 133 | event = (uint16_t)((0xFFFF0000 & payload[0]) >> 0x10); |
| 134 | switch (event) { |
| 135 | case AFE_EVENT_RTPORT_START: { |
| 136 | prtd->dsp_cnt = 0; |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 137 | prtd->poll_time = ((unsigned long)(( |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 138 | snd_pcm_lib_period_bytes |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 139 | (prtd->substream) * |
| 140 | 1000 * 1000)/ |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 141 | (runtime->rate * |
| 142 | runtime->channels * 2))); |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 143 | pr_info("prtd->poll_time: %d", |
| 144 | prtd->poll_time); |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 145 | hrtimer_start(&prtd->hrt, |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 146 | ns_to_ktime(prtd->poll_time * 1000), |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 147 | HRTIMER_MODE_REL); |
| 148 | break; |
| 149 | } |
| 150 | case AFE_EVENT_RTPORT_STOP: |
| 151 | pr_debug("%s: event!=0\n", __func__); |
| Jayasena Sangaraboina | 12bf076 | 2012-02-23 20:26:04 -0800 | [diff] [blame] | 152 | prtd->start = 0; |
| 153 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 154 | break; |
| 155 | case AFE_EVENT_RTPORT_LOW_WM: |
| 156 | pr_debug("%s: Underrun\n", __func__); |
| 157 | break; |
| 158 | case AFE_EVENT_RTPORT_HI_WM: |
| 159 | pr_debug("%s: Overrun\n", __func__); |
| 160 | break; |
| 161 | default: |
| 162 | break; |
| 163 | } |
| 164 | break; |
| 165 | } |
| 166 | case APR_BASIC_RSP_RESULT: { |
| 167 | switch (payload[0]) { |
| 168 | case AFE_SERVICE_CMD_RTPORT_WR: |
| 169 | pr_debug("write done\n"); |
| 170 | prtd->pcm_irq_pos += snd_pcm_lib_period_bytes |
| 171 | (prtd->substream); |
| 172 | break; |
| 173 | default: |
| 174 | break; |
| 175 | } |
| 176 | break; |
| 177 | } |
| 178 | default: |
| 179 | break; |
| 180 | } |
| 181 | spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags); |
| 182 | } |
| 183 | |
| 184 | static void pcm_afe_process_rx_pkt(uint32_t opcode, |
| 185 | uint32_t token, uint32_t *payload, |
| 186 | void *priv) |
| 187 | { |
| 188 | struct pcm_afe_info *prtd = priv; |
| 189 | unsigned long dsp_flags; |
| 190 | struct snd_pcm_substream *substream = NULL; |
| 191 | struct snd_pcm_runtime *runtime = NULL; |
| 192 | uint16_t event; |
| 193 | |
| 194 | if (prtd == NULL) |
| 195 | return; |
| 196 | substream = prtd->substream; |
| 197 | runtime = substream->runtime; |
| 198 | pr_debug("%s\n", __func__); |
| 199 | spin_lock_irqsave(&prtd->dsp_lock, dsp_flags); |
| 200 | switch (opcode) { |
| 201 | case AFE_EVENT_RT_PROXY_PORT_STATUS: { |
| 202 | event = (uint16_t)((0xFFFF0000 & payload[0]) >> 0x10); |
| 203 | switch (event) { |
| 204 | case AFE_EVENT_RTPORT_START: { |
| 205 | prtd->dsp_cnt = 0; |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 206 | prtd->poll_time = ((unsigned long)(( |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 207 | snd_pcm_lib_period_bytes(prtd->substream) |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 208 | * 1000 * 1000)/(runtime->rate |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 209 | * runtime->channels * 2))); |
| 210 | hrtimer_start(&prtd->hrt, |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 211 | ns_to_ktime(prtd->poll_time * 1000), |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 212 | HRTIMER_MODE_REL); |
| Laxminath Kasam | fa53b9f | 2011-09-22 15:14:18 +0530 | [diff] [blame] | 213 | pr_info("prtd->poll_time : %d", prtd->poll_time); |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | case AFE_EVENT_RTPORT_STOP: |
| 217 | pr_debug("%s: event!=0\n", __func__); |
| Jayasena Sangaraboina | 12bf076 | 2012-02-23 20:26:04 -0800 | [diff] [blame] | 218 | prtd->start = 0; |
| 219 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
| Laxminath Kasam | 32657ec | 2011-08-01 19:26:57 +0530 | [diff] [blame] | 220 | break; |
| 221 | case AFE_EVENT_RTPORT_LOW_WM: |
| 222 | pr_debug("%s: Underrun\n", __func__); |
| 223 | break; |
| 224 | case AFE_EVENT_RTPORT_HI_WM: |
| 225 | pr_debug("%s: Overrun\n", __func__); |
| 226 | break; |
| 227 | default: |
| 228 | break; |
| 229 | } |
| 230 | break; |
| 231 | } |
| 232 | case APR_BASIC_RSP_RESULT: { |
| 233 | switch (payload[0]) { |
| 234 | case AFE_SERVICE_CMD_RTPORT_RD: |
| 235 | pr_debug("Read done\n"); |
| 236 | prtd->pcm_irq_pos += snd_pcm_lib_period_bytes |
| 237 | (prtd->substream); |
| 238 | snd_pcm_period_elapsed(prtd->substream); |
| 239 | break; |
| 240 | default: |
| 241 | break; |
| 242 | } |
| 243 | break; |
| 244 | } |
| 245 | default: |
| 246 | break; |
| 247 | } |
| 248 | spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags); |
| 249 | } |
| 250 | |
| 251 | static int msm_afe_playback_prepare(struct snd_pcm_substream *substream) |
| 252 | { |
| 253 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 254 | struct pcm_afe_info *prtd = runtime->private_data; |
| 255 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 256 | struct snd_soc_dai *dai = rtd->cpu_dai; |
| 257 | int ret = 0; |
| 258 | |
| 259 | pr_debug("%s: sample_rate=%d\n", __func__, runtime->rate); |
| 260 | |
| 261 | pr_debug("%s: dai->id =%x\n", __func__, dai->id); |
| 262 | ret = afe_register_get_events(dai->id, |
| 263 | pcm_afe_process_tx_pkt, prtd); |
| 264 | if (ret < 0) { |
| 265 | pr_err("afe-pcm:register for events failed\n"); |
| 266 | return ret; |
| 267 | } |
| 268 | pr_info("%s:success\n", __func__); |
| 269 | prtd->prepared++; |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | static int msm_afe_capture_prepare(struct snd_pcm_substream *substream) |
| 274 | { |
| 275 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 276 | struct pcm_afe_info *prtd = runtime->private_data; |
| 277 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 278 | struct snd_soc_dai *dai = rtd->cpu_dai; |
| 279 | int ret = 0; |
| 280 | |
| 281 | pr_debug("%s\n", __func__); |
| 282 | |
| 283 | pr_debug("%s: dai->id =%x\n", __func__, dai->id); |
| 284 | ret = afe_register_get_events(dai->id, |
| 285 | pcm_afe_process_rx_pkt, prtd); |
| 286 | if (ret < 0) { |
| 287 | pr_err("afe-pcm:register for events failed\n"); |
| 288 | return ret; |
| 289 | } |
| 290 | pr_info("%s:success\n", __func__); |
| 291 | prtd->prepared++; |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | /* Conventional and unconventional sample rate supported */ |
| 296 | static unsigned int supported_sample_rates[] = { |
| 297 | 8000, 16000, 48000 |
| 298 | }; |
| 299 | |
| 300 | static struct snd_pcm_hw_constraint_list constraints_sample_rates = { |
| 301 | .count = ARRAY_SIZE(supported_sample_rates), |
| 302 | .list = supported_sample_rates, |
| 303 | .mask = 0, |
| 304 | }; |
| 305 | |
| 306 | static int msm_afe_open(struct snd_pcm_substream *substream) |
| 307 | { |
| 308 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 309 | struct pcm_afe_info *prtd = NULL; |
| 310 | int ret = 0; |
| 311 | |
| 312 | prtd = kzalloc(sizeof(struct pcm_afe_info), GFP_KERNEL); |
| 313 | if (prtd == NULL) { |
| 314 | pr_err("Failed to allocate memory for msm_audio\n"); |
| 315 | return -ENOMEM; |
| 316 | } else |
| 317 | pr_debug("prtd %x\n", (unsigned int)prtd); |
| 318 | |
| 319 | mutex_init(&prtd->lock); |
| 320 | spin_lock_init(&prtd->dsp_lock); |
| 321 | prtd->dsp_cnt = 0; |
| 322 | |
| 323 | mutex_lock(&prtd->lock); |
| 324 | |
| 325 | runtime->hw = msm_afe_hardware; |
| 326 | prtd->substream = substream; |
| 327 | runtime->private_data = prtd; |
| 328 | mutex_unlock(&prtd->lock); |
| 329 | hrtimer_init(&prtd->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 330 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 331 | prtd->hrt.function = afe_hrtimer_callback; |
| 332 | else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 333 | prtd->hrt.function = afe_hrtimer_rec_callback; |
| 334 | |
| 335 | ret = snd_pcm_hw_constraint_list(runtime, 0, |
| 336 | SNDRV_PCM_HW_PARAM_RATE, |
| 337 | &constraints_sample_rates); |
| 338 | if (ret < 0) |
| 339 | pr_err("snd_pcm_hw_constraint_list failed\n"); |
| 340 | /* Ensure that buffer size is a multiple of period size */ |
| 341 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 342 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 343 | if (ret < 0) |
| 344 | pr_err("snd_pcm_hw_constraint_integer failed\n"); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int msm_afe_close(struct snd_pcm_substream *substream) |
| 350 | { |
| 351 | int rc = 0; |
| 352 | struct snd_dma_buffer *dma_buf; |
| 353 | struct snd_pcm_runtime *runtime; |
| 354 | struct pcm_afe_info *prtd; |
| 355 | struct snd_soc_pcm_runtime *rtd = NULL; |
| 356 | struct snd_soc_dai *dai = NULL; |
| 357 | int ret = 0; |
| 358 | |
| 359 | pr_debug("%s\n", __func__); |
| 360 | if (substream == NULL) { |
| 361 | pr_err("substream is NULL\n"); |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | rtd = substream->private_data; |
| 365 | dai = rtd->cpu_dai; |
| 366 | runtime = substream->runtime; |
| 367 | prtd = runtime->private_data; |
| 368 | |
| 369 | mutex_lock(&prtd->lock); |
| 370 | |
| 371 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 372 | ret = afe_unregister_get_events(dai->id); |
| 373 | if (ret < 0) |
| 374 | pr_err("AFE unregister for events failed\n"); |
| 375 | } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 376 | ret = afe_unregister_get_events(dai->id); |
| 377 | if (ret < 0) |
| 378 | pr_err("AFE unregister for events failed\n"); |
| 379 | } |
| 380 | hrtimer_cancel(&prtd->hrt); |
| 381 | |
| 382 | rc = afe_cmd_memory_unmap(runtime->dma_addr); |
| 383 | if (rc < 0) |
| 384 | pr_err("AFE memory unmap failed\n"); |
| 385 | |
| 386 | pr_debug("release all buffer\n"); |
| 387 | dma_buf = &substream->dma_buffer; |
| 388 | if (dma_buf == NULL) { |
| 389 | pr_debug("dma_buf is NULL\n"); |
| 390 | goto done; |
| 391 | } |
| 392 | if (dma_buf->area != NULL) { |
| 393 | dma_free_coherent(substream->pcm->card->dev, |
| 394 | runtime->hw.buffer_bytes_max, dma_buf->area, |
| 395 | dma_buf->addr); |
| 396 | dma_buf->area = NULL; |
| 397 | } |
| 398 | done: |
| 399 | pr_debug("%s: dai->id =%x\n", __func__, dai->id); |
| 400 | mutex_unlock(&prtd->lock); |
| 401 | prtd->prepared--; |
| 402 | kfree(prtd); |
| 403 | return 0; |
| 404 | } |
| 405 | static int msm_afe_prepare(struct snd_pcm_substream *substream) |
| 406 | { |
| 407 | int ret = 0; |
| 408 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 409 | struct pcm_afe_info *prtd = runtime->private_data; |
| 410 | |
| 411 | prtd->pcm_irq_pos = 0; |
| 412 | if (prtd->prepared) |
| 413 | return 0; |
| 414 | mutex_lock(&prtd->lock); |
| 415 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 416 | ret = msm_afe_playback_prepare(substream); |
| 417 | else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 418 | ret = msm_afe_capture_prepare(substream); |
| 419 | mutex_unlock(&prtd->lock); |
| 420 | return ret; |
| 421 | } |
| 422 | static int msm_afe_mmap(struct snd_pcm_substream *substream, |
| 423 | struct vm_area_struct *vma) |
| 424 | { |
| 425 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 426 | struct pcm_afe_info *prtd = runtime->private_data; |
| 427 | |
| 428 | pr_debug("%s\n", __func__); |
| 429 | prtd->mmap_flag = 1; |
| 430 | dma_mmap_coherent(substream->pcm->card->dev, vma, |
| 431 | runtime->dma_area, |
| 432 | runtime->dma_addr, |
| 433 | runtime->dma_bytes); |
| 434 | return 0; |
| 435 | } |
| 436 | static int msm_afe_trigger(struct snd_pcm_substream *substream, int cmd) |
| 437 | { |
| 438 | int ret = 0; |
| 439 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 440 | struct pcm_afe_info *prtd = runtime->private_data; |
| 441 | |
| 442 | switch (cmd) { |
| 443 | case SNDRV_PCM_TRIGGER_START: |
| 444 | case SNDRV_PCM_TRIGGER_RESUME: |
| 445 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 446 | pr_debug("%s: SNDRV_PCM_TRIGGER_START\n", __func__); |
| 447 | prtd->start = 1; |
| 448 | break; |
| 449 | case SNDRV_PCM_TRIGGER_STOP: |
| 450 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 451 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 452 | pr_debug("%s: SNDRV_PCM_TRIGGER_STOP\n", __func__); |
| 453 | prtd->start = 0; |
| 454 | break; |
| 455 | default: |
| 456 | ret = -EINVAL; |
| 457 | break; |
| 458 | } |
| 459 | return ret; |
| 460 | } |
| 461 | static int msm_afe_hw_params(struct snd_pcm_substream *substream, |
| 462 | struct snd_pcm_hw_params *params) |
| 463 | { |
| 464 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 465 | struct snd_dma_buffer *dma_buf = &substream->dma_buffer; |
| 466 | struct pcm_afe_info *prtd = runtime->private_data; |
| 467 | int rc; |
| 468 | |
| 469 | pr_debug("%s:\n", __func__); |
| 470 | |
| 471 | mutex_lock(&prtd->lock); |
| 472 | |
| 473 | dma_buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 474 | dma_buf->dev.dev = substream->pcm->card->dev; |
| 475 | dma_buf->private_data = NULL; |
| 476 | dma_buf->area = dma_alloc_coherent(dma_buf->dev.dev, |
| 477 | runtime->hw.buffer_bytes_max, |
| 478 | &dma_buf->addr, GFP_KERNEL); |
| 479 | |
| 480 | pr_debug("%s: dma_buf->area: 0x%p, dma_buf->addr: 0x%x", __func__, |
| 481 | (unsigned int *) dma_buf->area, dma_buf->addr); |
| 482 | if (!dma_buf->area) { |
| 483 | pr_err("%s:MSM AFE memory allocation failed\n", __func__); |
| 484 | mutex_unlock(&prtd->lock); |
| 485 | return -ENOMEM; |
| 486 | } |
| 487 | dma_buf->bytes = runtime->hw.buffer_bytes_max; |
| 488 | memset(dma_buf->area, 0, runtime->hw.buffer_bytes_max); |
| 489 | prtd->dma_addr = (u32) dma_buf->addr; |
| 490 | |
| 491 | mutex_unlock(&prtd->lock); |
| 492 | |
| 493 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 494 | |
| 495 | rc = afe_cmd_memory_map(dma_buf->addr, dma_buf->bytes); |
| 496 | if (rc < 0) |
| 497 | pr_err("fail to map memory to DSP\n"); |
| 498 | |
| 499 | return rc; |
| 500 | } |
| 501 | static snd_pcm_uframes_t msm_afe_pointer(struct snd_pcm_substream *substream) |
| 502 | { |
| 503 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 504 | struct pcm_afe_info *prtd = runtime->private_data; |
| 505 | |
| 506 | if (prtd->pcm_irq_pos >= snd_pcm_lib_buffer_bytes(substream)) |
| 507 | prtd->pcm_irq_pos = 0; |
| 508 | |
| 509 | pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos); |
| 510 | return bytes_to_frames(runtime, (prtd->pcm_irq_pos)); |
| 511 | } |
| 512 | |
| 513 | static struct snd_pcm_ops msm_afe_ops = { |
| 514 | .open = msm_afe_open, |
| 515 | .hw_params = msm_afe_hw_params, |
| 516 | .trigger = msm_afe_trigger, |
| 517 | .close = msm_afe_close, |
| 518 | .prepare = msm_afe_prepare, |
| 519 | .mmap = msm_afe_mmap, |
| 520 | .pointer = msm_afe_pointer, |
| 521 | }; |
| 522 | |
| 523 | |
| 524 | static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd) |
| 525 | { |
| 526 | struct snd_card *card = rtd->card->snd_card; |
| 527 | int ret = 0; |
| 528 | |
| 529 | pr_err("%s\n", __func__); |
| 530 | if (!card->dev->coherent_dma_mask) |
| 531 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | static int msm_afe_afe_probe(struct snd_soc_platform *platform) |
| 536 | { |
| 537 | pr_err("%s\n", __func__); |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static struct snd_soc_platform_driver msm_soc_platform = { |
| 542 | .ops = &msm_afe_ops, |
| 543 | .pcm_new = msm_asoc_pcm_new, |
| 544 | .probe = msm_afe_afe_probe, |
| 545 | }; |
| 546 | |
| 547 | static __devinit int msm_afe_probe(struct platform_device *pdev) |
| 548 | { |
| 549 | pr_info("%s: dev name %s\n", __func__, dev_name(&pdev->dev)); |
| 550 | return snd_soc_register_platform(&pdev->dev, |
| 551 | &msm_soc_platform); |
| 552 | } |
| 553 | |
| 554 | static int msm_afe_remove(struct platform_device *pdev) |
| 555 | { |
| 556 | pr_err("%s\n", __func__); |
| 557 | snd_soc_unregister_platform(&pdev->dev); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static struct platform_driver msm_afe_driver = { |
| 562 | .driver = { |
| 563 | .name = "msm-pcm-afe", |
| 564 | .owner = THIS_MODULE, |
| 565 | }, |
| 566 | .probe = msm_afe_probe, |
| 567 | .remove = __devexit_p(msm_afe_remove), |
| 568 | }; |
| 569 | |
| 570 | static int __init msm_soc_platform_init(void) |
| 571 | { |
| 572 | pr_err("%s\n", __func__); |
| 573 | return platform_driver_register(&msm_afe_driver); |
| 574 | } |
| 575 | module_init(msm_soc_platform_init); |
| 576 | |
| 577 | static void __exit msm_soc_platform_exit(void) |
| 578 | { |
| 579 | pr_err("%s\n", __func__); |
| 580 | platform_driver_unregister(&msm_afe_driver); |
| 581 | } |
| 582 | module_exit(msm_soc_platform_exit); |
| 583 | |
| 584 | MODULE_DESCRIPTION("AFE PCM module platform driver"); |
| 585 | MODULE_LICENSE("GPL v2"); |