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