summaryrefslogtreecommitdiff
path: root/alsa/aoc_alsa_voip.c
blob: f6200ddb38430f69c1183855508e60da55d8ae00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Google Whitechapel AoC ALSA Driver on PCM
 *
 * Copyright (c) 2019 Google LLC
 *
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/version.h>

#include "aoc_alsa.h"
#include "aoc_alsa_drv.h"

#include <linux/hrtimer.h>
#include <linux/ktime.h>

/* Hardware definition
 * TODO: different pcm may have different hardware setup,
 * considering deep buffer and compressed offload buffer
 */
static struct snd_pcm_hardware snd_aoc_playback_hw = {
	.info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP |
		 SNDRV_PCM_INFO_MMAP_VALID),
	.formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
		   SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_FLOAT_LE,
	.rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
	.rate_min = 8000,
	.rate_max = 48000,
	.channels_min = 1,
	.channels_max = 4,
	.buffer_bytes_max = 16384,
	.period_bytes_min = 16,
	.period_bytes_max = 7680,
	.periods_min = 2,
	.periods_max = 128,
};

static bool aoc_voip_support_interrupt(uint8_t mbox_index)
{
	return (mbox_index == VOIP_CHANNEL);
}

static enum hrtimer_restart aoc_voip_irq_process(struct aoc_alsa_stream *alsa_stream)
{
	struct aoc_service_dev *dev;
	unsigned long consumed; /* TODO: uint64_t? */

	/* The number of bytes read/writtien should be the bytes in the buffer
	 * already played out in the case of playback. But this may not be true
	 * in the AoC ring buffer implementation, since the reader pointer in
	 * the playback case represents what has been read from the buffer,
	 * not what already played out .
	*/
	if (alsa_stream->dev == NULL)
		return HRTIMER_RESTART;

	dev = alsa_stream->dev;
	consumed = ((alsa_stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
				  aoc_ring_bytes_read(dev->service, AOC_DOWN) :
				  aoc_ring_bytes_written(dev->service, AOC_UP));

	dev_dbg(&(dev->dev), "consumed = %ld , hw_ptr_base =%ld\n", consumed,
		alsa_stream->hw_ptr_base);

	/* TODO: To do more on no pointer update? */
	if (consumed == alsa_stream->prev_consumed)
		return HRTIMER_RESTART;

	/* To deal with overlfow in Tx or Rx in int32_t */
	if (consumed < alsa_stream->prev_consumed) {
		alsa_stream->n_overflow++;
		dev_notice(&(dev->dev), "overflow in Tx/Rx: %ld - %ld - %d times\n", consumed,
			   alsa_stream->prev_consumed, alsa_stream->n_overflow);
	}
	alsa_stream->prev_consumed = consumed;

	/* Update the pcm pointer  */
	if (unlikely(alsa_stream->n_overflow)) {
		alsa_stream->pos = (consumed + 0x100000000 * alsa_stream->n_overflow -
				    alsa_stream->hw_ptr_base) %
				   alsa_stream->buffer_size;
	} else {
		alsa_stream->pos = (consumed - alsa_stream->hw_ptr_base) % alsa_stream->buffer_size;
	}

	/* Do not queue a work if the cancel_work is active */
	if (atomic_read(&alsa_stream->cancel_work_active) > 0
			|| alsa_stream->voip_period_wq == NULL)
		return HRTIMER_RESTART;

	if (!queue_work(alsa_stream->voip_period_wq, &alsa_stream->pcm_period_work)) {
		wake_up(&alsa_stream->substream->runtime->sleep);
		wake_up(&alsa_stream->substream->runtime->tsleep);
		alsa_stream->wq_busy_count++;

		if (!(alsa_stream->wq_busy_count % 5))
			pr_warn("voip period work busy count = %d\n", alsa_stream->wq_busy_count);
	} else
		alsa_stream->wq_busy_count = 0;

	return HRTIMER_RESTART;
}

static enum hrtimer_restart aoc_voip_hrtimer_irq_handler(struct hrtimer *timer)
{
	struct aoc_alsa_stream *alsa_stream;

	WARN_ON(!timer);
	alsa_stream = container_of(timer, struct aoc_alsa_stream, hr_timer);

	WARN_ON(!alsa_stream || !alsa_stream->substream);

	/* Start the timer immediately for next period */
	/* aoc_timer_start(alsa_stream); */
	aoc_timer_restart(alsa_stream);

	return aoc_voip_irq_process(alsa_stream);
}

void aoc_voip_isr(struct aoc_service_dev *dev)
{
	struct aoc_alsa_stream *alsa_stream;

	if (!dev) {
		pr_err("ERR: NULL aoc service pointer\n");
		return;
	}

	alsa_stream = dev->prvdata;

	if (alsa_stream == NULL)
		return;

	if (alsa_stream->substream == NULL) {
		pr_err("ERR: NULL alsa_stream->substream pointer\n");
		return;
	}

	aoc_voip_irq_process(alsa_stream);
}

static void snd_aoc_pcm_free(struct snd_pcm_runtime *runtime)
{
	pr_debug("Freeing up alsa stream here ..\n");

	kfree(runtime->private_data);
	runtime->private_data = NULL;
}

/* PCM open callback */
static int snd_aoc_pcm_open(struct snd_soc_component *component,
			    struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_card *card = rtd->card;
	struct aoc_chip *chip = snd_soc_card_get_drvdata(card);
	struct snd_pcm_runtime *runtime = substream->runtime;

	struct aoc_alsa_stream *alsa_stream = NULL;
	struct aoc_service_dev *dev = NULL;
	int idx;
	int err;

	dev_dbg(component->dev, "stream (%d)\n", substream->number); /* Playback or capture */
	if (mutex_lock_interruptible(&chip->audio_mutex)) {
		dev_err(component->dev, "ERR: interrupted whilst waiting for lock\n");
		return -EINTR;
	}

	idx = substream->pcm->device;
	dev_dbg(component->dev, "pcm device open (%d)\n", idx);
	dev_dbg(component->dev, "chip open (%llu)\n", chip->opened);

	/* Find the corresponding aoc audio service */
	err = alloc_aoc_audio_service(rtd->dai_link->name, &dev, NULL, NULL);
	if (err < 0) {
		dev_err(component->dev, "ERR:%d fail to alloc service for %s", err,
			rtd->dai_link->name);
		goto out;
	}

	alsa_stream = kzalloc(sizeof(struct aoc_alsa_stream), GFP_KERNEL);
	if (alsa_stream == NULL) {
		err = -ENOMEM;
		dev_err(component->dev, "ERR: fail to alloc alsa_stream for %s",
			rtd->dai_link->name);
		goto out;
	}

	/* Initialise alsa_stream */
	alsa_stream->chip = chip;
	alsa_stream->substream = substream;
	alsa_stream->cstream = NULL;
	alsa_stream->dev = dev;
	alsa_stream->idx = idx;
	alsa_stream->wq_busy_count = 0;
	atomic_set(&alsa_stream->cancel_work_active, 0);

	INIT_WORK(&alsa_stream->pcm_period_work, aoc_pcm_period_work_handler);
	alsa_stream->voip_period_wq = alloc_ordered_workqueue("alsa_voip_period_work", WQ_HIGHPRI);
	if (!alsa_stream->voip_period_wq) {
		err = -ENOMEM;
		pr_err("ERR: fail to alloc workqueue for %s", rtd->dai_link->name);
		goto out;
	}

	/* Ring buffer will be flushed at prepare() before playback/capture */
	alsa_stream->hw_ptr_base = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
						 aoc_ring_bytes_read(dev->service, AOC_DOWN) :
						 aoc_ring_bytes_written(dev->service, AOC_UP);
	alsa_stream->prev_consumed = alsa_stream->hw_ptr_base;
	alsa_stream->n_overflow = 0;

	err = aoc_audio_open(alsa_stream);
	if (err != 0) {
		pr_err("ERR: fail to audio open for %s", rtd->dai_link->name);
		goto out;
	}
	runtime->private_data = alsa_stream;
	runtime->private_free = snd_aoc_pcm_free;
	runtime->hw = snd_aoc_playback_hw;
	chip->alsa_stream[idx] = alsa_stream;
	chip->opened |= (1 << idx);
	alsa_stream->open = 1;
	alsa_stream->draining = 1;

	if (aoc_voip_support_interrupt(alsa_stream->dev->mbox_index)) {
		dev->prvdata = alsa_stream;
		alsa_stream->isr_type = INTR;
	} else {
		alsa_stream->timer_interval_ns = PCM_TIMER_INTERVAL_NANOSECS;
		hrtimer_init(&(alsa_stream->hr_timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
		alsa_stream->hr_timer.function = &aoc_voip_hrtimer_irq_handler;
		alsa_stream->isr_type = TIMER;
	}

	/* TODO: refactor needed on mapping between device number and entrypoint */
	alsa_stream->entry_point_idx = (idx == 7) ? HAPTICS : idx;
	mutex_unlock(&chip->audio_mutex);

	return 0;
out:
	if (alsa_stream) {
		if (alsa_stream->voip_period_wq) {
			flush_workqueue(alsa_stream->voip_period_wq);
			destroy_workqueue(alsa_stream->voip_period_wq);
			alsa_stream->voip_period_wq = NULL;
		}
		kfree(alsa_stream);
	}

	if (dev) {
		free_aoc_audio_service(rtd->dai_link->name, dev);
		dev = NULL;
	}
	mutex_unlock(&chip->audio_mutex);

	dev_dbg(component->dev, "pcm open err=%d\n", err);
	return err;
}

/* Close callback */
static int snd_aoc_pcm_close(struct snd_soc_component *component,
			     struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct aoc_alsa_stream *alsa_stream = runtime->private_data;
	struct aoc_chip *chip = alsa_stream->chip;
	int err;

	dev_dbg(component->dev, "name %s substream %pK", rtd->dai_link->name, substream);
	aoc_timer_stop_sync(alsa_stream);
	atomic_set(&alsa_stream->cancel_work_active, 1);
	audio_free_isr(alsa_stream->dev);
	if (alsa_stream->voip_period_wq) {
		flush_workqueue(alsa_stream->voip_period_wq);
		destroy_workqueue(alsa_stream->voip_period_wq);
		alsa_stream->voip_period_wq = NULL;
	}
	atomic_set(&alsa_stream->cancel_work_active, 0);

	if (mutex_lock_interruptible(&chip->audio_mutex)) {
		dev_err(component->dev, "ERR: interrupted while waiting for lock\n");
		return -EINTR;
	}

	/* Stop voip call (Refactor needed) */
	pr_notice("Stop voip call\n");
	err = teardown_voipcall(alsa_stream);
	if (err < 0)
		pr_err("ERR: fail in voip call tearing down\n");

	runtime = substream->runtime;
	alsa_stream = runtime->private_data;

	dev_dbg(component->dev, "alsa pcm close\n");
	free_aoc_audio_service(rtd->dai_link->name, alsa_stream->dev);
	/*
	* Call stop if it's still running. This happens when app
	* is force killed and we don't get a stop trigger.
	*/
	if (alsa_stream->running) {
		err = aoc_audio_voip_stop(alsa_stream);
		alsa_stream->running = 0;
		if (err != 0)
			dev_err(component->dev, "ERR: fail to stop alsa stream\n");
	}

	alsa_stream->period_size = 0;
	alsa_stream->buffer_size = 0;

	if (alsa_stream->open) {
		alsa_stream->open = 0;
		aoc_audio_close(alsa_stream);
	}
	if (alsa_stream->chip)
		alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
	/*
	* Do not free up alsa_stream here, it will be freed up by
	* runtime->private_free callback we registered in *_open above
	*/
	chip->opened &= ~(1 << alsa_stream->idx);

	mutex_unlock(&chip->audio_mutex);
	return 0;
}

/* PCM hw_params callback */
static int snd_aoc_pcm_hw_params(struct snd_soc_component *component,
				 struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *params)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct aoc_alsa_stream *alsa_stream = runtime->private_data;
	struct aoc_chip *chip = alsa_stream->chip;
	int err;

	err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
	if (err < 0) {
		dev_err(component->dev, "ERR:%d fail in pcm buffer allocation\n", err);
		return err;
	}

	substream->wait_time = msecs_to_jiffies(chip->voice_pcm_wait_time_in_ms);

	alsa_stream->channels = params_channels(params);
	alsa_stream->params_rate = params_rate(params);
	alsa_stream->pcm_format_width = snd_pcm_format_width(params_format(params));

	alsa_stream->pcm_float_fmt = (params_format(params) == SNDRV_PCM_FORMAT_FLOAT_LE);

	dev_dbg(component->dev, "alsa_stream->pcm_format_width = %d\n",
		alsa_stream->pcm_format_width);
	return err;
}

/* PCM hw_free callback */
static int snd_aoc_pcm_hw_free(struct snd_soc_component *component,
			       struct snd_pcm_substream *substream)
{
	return snd_pcm_lib_free_pages(substream);
}

/* PCM prepare callback */
static int snd_aoc_pcm_prepare(struct snd_soc_component *component,
			       struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct aoc_alsa_stream *alsa_stream = runtime->private_data;
	struct aoc_service_dev *dev = alsa_stream->dev;
	struct aoc_chip *chip = alsa_stream->chip;
	int channels, err;

	aoc_timer_stop_sync(alsa_stream);

	if (mutex_lock_interruptible(&chip->audio_mutex))
		return -EINTR;

	channels = alsa_stream->channels;

	pr_debug("channels = %d, rate = %d, bits = %d, float-fmt = %d\n",
		 channels, alsa_stream->params_rate,
		 alsa_stream->pcm_format_width, alsa_stream->pcm_float_fmt);

	aoc_audio_setup(alsa_stream);

	pr_debug("Flush aoc ring buffer\n");
	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
		if (!aoc_ring_flush_read_data(alsa_stream->dev->service, AOC_UP, 0)) {
			pr_err("ERR: capture ring buffer flush fail\n");
			err = -EINVAL;
		}
	}

	/* in preparation of the stream */
	/* aoc_audio_set_ctls(alsa_stream->chip); */
	alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
	alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
	alsa_stream->pos = 0;
	alsa_stream->hw_ptr_base = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
						 aoc_ring_bytes_written(dev->service, AOC_DOWN) :
						 aoc_ring_bytes_written(dev->service, AOC_UP);
	alsa_stream->prev_consumed = alsa_stream->hw_ptr_base;
	alsa_stream->n_overflow = 0;

	pr_notice("Start voip call\n");
	err = prepare_voipcall(alsa_stream);
	if (err < 0)
		dev_err(component->dev, "ERR:%d in preparing voip call\n", err);

	dev_dbg(component->dev, "pcm prepare: hw_ptr_base = %lu\n", alsa_stream->hw_ptr_base);

	dev_dbg(component->dev, "buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
		alsa_stream->buffer_size, alsa_stream->period_size, alsa_stream->pos,
		runtime->frame_bits);

	mutex_unlock(&chip->audio_mutex);
	return err;
}

/* Trigger callback */
static int snd_aoc_pcm_trigger(struct snd_soc_component *component,
			       struct snd_pcm_substream *substream, int cmd)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct aoc_alsa_stream *alsa_stream = runtime->private_data;
	int err = 0;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		dev_dbg(component->dev, "aoc_AUDIO_TRIGGER_START running=%d\n",
			alsa_stream->running);
		if (!alsa_stream->running) {
			/* start timer first to avoid underrun/overrun */
			aoc_timer_start(alsa_stream);

			err = aoc_audio_voip_start(alsa_stream);
			if (err == 0) {
				alsa_stream->running = 1;
				alsa_stream->draining = 1;
			} else {
				dev_err(component->dev, "ERR:%d fail to START stream\n", err);
			}
		}
		break;
	case SNDRV_PCM_TRIGGER_STOP:
		dev_dbg(component->dev, "aoc_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
			alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);

		if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
			dev_dbg(component->dev, "DRAINING\n");
			alsa_stream->draining = 1;
		} else {
			dev_dbg(component->dev, "DROPPING\n");
			alsa_stream->draining = 0;
		}
		if (alsa_stream->running) {
			err = aoc_audio_voip_stop(alsa_stream);
			if (err != 0)
				dev_err(component->dev, "ERR:%d fail to STOP stream\n", err);
			alsa_stream->running = 0;
		}
		break;
	default:
		err = -EINVAL;
	}

	return err;
}

/* Copy data from user space to hardware buffer  */
static int snd_aoc_pcm_playback_copy_user(struct snd_soc_component *component,
					  struct snd_pcm_substream *substream, int channel,
					  unsigned long pos, void __user *buf, unsigned long count)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct aoc_alsa_stream *alsa_stream = runtime->private_data;
	int err = 0;

	err = aoc_audio_write(alsa_stream, buf, count);
	if (err)
		dev_err(component->dev, "ERR:%d fail to send audio to aoc\n", err);

	return err;
}

/* Copy data from hardware buffer to user space */
static int snd_aoc_pcm_capture_copy_user(struct snd_soc_component *component,
					 struct snd_pcm_substream *substream, int channel,
					 unsigned long pos, void __user *buf, unsigned long count)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct aoc_alsa_stream *alsa_stream = runtime->private_data;
	int err = 0;

	err = aoc_audio_read(alsa_stream, buf, count);
	if (err)
		dev_err(component->dev, "ERR:%d fail to get audio from aoc\n", err);

	return err;
}

/* Copy data between hardware buffer and user space */
static int snd_aoc_pcm_copy_user(struct snd_soc_component *component,
				 struct snd_pcm_substream *substream, int channel,
				 unsigned long pos, void __user *buf, unsigned long count)
{
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		return snd_aoc_pcm_playback_copy_user(component, substream, channel, pos, buf,
						      count);
	} else { /* Capture */
		return snd_aoc_pcm_capture_copy_user(component, substream, channel, pos, buf,
						     count);
	}
}

/* Pointer callback */
static snd_pcm_uframes_t snd_aoc_pcm_pointer(struct snd_soc_component *component,
					     struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct aoc_alsa_stream *alsa_stream = runtime->private_data;
	int pointer;

	dev_dbg(component->dev, "pcm_pointer... (%d) hwptr=%ld appl=%ld pos=%d\n", 0,
		frames_to_bytes(runtime, runtime->status->hw_ptr),
		frames_to_bytes(runtime, runtime->control->appl_ptr), alsa_stream->pos);

	pointer = bytes_to_frames(substream->runtime, alsa_stream->pos);

	dev_dbg(component->dev, "pcm pointer  = %d\n", pointer);

	return pointer;
}

static int snd_aoc_pcm_lib_ioctl(struct snd_soc_component *component,
				 struct snd_pcm_substream *substream, unsigned int cmd, void *arg)
{
	int err = snd_pcm_lib_ioctl(substream, cmd, arg);

	dev_dbg(component->dev, " .. substream=%pK, cmd=%d, arg=%pK (%x) err=%d\n", substream, cmd,
		arg, arg ? *(unsigned int *)arg : 0, err);
	return err;
}

static int aoc_pcm_new(struct snd_soc_component *component, struct snd_soc_pcm_runtime *rtd)
{
	struct snd_pcm_substream *substream = NULL;
	/* Allocate DMA memory */
	if (rtd->dai_link->dpcm_playback) {
		substream = rtd->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
		snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_CONTINUOUS,
					      snd_dma_continuous_data(GFP_KERNEL),
					      snd_aoc_playback_hw.buffer_bytes_max,
					      snd_aoc_playback_hw.buffer_bytes_max);
	}

	if (rtd->dai_link->dpcm_capture) {
		substream = rtd->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
		snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_CONTINUOUS,
					      snd_dma_continuous_data(GFP_KERNEL),
					      snd_aoc_playback_hw.buffer_bytes_max,
					      snd_aoc_playback_hw.buffer_bytes_max);
	}

	rtd->pcm->nonatomic = true;
	return 0;
}

static const struct snd_soc_component_driver aoc_pcm_component = {
	.name = "AoC PCM",
	.open = snd_aoc_pcm_open,
	.close = snd_aoc_pcm_close,
	.ioctl = snd_aoc_pcm_lib_ioctl,
	.hw_params = snd_aoc_pcm_hw_params,
	.hw_free = snd_aoc_pcm_hw_free,
	.copy_user = snd_aoc_pcm_copy_user,
	.prepare = snd_aoc_pcm_prepare,
	.trigger = snd_aoc_pcm_trigger,
	.pointer = snd_aoc_pcm_pointer,
	.pcm_construct = aoc_pcm_new,
};

static int aoc_pcm_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	int err = 0;

	if (!np)
		return -EINVAL;
	err = devm_snd_soc_register_component(dev, &aoc_pcm_component, NULL, 0);
	if (err) {
		dev_err(dev, "ERR:%d fail to reigster aoc pcm comp\n", err);
		return err;
	}

	return 0;
}

static const struct of_device_id aoc_voip_of_match[] = {
	{
		.compatible = "google-aoc-snd-voip",
	},
	{},
};
MODULE_DEVICE_TABLE(of, aoc_voip_of_match);

static struct platform_driver aoc_pcm_drv = {
    .driver =
        {
            .name = "google-aoc-snd-voip",
            .of_match_table = aoc_voip_of_match,
        },
    .probe = aoc_pcm_probe,
};

int aoc_voip_init(void)
{
	int err;

	err = platform_driver_register(&aoc_pcm_drv);
	if (err) {
		pr_err("ERR:%d in registering aoc pcm drv\n", err);
		return err;
	}

	return 0;
}

void aoc_voip_exit(void)
{
	platform_driver_unregister(&aoc_pcm_drv);
}