Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2012-2017, The Linux Foundation. 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 <linux/dma-mapping.h> |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 23 | |
| 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> |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 30 | #include <asm/dma.h> |
Laxminath Kasam | 605b42f | 2017-08-01 22:02:15 +0530 | [diff] [blame] | 31 | #include <dsp/msm_audio_ion.h> |
| 32 | #include <dsp/q6adm-v2.h> |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 33 | #include "msm-pcm-afe-v2.h" |
| 34 | |
| 35 | #define MIN_PLAYBACK_PERIOD_SIZE (128 * 2) |
| 36 | #define MAX_PLAYBACK_PERIOD_SIZE (128 * 2 * 2 * 6) |
| 37 | #define MIN_PLAYBACK_NUM_PERIODS (4) |
| 38 | #define MAX_PLAYBACK_NUM_PERIODS (384) |
| 39 | |
| 40 | #define MIN_CAPTURE_PERIOD_SIZE (128 * 2) |
| 41 | #define MAX_CAPTURE_PERIOD_SIZE (192 * 2 * 2 * 8 * 4) |
| 42 | #define MIN_CAPTURE_NUM_PERIODS (4) |
| 43 | #define MAX_CAPTURE_NUM_PERIODS (384) |
| 44 | |
| 45 | static struct snd_pcm_hardware msm_afe_hardware_playback = { |
| 46 | .info = (SNDRV_PCM_INFO_MMAP | |
| 47 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 48 | SNDRV_PCM_INFO_MMAP_VALID | |
| 49 | SNDRV_PCM_INFO_INTERLEAVED), |
| 50 | .formats = SNDRV_PCM_FMTBIT_S16_LE| |
| 51 | SNDRV_PCM_FMTBIT_S24_LE, |
| 52 | .rates = (SNDRV_PCM_RATE_8000 | |
| 53 | SNDRV_PCM_RATE_16000 | |
| 54 | SNDRV_PCM_RATE_48000), |
| 55 | .rate_min = 8000, |
| 56 | .rate_max = 48000, |
| 57 | .channels_min = 1, |
| 58 | .channels_max = 6, |
| 59 | .buffer_bytes_max = MAX_PLAYBACK_PERIOD_SIZE * |
| 60 | MAX_PLAYBACK_NUM_PERIODS, |
| 61 | .period_bytes_min = MIN_PLAYBACK_PERIOD_SIZE, |
| 62 | .period_bytes_max = MAX_PLAYBACK_PERIOD_SIZE, |
| 63 | .periods_min = MIN_PLAYBACK_NUM_PERIODS, |
| 64 | .periods_max = MAX_PLAYBACK_NUM_PERIODS, |
| 65 | .fifo_size = 0, |
| 66 | }; |
| 67 | |
| 68 | static struct snd_pcm_hardware msm_afe_hardware_capture = { |
| 69 | .info = (SNDRV_PCM_INFO_MMAP | |
| 70 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 71 | SNDRV_PCM_INFO_MMAP_VALID | |
| 72 | SNDRV_PCM_INFO_INTERLEAVED), |
| 73 | .formats = SNDRV_PCM_FMTBIT_S16_LE| |
| 74 | SNDRV_PCM_FMTBIT_S24_LE, |
| 75 | .rates = (SNDRV_PCM_RATE_8000 | |
| 76 | SNDRV_PCM_RATE_16000 | |
| 77 | SNDRV_PCM_RATE_48000), |
| 78 | .rate_min = 8000, |
| 79 | .rate_max = 48000, |
| 80 | .channels_min = 1, |
| 81 | .channels_max = 6, |
| 82 | .buffer_bytes_max = MAX_CAPTURE_PERIOD_SIZE * |
| 83 | MAX_CAPTURE_NUM_PERIODS, |
| 84 | .period_bytes_min = MIN_CAPTURE_PERIOD_SIZE, |
| 85 | .period_bytes_max = MAX_CAPTURE_PERIOD_SIZE, |
| 86 | .periods_min = MIN_CAPTURE_NUM_PERIODS, |
| 87 | .periods_max = MAX_CAPTURE_NUM_PERIODS, |
| 88 | .fifo_size = 0, |
| 89 | }; |
| 90 | |
| 91 | |
| 92 | static enum hrtimer_restart afe_hrtimer_callback(struct hrtimer *hrt); |
| 93 | static enum hrtimer_restart afe_hrtimer_rec_callback(struct hrtimer *hrt); |
| 94 | |
| 95 | static enum hrtimer_restart afe_hrtimer_callback(struct hrtimer *hrt) |
| 96 | { |
| 97 | struct pcm_afe_info *prtd = |
| 98 | container_of(hrt, struct pcm_afe_info, hrt); |
| 99 | struct snd_pcm_substream *substream = prtd->substream; |
| 100 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 101 | u32 mem_map_handle = 0; |
| 102 | |
| 103 | mem_map_handle = afe_req_mmap_handle(prtd->audio_client); |
| 104 | if (!mem_map_handle) |
| 105 | pr_err("%s: mem_map_handle is NULL\n", __func__); |
| 106 | |
| 107 | if (prtd->start) { |
| 108 | pr_debug("sending frame to DSP: poll_time: %d\n", |
| 109 | prtd->poll_time); |
| 110 | if (prtd->dsp_cnt == runtime->periods) |
| 111 | prtd->dsp_cnt = 0; |
| 112 | pr_debug("%s: mem_map_handle 0x%x\n", __func__, mem_map_handle); |
| 113 | afe_rt_proxy_port_write( |
| 114 | (prtd->dma_addr + |
| 115 | (prtd->dsp_cnt * |
| 116 | snd_pcm_lib_period_bytes(prtd->substream))), mem_map_handle, |
| 117 | snd_pcm_lib_period_bytes(prtd->substream)); |
| 118 | prtd->dsp_cnt++; |
| 119 | hrtimer_forward_now(hrt, ns_to_ktime(prtd->poll_time |
| 120 | * 1000)); |
| 121 | |
| 122 | return HRTIMER_RESTART; |
| 123 | } else |
| 124 | return HRTIMER_NORESTART; |
| 125 | } |
| 126 | static enum hrtimer_restart afe_hrtimer_rec_callback(struct hrtimer *hrt) |
| 127 | { |
| 128 | struct pcm_afe_info *prtd = |
| 129 | container_of(hrt, struct pcm_afe_info, hrt); |
| 130 | struct snd_pcm_substream *substream = prtd->substream; |
| 131 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 132 | u32 mem_map_handle = 0; |
| 133 | int ret; |
| 134 | |
| 135 | mem_map_handle = afe_req_mmap_handle(prtd->audio_client); |
| 136 | if (!mem_map_handle) |
| 137 | pr_err("%s: mem_map_handle is NULL\n", __func__); |
| 138 | |
| 139 | if (prtd->start) { |
| 140 | if (prtd->dsp_cnt == runtime->periods) |
| 141 | prtd->dsp_cnt = 0; |
| 142 | pr_debug("%s: mem_map_handle 0x%x\n", __func__, mem_map_handle); |
| 143 | ret = afe_rt_proxy_port_read( |
| 144 | (prtd->dma_addr + (prtd->dsp_cnt |
| 145 | * snd_pcm_lib_period_bytes(prtd->substream))), mem_map_handle, |
| 146 | snd_pcm_lib_period_bytes(prtd->substream)); |
| 147 | if (ret < 0) { |
| 148 | pr_err("%s: AFE port read fails: %d\n", __func__, ret); |
| 149 | prtd->start = 0; |
| 150 | return HRTIMER_NORESTART; |
| 151 | } |
| 152 | prtd->dsp_cnt++; |
| 153 | pr_debug("sending frame rec to DSP: poll_time: %d\n", |
| 154 | prtd->poll_time); |
| 155 | hrtimer_forward_now(hrt, ns_to_ktime(prtd->poll_time |
| 156 | * 1000)); |
| 157 | |
| 158 | return HRTIMER_RESTART; |
| 159 | } else |
| 160 | return HRTIMER_NORESTART; |
| 161 | } |
| 162 | static void pcm_afe_process_tx_pkt(uint32_t opcode, |
| 163 | uint32_t token, uint32_t *payload, |
| 164 | void *priv) |
| 165 | { |
| 166 | struct pcm_afe_info *prtd = priv; |
| 167 | unsigned long dsp_flags; |
| 168 | struct snd_pcm_substream *substream = NULL; |
| 169 | struct snd_pcm_runtime *runtime = NULL; |
| 170 | uint16_t event; |
| 171 | uint64_t period_bytes; |
| 172 | uint64_t bytes_one_sec; |
| 173 | |
| 174 | if (prtd == NULL) |
| 175 | return; |
| 176 | substream = prtd->substream; |
| 177 | runtime = substream->runtime; |
| 178 | pr_debug("%s\n", __func__); |
| 179 | spin_lock_irqsave(&prtd->dsp_lock, dsp_flags); |
| 180 | switch (opcode) { |
| 181 | case AFE_EVENT_RT_PROXY_PORT_STATUS: { |
| 182 | event = (uint16_t)((0xFFFF0000 & payload[0]) >> 0x10); |
| 183 | switch (event) { |
| 184 | case AFE_EVENT_RTPORT_START: { |
| 185 | prtd->dsp_cnt = 0; |
| 186 | /* Calculate poll time. |
| 187 | * Split steps to avoid overflow. |
| 188 | * Poll time-time corresponding to one period |
| 189 | * in bytes. |
| 190 | * (Samplerate * channelcount * format) = |
| 191 | * bytes in 1 sec. |
| 192 | * Poll time = |
| 193 | * (period bytes / bytes in one sec) * |
| 194 | * 1000000 micro seconds. |
| 195 | * Multiplication by 1000000 is done in two |
| 196 | * steps to keep the accuracy of poll time. |
| 197 | */ |
| 198 | if (prtd->mmap_flag) { |
| 199 | period_bytes = ((uint64_t)( |
| 200 | (snd_pcm_lib_period_bytes( |
| 201 | prtd->substream)) * |
| 202 | 1000)); |
| 203 | bytes_one_sec = (runtime->rate |
| 204 | * runtime->channels * 2); |
| 205 | bytes_one_sec = |
| 206 | div_u64(bytes_one_sec, 1000); |
| 207 | prtd->poll_time = |
| 208 | div_u64(period_bytes, |
| 209 | bytes_one_sec); |
| 210 | pr_debug("prtd->poll_time: %d", |
| 211 | prtd->poll_time); |
| 212 | } |
| 213 | break; |
| 214 | } |
| 215 | case AFE_EVENT_RTPORT_STOP: |
| 216 | pr_debug("%s: event!=0\n", __func__); |
| 217 | prtd->start = 0; |
| 218 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
| 219 | break; |
| 220 | case AFE_EVENT_RTPORT_LOW_WM: |
| 221 | pr_debug("%s: Underrun\n", __func__); |
| 222 | break; |
| 223 | case AFE_EVENT_RTPORT_HI_WM: |
| 224 | pr_debug("%s: Overrun\n", __func__); |
| 225 | break; |
| 226 | default: |
| 227 | break; |
| 228 | } |
| 229 | break; |
| 230 | } |
| 231 | case APR_BASIC_RSP_RESULT: { |
| 232 | switch (payload[0]) { |
| 233 | case AFE_PORT_DATA_CMD_RT_PROXY_PORT_WRITE_V2: |
| 234 | pr_debug("write done\n"); |
| 235 | prtd->pcm_irq_pos += snd_pcm_lib_period_bytes |
| 236 | (prtd->substream); |
| 237 | snd_pcm_period_elapsed(prtd->substream); |
| 238 | break; |
| 239 | default: |
| 240 | break; |
| 241 | } |
| 242 | break; |
| 243 | } |
| 244 | case RESET_EVENTS: |
| 245 | prtd->pcm_irq_pos += snd_pcm_lib_period_bytes |
| 246 | (prtd->substream); |
| 247 | prtd->reset_event = true; |
| 248 | snd_pcm_period_elapsed(prtd->substream); |
| 249 | break; |
| 250 | default: |
| 251 | break; |
| 252 | } |
| 253 | spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags); |
| 254 | } |
| 255 | |
| 256 | static void pcm_afe_process_rx_pkt(uint32_t opcode, |
| 257 | uint32_t token, uint32_t *payload, |
| 258 | void *priv) |
| 259 | { |
| 260 | struct pcm_afe_info *prtd = priv; |
| 261 | unsigned long dsp_flags; |
| 262 | struct snd_pcm_substream *substream = NULL; |
| 263 | struct snd_pcm_runtime *runtime = NULL; |
| 264 | uint16_t event; |
| 265 | uint64_t period_bytes; |
| 266 | uint64_t bytes_one_sec; |
| 267 | uint32_t mem_map_handle = 0; |
| 268 | |
| 269 | if (prtd == NULL) |
| 270 | return; |
| 271 | substream = prtd->substream; |
| 272 | runtime = substream->runtime; |
| 273 | pr_debug("%s\n", __func__); |
| 274 | spin_lock_irqsave(&prtd->dsp_lock, dsp_flags); |
| 275 | switch (opcode) { |
| 276 | case AFE_EVENT_RT_PROXY_PORT_STATUS: { |
| 277 | event = (uint16_t)((0xFFFF0000 & payload[0]) >> 0x10); |
| 278 | switch (event) { |
| 279 | case AFE_EVENT_RTPORT_START: { |
| 280 | prtd->dsp_cnt = 0; |
| 281 | /* Calculate poll time. Split steps to avoid overflow. |
| 282 | * Poll time-time corresponding to one period in bytes. |
| 283 | * (Samplerate * channelcount * format)=bytes in 1 sec. |
| 284 | * Poll time = (period bytes / bytes in one sec) * |
| 285 | * 1000000 micro seconds. |
| 286 | * Multiplication by 1000000 is done in two steps to |
| 287 | * keep the accuracy of poll time. |
| 288 | */ |
| 289 | if (prtd->mmap_flag) { |
| 290 | period_bytes = ((uint64_t)( |
| 291 | (snd_pcm_lib_period_bytes( |
| 292 | prtd->substream)) * 1000)); |
| 293 | bytes_one_sec = (runtime->rate * |
| 294 | runtime->channels * 2); |
| 295 | bytes_one_sec = div_u64(bytes_one_sec, 1000); |
| 296 | prtd->poll_time = |
| 297 | div_u64(period_bytes, bytes_one_sec); |
| 298 | pr_debug("prtd->poll_time : %d\n", |
| 299 | prtd->poll_time); |
| 300 | } else { |
| 301 | mem_map_handle = |
| 302 | afe_req_mmap_handle(prtd->audio_client); |
| 303 | if (!mem_map_handle) |
| 304 | pr_err("%s:mem_map_handle is NULL\n", |
| 305 | __func__); |
| 306 | /* Do initial read to start transfer */ |
| 307 | afe_rt_proxy_port_read((prtd->dma_addr + |
| 308 | (prtd->dsp_cnt * |
| 309 | snd_pcm_lib_period_bytes( |
| 310 | prtd->substream))), |
| 311 | mem_map_handle, |
| 312 | snd_pcm_lib_period_bytes( |
| 313 | prtd->substream)); |
| 314 | prtd->dsp_cnt++; |
| 315 | } |
| 316 | break; |
| 317 | } |
| 318 | case AFE_EVENT_RTPORT_STOP: |
| 319 | pr_debug("%s: event!=0\n", __func__); |
| 320 | prtd->start = 0; |
| 321 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
| 322 | break; |
| 323 | case AFE_EVENT_RTPORT_LOW_WM: |
| 324 | pr_debug("%s: Underrun\n", __func__); |
| 325 | break; |
| 326 | case AFE_EVENT_RTPORT_HI_WM: |
| 327 | pr_debug("%s: Overrun\n", __func__); |
| 328 | break; |
| 329 | default: |
| 330 | break; |
| 331 | } |
| 332 | break; |
| 333 | } |
| 334 | case APR_BASIC_RSP_RESULT: { |
| 335 | switch (payload[0]) { |
| 336 | case AFE_PORT_DATA_CMD_RT_PROXY_PORT_READ_V2: |
| 337 | pr_debug("%s :Read done\n", __func__); |
| 338 | prtd->pcm_irq_pos += snd_pcm_lib_period_bytes |
| 339 | (prtd->substream); |
| 340 | if (!prtd->mmap_flag) { |
| 341 | atomic_set(&prtd->rec_bytes_avail, 1); |
| 342 | wake_up(&prtd->read_wait); |
| 343 | } |
| 344 | snd_pcm_period_elapsed(prtd->substream); |
| 345 | break; |
| 346 | default: |
| 347 | break; |
| 348 | } |
| 349 | break; |
| 350 | } |
| 351 | case RESET_EVENTS: |
| 352 | prtd->pcm_irq_pos += snd_pcm_lib_period_bytes |
| 353 | (prtd->substream); |
| 354 | prtd->reset_event = true; |
| 355 | if (!prtd->mmap_flag) { |
| 356 | atomic_set(&prtd->rec_bytes_avail, 1); |
| 357 | wake_up(&prtd->read_wait); |
| 358 | } |
| 359 | snd_pcm_period_elapsed(prtd->substream); |
| 360 | break; |
| 361 | default: |
| 362 | break; |
| 363 | } |
| 364 | spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags); |
| 365 | } |
| 366 | |
| 367 | static int msm_afe_playback_prepare(struct snd_pcm_substream *substream) |
| 368 | { |
| 369 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 370 | struct pcm_afe_info *prtd = runtime->private_data; |
| 371 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 372 | struct snd_soc_dai *dai = rtd->cpu_dai; |
| 373 | int ret = 0; |
| 374 | |
| 375 | pr_debug("%s: sample_rate=%d\n", __func__, runtime->rate); |
| 376 | |
| 377 | pr_debug("%s: dai->id =%x\n", __func__, dai->id); |
| 378 | ret = afe_register_get_events(dai->id, |
| 379 | pcm_afe_process_tx_pkt, prtd); |
| 380 | if (ret < 0) { |
| 381 | pr_err("afe-pcm:register for events failed\n"); |
| 382 | return ret; |
| 383 | } |
| 384 | pr_debug("%s:success\n", __func__); |
| 385 | prtd->prepared++; |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | static int msm_afe_capture_prepare(struct snd_pcm_substream *substream) |
| 390 | { |
| 391 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 392 | struct pcm_afe_info *prtd = runtime->private_data; |
| 393 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 394 | struct snd_soc_dai *dai = rtd->cpu_dai; |
| 395 | int ret = 0; |
| 396 | |
| 397 | pr_debug("%s\n", __func__); |
| 398 | |
| 399 | pr_debug("%s: dai->id =%x\n", __func__, dai->id); |
| 400 | ret = afe_register_get_events(dai->id, |
| 401 | pcm_afe_process_rx_pkt, prtd); |
| 402 | if (ret < 0) { |
| 403 | pr_err("afe-pcm:register for events failed\n"); |
| 404 | return ret; |
| 405 | } |
| 406 | pr_debug("%s:success\n", __func__); |
| 407 | prtd->prepared++; |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | /* Conventional and unconventional sample rate supported */ |
| 412 | static unsigned int supported_sample_rates[] = { |
| 413 | 8000, 16000, 48000 |
| 414 | }; |
| 415 | |
| 416 | static struct snd_pcm_hw_constraint_list constraints_sample_rates = { |
| 417 | .count = ARRAY_SIZE(supported_sample_rates), |
| 418 | .list = supported_sample_rates, |
| 419 | .mask = 0, |
| 420 | }; |
| 421 | |
| 422 | static int msm_afe_open(struct snd_pcm_substream *substream) |
| 423 | { |
| 424 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 425 | struct pcm_afe_info *prtd = NULL; |
| 426 | int ret = 0; |
| 427 | |
| 428 | prtd = kzalloc(sizeof(struct pcm_afe_info), GFP_KERNEL); |
| 429 | if (prtd == NULL) |
| 430 | return -ENOMEM; |
| 431 | pr_debug("prtd %pK\n", prtd); |
| 432 | |
| 433 | mutex_init(&prtd->lock); |
| 434 | spin_lock_init(&prtd->dsp_lock); |
| 435 | prtd->dsp_cnt = 0; |
| 436 | |
| 437 | mutex_lock(&prtd->lock); |
| 438 | |
| 439 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 440 | runtime->hw = msm_afe_hardware_playback; |
| 441 | else |
| 442 | runtime->hw = msm_afe_hardware_capture; |
| 443 | |
| 444 | prtd->substream = substream; |
| 445 | runtime->private_data = prtd; |
| 446 | prtd->audio_client = q6afe_audio_client_alloc(prtd); |
| 447 | if (!prtd->audio_client) { |
| 448 | pr_debug("%s: Could not allocate memory\n", __func__); |
| 449 | mutex_unlock(&prtd->lock); |
| 450 | kfree(prtd); |
| 451 | return -ENOMEM; |
| 452 | } |
| 453 | |
| 454 | atomic_set(&prtd->rec_bytes_avail, 0); |
| 455 | init_waitqueue_head(&prtd->read_wait); |
| 456 | |
| 457 | hrtimer_init(&prtd->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 458 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 459 | prtd->hrt.function = afe_hrtimer_callback; |
| 460 | else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 461 | prtd->hrt.function = afe_hrtimer_rec_callback; |
| 462 | |
| 463 | mutex_unlock(&prtd->lock); |
| 464 | ret = snd_pcm_hw_constraint_list(runtime, 0, |
| 465 | SNDRV_PCM_HW_PARAM_RATE, |
| 466 | &constraints_sample_rates); |
| 467 | if (ret < 0) |
| 468 | pr_err("snd_pcm_hw_constraint_list failed\n"); |
| 469 | /* Ensure that buffer size is a multiple of period size */ |
| 470 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 471 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 472 | if (ret < 0) |
| 473 | pr_err("snd_pcm_hw_constraint_integer failed\n"); |
| 474 | |
| 475 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 476 | ret = snd_pcm_hw_constraint_minmax(runtime, |
| 477 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, |
| 478 | MIN_CAPTURE_NUM_PERIODS * MIN_CAPTURE_PERIOD_SIZE, |
| 479 | MAX_CAPTURE_NUM_PERIODS * MAX_CAPTURE_PERIOD_SIZE); |
| 480 | |
| 481 | if (ret < 0) { |
| 482 | pr_err("constraint for buffer bytes min max ret = %d\n", |
| 483 | ret); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | prtd->reset_event = false; |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static int msm_afe_playback_copy(struct snd_pcm_substream *substream, |
| 492 | int channel, snd_pcm_uframes_t hwoff, |
| 493 | void __user *buf, snd_pcm_uframes_t frames) |
| 494 | { |
| 495 | int ret = 0; |
| 496 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 497 | struct pcm_afe_info *prtd = runtime->private_data; |
| 498 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); |
| 499 | u32 mem_map_handle = 0; |
| 500 | |
| 501 | pr_debug("%s : appl_ptr 0x%lx hw_ptr 0x%lx dest_to_copy 0x%pK\n", |
| 502 | __func__, |
| 503 | runtime->control->appl_ptr, runtime->status->hw_ptr, hwbuf); |
| 504 | |
| 505 | if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames))) { |
| 506 | pr_err("%s :Failed to copy audio from user buffer\n", |
| 507 | __func__); |
| 508 | |
| 509 | ret = -EFAULT; |
| 510 | goto fail; |
| 511 | } |
| 512 | |
| 513 | if (!prtd->mmap_flag) { |
| 514 | mem_map_handle = afe_req_mmap_handle(prtd->audio_client); |
| 515 | if (!mem_map_handle) { |
| 516 | pr_err("%s: mem_map_handle is NULL\n", __func__); |
| 517 | ret = -EFAULT; |
| 518 | goto fail; |
| 519 | } |
| 520 | |
| 521 | pr_debug("%s : prtd-> dma_addr 0x%lx dsp_cnt %d\n", __func__, |
| 522 | prtd->dma_addr, prtd->dsp_cnt); |
| 523 | |
| 524 | if (prtd->dsp_cnt == runtime->periods) |
| 525 | prtd->dsp_cnt = 0; |
| 526 | |
| 527 | ret = afe_rt_proxy_port_write( |
| 528 | (prtd->dma_addr + (prtd->dsp_cnt * |
| 529 | snd_pcm_lib_period_bytes(prtd->substream))), |
| 530 | mem_map_handle, |
| 531 | snd_pcm_lib_period_bytes(prtd->substream)); |
| 532 | |
| 533 | if (ret) { |
| 534 | pr_err("%s: AFE proxy port write failed %d\n", |
| 535 | __func__, ret); |
| 536 | goto fail; |
| 537 | } |
| 538 | prtd->dsp_cnt++; |
| 539 | } |
| 540 | fail: |
| 541 | return ret; |
| 542 | } |
| 543 | |
| 544 | static int msm_afe_capture_copy(struct snd_pcm_substream *substream, |
| 545 | int channel, snd_pcm_uframes_t hwoff, |
| 546 | void __user *buf, snd_pcm_uframes_t frames) |
| 547 | { |
| 548 | int ret = 0; |
| 549 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 550 | struct pcm_afe_info *prtd = runtime->private_data; |
| 551 | char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff); |
| 552 | u32 mem_map_handle = 0; |
| 553 | |
| 554 | if (!prtd->mmap_flag) { |
| 555 | mem_map_handle = afe_req_mmap_handle(prtd->audio_client); |
| 556 | |
| 557 | if (!mem_map_handle) { |
| 558 | pr_err("%s: mem_map_handle is NULL\n", __func__); |
| 559 | ret = -EFAULT; |
| 560 | goto fail; |
| 561 | } |
| 562 | |
| 563 | if (prtd->dsp_cnt == runtime->periods) |
| 564 | prtd->dsp_cnt = 0; |
| 565 | |
| 566 | ret = afe_rt_proxy_port_read((prtd->dma_addr + |
| 567 | (prtd->dsp_cnt * |
| 568 | snd_pcm_lib_period_bytes(prtd->substream))), |
| 569 | mem_map_handle, |
| 570 | snd_pcm_lib_period_bytes(prtd->substream)); |
| 571 | |
| 572 | if (ret) { |
| 573 | pr_err("%s: AFE proxy port read failed %d\n", |
| 574 | __func__, ret); |
| 575 | goto fail; |
| 576 | } |
| 577 | |
| 578 | prtd->dsp_cnt++; |
| 579 | ret = wait_event_timeout(prtd->read_wait, |
| 580 | atomic_read(&prtd->rec_bytes_avail), 5 * HZ); |
| 581 | if (ret < 0) { |
| 582 | pr_err("%s: wait_event_timeout failed\n", __func__); |
| 583 | |
| 584 | ret = -ETIMEDOUT; |
| 585 | goto fail; |
| 586 | } |
| 587 | atomic_set(&prtd->rec_bytes_avail, 0); |
| 588 | } |
| 589 | pr_debug("%s:appl_ptr 0x%lx hw_ptr 0x%lx src_to_copy 0x%pK\n", |
| 590 | __func__, runtime->control->appl_ptr, |
| 591 | runtime->status->hw_ptr, hwbuf); |
| 592 | |
| 593 | if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames))) { |
| 594 | pr_err("%s: copy to user failed\n", __func__); |
| 595 | |
| 596 | goto fail; |
| 597 | ret = -EFAULT; |
| 598 | } |
| 599 | |
| 600 | fail: |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | static int msm_afe_copy(struct snd_pcm_substream *substream, int channel, |
| 605 | snd_pcm_uframes_t hwoff, void __user *buf, |
| 606 | snd_pcm_uframes_t frames) |
| 607 | { |
| 608 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 609 | struct pcm_afe_info *prtd = runtime->private_data; |
| 610 | |
| 611 | int ret = 0; |
| 612 | |
| 613 | if (prtd->reset_event) { |
| 614 | pr_debug("%s: reset events received from ADSP, return error\n", |
| 615 | __func__); |
| 616 | return -ENETRESET; |
| 617 | } |
| 618 | |
| 619 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 620 | ret = msm_afe_playback_copy(substream, channel, hwoff, |
| 621 | buf, frames); |
| 622 | else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 623 | ret = msm_afe_capture_copy(substream, channel, hwoff, |
| 624 | buf, frames); |
| 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | static int msm_afe_close(struct snd_pcm_substream *substream) |
| 629 | { |
| 630 | int rc = 0; |
| 631 | struct snd_dma_buffer *dma_buf; |
| 632 | struct snd_pcm_runtime *runtime; |
| 633 | struct pcm_afe_info *prtd; |
| 634 | struct snd_soc_pcm_runtime *rtd = NULL; |
| 635 | struct snd_soc_dai *dai = NULL; |
| 636 | int dir = IN; |
| 637 | int ret = 0; |
| 638 | |
| 639 | pr_debug("%s\n", __func__); |
| 640 | if (substream == NULL) { |
| 641 | pr_err("substream is NULL\n"); |
| 642 | return -EINVAL; |
| 643 | } |
| 644 | rtd = substream->private_data; |
| 645 | dai = rtd->cpu_dai; |
| 646 | runtime = substream->runtime; |
| 647 | prtd = runtime->private_data; |
| 648 | |
| 649 | mutex_lock(&prtd->lock); |
| 650 | |
| 651 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 652 | dir = IN; |
| 653 | ret = afe_unregister_get_events(dai->id); |
| 654 | if (ret < 0) |
| 655 | pr_err("AFE unregister for events failed\n"); |
| 656 | } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 657 | dir = OUT; |
| 658 | ret = afe_unregister_get_events(dai->id); |
| 659 | if (ret < 0) |
| 660 | pr_err("AFE unregister for events failed\n"); |
| 661 | } |
| 662 | if (prtd->mmap_flag) |
| 663 | hrtimer_cancel(&prtd->hrt); |
| 664 | |
| 665 | rc = afe_cmd_memory_unmap(afe_req_mmap_handle(prtd->audio_client)); |
| 666 | if (rc < 0) |
| 667 | pr_err("AFE memory unmap failed\n"); |
| 668 | |
| 669 | pr_debug("release all buffer\n"); |
| 670 | dma_buf = &substream->dma_buffer; |
| 671 | if (dma_buf == NULL) { |
| 672 | pr_debug("dma_buf is NULL\n"); |
| 673 | goto done; |
| 674 | } |
| 675 | |
| 676 | if (dma_buf->area) |
| 677 | dma_buf->area = NULL; |
| 678 | q6afe_audio_client_buf_free_contiguous(dir, prtd->audio_client); |
| 679 | done: |
| 680 | pr_debug("%s: dai->id =%x\n", __func__, dai->id); |
| 681 | q6afe_audio_client_free(prtd->audio_client); |
| 682 | mutex_unlock(&prtd->lock); |
| 683 | prtd->prepared--; |
| 684 | kfree(prtd); |
| 685 | runtime->private_data = NULL; |
| 686 | return 0; |
| 687 | } |
| 688 | static int msm_afe_prepare(struct snd_pcm_substream *substream) |
| 689 | { |
| 690 | int ret = 0; |
| 691 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 692 | struct pcm_afe_info *prtd = runtime->private_data; |
| 693 | |
| 694 | prtd->pcm_irq_pos = 0; |
| 695 | if (prtd->prepared) |
| 696 | return 0; |
| 697 | mutex_lock(&prtd->lock); |
| 698 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 699 | ret = msm_afe_playback_prepare(substream); |
| 700 | else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 701 | ret = msm_afe_capture_prepare(substream); |
| 702 | mutex_unlock(&prtd->lock); |
| 703 | return ret; |
| 704 | } |
| 705 | static int msm_afe_mmap(struct snd_pcm_substream *substream, |
| 706 | struct vm_area_struct *vma) |
| 707 | { |
| 708 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 709 | struct pcm_afe_info *prtd = runtime->private_data; |
| 710 | struct afe_audio_client *ac = prtd->audio_client; |
| 711 | struct afe_audio_port_data *apd = ac->port; |
| 712 | struct afe_audio_buffer *ab; |
| 713 | int dir = -1; |
| 714 | |
| 715 | pr_debug("%s\n", __func__); |
| 716 | prtd->mmap_flag = 1; |
| 717 | |
| 718 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 719 | dir = IN; |
| 720 | else |
| 721 | dir = OUT; |
| 722 | ab = &(apd[dir].buf[0]); |
| 723 | |
| 724 | return msm_audio_ion_mmap((struct audio_buffer *)ab, vma); |
| 725 | } |
| 726 | static int msm_afe_trigger(struct snd_pcm_substream *substream, int cmd) |
| 727 | { |
| 728 | int ret = 0; |
| 729 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 730 | struct pcm_afe_info *prtd = runtime->private_data; |
| 731 | |
| 732 | switch (cmd) { |
| 733 | case SNDRV_PCM_TRIGGER_START: |
| 734 | case SNDRV_PCM_TRIGGER_RESUME: |
| 735 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 736 | pr_debug("%s: SNDRV_PCM_TRIGGER_START\n", __func__); |
| 737 | prtd->start = 1; |
| 738 | if (prtd->mmap_flag) |
| 739 | hrtimer_start(&prtd->hrt, ns_to_ktime(0), |
| 740 | HRTIMER_MODE_REL); |
| 741 | break; |
| 742 | case SNDRV_PCM_TRIGGER_STOP: |
| 743 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 744 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 745 | pr_debug("%s: SNDRV_PCM_TRIGGER_STOP\n", __func__); |
| 746 | prtd->start = 0; |
| 747 | break; |
| 748 | default: |
| 749 | ret = -EINVAL; |
| 750 | break; |
| 751 | } |
| 752 | return ret; |
| 753 | } |
| 754 | static int msm_afe_hw_params(struct snd_pcm_substream *substream, |
| 755 | struct snd_pcm_hw_params *params) |
| 756 | { |
| 757 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 758 | struct snd_dma_buffer *dma_buf = &substream->dma_buffer; |
| 759 | struct pcm_afe_info *prtd = runtime->private_data; |
| 760 | struct afe_audio_buffer *buf; |
| 761 | int dir, rc; |
| 762 | |
| 763 | pr_debug("%s:\n", __func__); |
| 764 | |
| 765 | mutex_lock(&prtd->lock); |
| 766 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 767 | dir = IN; |
| 768 | else |
| 769 | dir = OUT; |
| 770 | |
| 771 | rc = q6afe_audio_client_buf_alloc_contiguous(dir, |
| 772 | prtd->audio_client, |
| 773 | (params_buffer_bytes(params) / params_periods(params)), |
| 774 | params_periods(params)); |
| 775 | pr_debug("params_buffer_bytes(params) = %d\n", |
| 776 | (params_buffer_bytes(params))); |
| 777 | pr_debug("params_periods(params) = %d\n", |
| 778 | (params_periods(params))); |
| 779 | pr_debug("params_periodsize(params) = %d\n", |
| 780 | (params_buffer_bytes(params) / params_periods(params))); |
| 781 | |
| 782 | if (rc < 0) { |
| 783 | pr_err("Audio Start: Buffer Allocation failed rc = %d\n", rc); |
| 784 | mutex_unlock(&prtd->lock); |
| 785 | return -ENOMEM; |
| 786 | } |
| 787 | buf = prtd->audio_client->port[dir].buf; |
| 788 | |
| 789 | if (buf == NULL || buf[0].data == NULL) { |
| 790 | mutex_unlock(&prtd->lock); |
| 791 | return -ENOMEM; |
| 792 | } |
| 793 | |
| 794 | pr_debug("%s:buf = %pK\n", __func__, buf); |
| 795 | dma_buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 796 | dma_buf->dev.dev = substream->pcm->card->dev; |
| 797 | dma_buf->private_data = NULL; |
| 798 | dma_buf->area = buf[0].data; |
| 799 | dma_buf->addr = buf[0].phys; |
| 800 | |
| 801 | dma_buf->bytes = params_buffer_bytes(params); |
| 802 | |
| 803 | if (!dma_buf->area) { |
| 804 | pr_err("%s:MSM AFE physical memory allocation failed\n", |
| 805 | __func__); |
| 806 | mutex_unlock(&prtd->lock); |
| 807 | return -ENOMEM; |
| 808 | } |
| 809 | |
| 810 | memset(dma_buf->area, 0, params_buffer_bytes(params)); |
| 811 | |
| 812 | prtd->dma_addr = (phys_addr_t) dma_buf->addr; |
| 813 | |
| 814 | mutex_unlock(&prtd->lock); |
| 815 | |
| 816 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 817 | |
| 818 | rc = afe_memory_map(dma_buf->addr, dma_buf->bytes, prtd->audio_client); |
| 819 | if (rc < 0) |
| 820 | pr_err("fail to map memory to DSP\n"); |
| 821 | |
| 822 | return rc; |
| 823 | } |
| 824 | static snd_pcm_uframes_t msm_afe_pointer(struct snd_pcm_substream *substream) |
| 825 | { |
| 826 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 827 | struct pcm_afe_info *prtd = runtime->private_data; |
| 828 | |
| 829 | if (prtd->pcm_irq_pos >= snd_pcm_lib_buffer_bytes(substream)) |
| 830 | prtd->pcm_irq_pos = 0; |
| 831 | |
| 832 | if (prtd->reset_event) { |
| 833 | pr_debug("%s: reset events received from ADSP, return XRUN\n", |
| 834 | __func__); |
| 835 | return SNDRV_PCM_POS_XRUN; |
| 836 | } |
| 837 | |
| 838 | pr_debug("pcm_irq_pos = %d\n", prtd->pcm_irq_pos); |
| 839 | return bytes_to_frames(runtime, (prtd->pcm_irq_pos)); |
| 840 | } |
| 841 | |
| 842 | static const struct snd_pcm_ops msm_afe_ops = { |
| 843 | .open = msm_afe_open, |
| 844 | .copy = msm_afe_copy, |
| 845 | .hw_params = msm_afe_hw_params, |
| 846 | .trigger = msm_afe_trigger, |
| 847 | .close = msm_afe_close, |
| 848 | .prepare = msm_afe_prepare, |
| 849 | .mmap = msm_afe_mmap, |
| 850 | .pointer = msm_afe_pointer, |
| 851 | }; |
| 852 | |
| 853 | |
| 854 | static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd) |
| 855 | { |
| 856 | struct snd_card *card = rtd->card->snd_card; |
| 857 | int ret = 0; |
| 858 | |
| 859 | pr_debug("%s\n", __func__); |
| 860 | if (!card->dev->coherent_dma_mask) |
| 861 | card->dev->coherent_dma_mask = DMA_BIT_MASK(32); |
| 862 | return ret; |
| 863 | } |
| 864 | |
| 865 | static int msm_afe_afe_probe(struct snd_soc_platform *platform) |
| 866 | { |
| 867 | pr_debug("%s\n", __func__); |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | static struct snd_soc_platform_driver msm_soc_platform = { |
| 872 | .ops = &msm_afe_ops, |
| 873 | .pcm_new = msm_asoc_pcm_new, |
| 874 | .probe = msm_afe_afe_probe, |
| 875 | }; |
| 876 | |
| 877 | static int msm_afe_probe(struct platform_device *pdev) |
| 878 | { |
| 879 | |
| 880 | pr_debug("%s: dev name %s\n", __func__, dev_name(&pdev->dev)); |
| 881 | return snd_soc_register_platform(&pdev->dev, |
| 882 | &msm_soc_platform); |
| 883 | } |
| 884 | |
| 885 | static int msm_afe_remove(struct platform_device *pdev) |
| 886 | { |
| 887 | pr_debug("%s\n", __func__); |
| 888 | snd_soc_unregister_platform(&pdev->dev); |
| 889 | return 0; |
| 890 | } |
| 891 | static const struct of_device_id msm_pcm_afe_dt_match[] = { |
| 892 | {.compatible = "qcom,msm-pcm-afe"}, |
| 893 | {} |
| 894 | }; |
| 895 | MODULE_DEVICE_TABLE(of, msm_pcm_afe_dt_match); |
| 896 | |
| 897 | static struct platform_driver msm_afe_driver = { |
| 898 | .driver = { |
| 899 | .name = "msm-pcm-afe", |
| 900 | .owner = THIS_MODULE, |
| 901 | .of_match_table = msm_pcm_afe_dt_match, |
| 902 | }, |
| 903 | .probe = msm_afe_probe, |
| 904 | .remove = msm_afe_remove, |
| 905 | }; |
| 906 | |
Laxminath Kasam | 8b1366a | 2017-10-05 01:44:16 +0530 | [diff] [blame] | 907 | int __init msm_pcm_afe_init(void) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 908 | { |
| 909 | pr_debug("%s\n", __func__); |
| 910 | return platform_driver_register(&msm_afe_driver); |
| 911 | } |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 912 | |
Asish Bhattacharya | 5faacb3 | 2017-12-04 17:23:15 +0530 | [diff] [blame] | 913 | void msm_pcm_afe_exit(void) |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 914 | { |
| 915 | pr_debug("%s\n", __func__); |
| 916 | platform_driver_unregister(&msm_afe_driver); |
| 917 | } |
Asish Bhattacharya | 8e2277f | 2017-07-20 18:31:55 +0530 | [diff] [blame] | 918 | |
| 919 | MODULE_DESCRIPTION("AFE PCM module platform driver"); |
| 920 | MODULE_LICENSE("GPL v2"); |