summaryrefslogtreecommitdiff
path: root/sound/soc/tegra/tegra30_avp.c
blob: 6416a866ba04742b31023338aed3a9ac81d1d355 (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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
/*
 * tegra30_avp.c - Tegra AVP audio driver
 *
 * Author: Sumit Bhattacharya <sumitb@nvidia.com>
 * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

/*
#define VERBOSE_DEBUG 1
#define DEBUG 1
#define DEBUG_AVP
*/

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/completion.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/platform_device.h>
#include <linux/firmware.h>
#include <linux/kthread.h>

#include <linux/dmaengine.h>

#include <sound/compress_offload.h>
#include <sound/compress_params.h>
#include <sound/pcm.h>

#include <linux/tegra_avp_audio.h>
#include "tegra_offload.h"

#include <linux/tegra_nvavp.h>
#include "../../drivers/media/platform/tegra/nvavp/nvavp_os.h"

#define DRV_NAME "tegra30-avp-audio"

/******************************************************************************
 * DEFINES, ENUMS & GLOBALS
 *****************************************************************************/
#define AVP_UCODE_HEADER		"NVAVPAPP"
#define AVP_UCODE_HEADER_SIZE		(sizeof(AVP_UCODE_HEADER) - 1)
#define AVP_INIT_SAMPLE_RATE		48000

#define AVP_COMPR_THRESHOLD		(4 * 1024)
#define AVP_UNITY_STREAM_VOLUME		0x10000

#define AVP_CMD_BUFFER_SIZE		256

enum avp_compr_formats {
	avp_compr_mp3,
	avp_compr_aac,
	avp_compr_max,
};

#ifdef DEBUG_AVP
#define DUMP_AVP_STATUS(s) \
	pr_info("%s %d : id %d:: SS %d Halt %d RP %d %d PP %d LP %d FD %d "\
		"WP %d WC %d DS %d DBWP %d SR %d DBRP %d\n",\
		__func__, __LINE__,\
		s->id,\
		(int)s->stream->stream_state_current,\
		s->stream->halted,\
		s->stream->source_buffer_read_position,\
		s->stream->source_buffer_read_position_fraction,\
		s->stream->source_buffer_presentation_position,\
		s->stream->source_buffer_linear_position,\
		s->stream->source_buffer_frames_decoded,\
		s->stream->source_buffer_write_position,\
		s->stream->source_buffer_write_count,\
		(int)s->audio_avp->audio_engine->device_state_current,\
		s->audio_avp->audio_engine->device_buffer_write_position,\
		s->stream->stream_notification_request, \
		s->audio_avp->audio_engine->device_buffer_read_position);
#else
#define DUMP_AVP_STATUS(s)
#endif

/******************************************************************************
 * STRUCTURES
 *****************************************************************************/

static const struct tegra30_avp_ucode_desc {
	int max_mem_size;
	const char *bin_name;
} avp_ucode_desc[] = {
	[CODEC_PCM] = {30000, "nvavp_aud_ucode.bin" },
	[CODEC_MP3] = {60000, "nvavp_mp3dec_ucode.bin" },
	[CODEC_AAC] = {100000, "nvavp_aacdec_ucode.bin" },
};

struct tegra30_avp_audio_dma {
	struct tegra_offload_dma_params	params;
	struct dma_chan			*chan;
	struct dma_async_tx_descriptor	*chan_desc;
	struct dma_slave_config		chan_slave_config;
	dma_cookie_t			chan_cookie;

	int				use_count;
	int				active_count;
};

struct tegra30_avp_stream {
	struct tegra30_avp_audio	*audio_avp;
	struct tegra_offload_mem	source_buf;
	struct stream_data		*stream;
	enum avp_audio_stream_id	id;
	int				period_size;

	/* TODO : Use spinlock in appropriate places */
	spinlock_t			lock;

	unsigned int			last_notification_offset;
	unsigned int			notification_received;
	unsigned int			source_buffer_offset;

	void		(*notify_cb)(void *args, unsigned int is_eos);
	void		*notify_args;
	unsigned int	is_drain_called;
};

struct tegra30_avp_audio {
	struct device			*dev;

	nvavp_clientctx_t		nvavp_client;
	struct audio_engine_data	*audio_engine;
	struct tegra30_avp_stream	avp_stream[RENDERSW_MAX_STREAMS];

	struct tegra_offload_mem	ucode_mem;
	struct tegra_offload_mem	cmd_buf_mem;
	struct tegra_offload_mem	param_mem;

	unsigned int			*cmd_buf;
	int				cmd_buf_idx;
	int				stream_active_count;
	struct tegra30_avp_audio_dma	audio_dma;
	spinlock_t			lock;
};

struct snd_compr_caps tegra30_avp_compr_caps[SND_COMPRESS_CAPTURE + 1] = {
	[SND_COMPRESS_PLAYBACK] = {
		.num_codecs = avp_compr_max,
		.direction = SND_COMPRESS_PLAYBACK,
		.min_fragment_size = 1024,
		.max_fragment_size = 1024 * 1024,	/* 1 MB */
		.min_fragments = 2,
		.max_fragments = 1024,
		.codecs = {
			[0] = SND_AUDIOCODEC_MP3,
			[1] = SND_AUDIOCODEC_AAC,
		},
	},
	[SND_COMPRESS_CAPTURE] = {
		.num_codecs = 0,
		.direction = SND_COMPRESS_CAPTURE,
	},
};

struct snd_compr_codec_caps tegra30_avp_compr_codec_caps[] = {
	[avp_compr_mp3] = {
		.codec = SND_AUDIOCODEC_MP3,
		.num_descriptors = 1,
		.descriptor = {
			[0] = {
				.max_ch = 2,
				.sample_rates = SNDRV_PCM_RATE_44100 |
						SNDRV_PCM_RATE_48000,
				.bit_rate = {
					[0] = 32000,
					[1] = 64000,
					[2] = 128000,
					[3] = 256000,
					[4] = 320000,
				},
				.num_bitrates = 5,
				.rate_control =
					SND_RATECONTROLMODE_CONSTANTBITRATE |
					SND_RATECONTROLMODE_VARIABLEBITRATE,
				.profiles = 0,
				.modes = SND_AUDIOCHANMODE_MP3_STEREO,
				.formats = SND_AUDIOSTREAMFORMAT_UNDEFINED,
				.min_buffer = 1024,
			},
		},
	},
	[avp_compr_aac] = {
		.codec = SND_AUDIOCODEC_AAC,
		.num_descriptors = 1,
		.descriptor = {
			[0] = {
				.max_ch = 2,
				.sample_rates = SNDRV_PCM_RATE_44100 |
						SNDRV_PCM_RATE_48000,
				.bit_rate = {
					[0] = 32000,
					[1] = 64000,
					[2] = 128000,
					[3] = 256000,
					[4] = 320000,
				},
				.num_bitrates = 5,
				.rate_control =
					SND_RATECONTROLMODE_CONSTANTBITRATE |
					SND_RATECONTROLMODE_VARIABLEBITRATE,
				.profiles = SND_AUDIOPROFILE_AAC,
				.modes = SND_AUDIOMODE_AAC_LC,
				.formats = SND_AUDIOSTREAMFORMAT_MP4ADTS,
				.min_buffer = 1024,
			},
		},
	},
};

static struct tegra30_avp_audio *avp_audio_ctx;

/******************************************************************************
 * PRIVATE FUNCTIONS
 *****************************************************************************/

static int tegra30_avp_mem_alloc(struct tegra_offload_mem *mem, size_t size)
{
	mem->virt_addr = dma_alloc_coherent(avp_audio_ctx->dev, size,
						&mem->phys_addr, GFP_KERNEL);
	if (!mem->virt_addr) {
		dev_err(avp_audio_ctx->dev, "Failed to allocate memory");
		return -ENOMEM;
	}
	mem->bytes = size;
	mem->dev = avp_audio_ctx->dev;
	memset(mem->virt_addr, 0, mem->bytes);

	dev_vdbg(mem->dev, "%s::virt %p phys %llx size %d\n", __func__,
		mem->virt_addr, (u64)mem->phys_addr, (int)size);
	return 0;
}

static void tegra30_avp_mem_free(struct tegra_offload_mem *mem)
{
	if (mem->virt_addr)
		dma_free_coherent(mem->dev, mem->bytes,
			mem->virt_addr, mem->phys_addr);
}

static int tegra30_avp_load_ucode(void)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct audio_engine_data *audio_engine;
	const struct firmware *ucode_fw;
	const struct tegra30_avp_ucode_desc *ucode_desc;
	int ucode_size = 0, ucode_offset = 0, total_ucode_size = 0;
	int i, ret = 0;

	dev_vdbg(audio_avp->dev, "%s", __func__);

	for (i = 0; i < ARRAY_SIZE(avp_ucode_desc); i++)
		total_ucode_size += avp_ucode_desc[i].max_mem_size;

	/* allocate memory for Ucode */
	ret = tegra30_avp_mem_alloc(&audio_avp->ucode_mem, total_ucode_size);
	if (ret < 0) {
		dev_err(audio_avp->dev, "Failed to allocate ucode memory");
		return ret;
	}

	/* allocate memory for parameters */
	ret = tegra30_avp_mem_alloc(&audio_avp->param_mem,
		sizeof(struct audio_engine_data));
	if (ret < 0) {
		dev_err(audio_avp->dev, "Failed to allocate param memory");
		goto err_ucode_mem_free;
	}
	audio_engine =
		(struct audio_engine_data *)audio_avp->param_mem.virt_addr;
	audio_avp->audio_engine = audio_engine;
	audio_engine->codec_ucode_base_address =
		audio_avp->ucode_mem.phys_addr;

	for (i = 0; i < ARRAY_SIZE(avp_ucode_desc); i++) {
		ucode_desc = &avp_ucode_desc[i];

		ret = request_firmware(&ucode_fw, ucode_desc->bin_name,
			audio_avp->dev);
		if (ret < 0) {
			dev_err(audio_avp->dev, "cannot read audio firmware %s",
				ucode_desc->bin_name);
			goto err_param_mem_free;
		}

		ucode_size = ucode_fw->size;
		if (ucode_size <= 0) {
			dev_err(audio_avp->dev, "Invalid ucode size.");
			ret = -EINVAL;
			release_firmware(ucode_fw);
			goto err_param_mem_free;
		}
		dev_vdbg(audio_avp->dev, "%s ucode size = %d bytes",
			ucode_desc->bin_name, ucode_size);

		/* Read ucode */
		if (strncmp((const char *)ucode_fw->data, AVP_UCODE_HEADER,
			AVP_UCODE_HEADER_SIZE)) {
			dev_err(audio_avp->dev, "ucode Header mismatch");
			ret = -EINVAL;
			release_firmware(ucode_fw);
			goto err_param_mem_free;
		}

		memcpy((char *)audio_avp->ucode_mem.virt_addr + ucode_offset,
			ucode_fw->data + AVP_UCODE_HEADER_SIZE,
			ucode_size - AVP_UCODE_HEADER_SIZE);

		audio_engine->codec_ucode_desc[i][UCODE_DESC_OFFSET] =
								ucode_offset;
		audio_engine->codec_ucode_desc[i][UCODE_DESC_SIZE] =
						ucode_desc->max_mem_size;
		ucode_offset += ucode_desc->max_mem_size;

		release_firmware(ucode_fw);
	}

	/* allocate memory for command buffer */
	ret = tegra30_avp_mem_alloc(&audio_avp->cmd_buf_mem,
			AVP_CMD_BUFFER_SIZE);
	if (ret < 0) {
		dev_err(audio_avp->dev, "Failed to allocate cmd buffer memory");
		goto err_param_mem_free;
	}
	audio_avp->cmd_buf = (unsigned int *)audio_avp->cmd_buf_mem.virt_addr;

	/* Set command */
	audio_avp->cmd_buf_idx = 0;
	audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
		NVE26E_CH_OPCODE_INCR(NVE276_SET_MICROCODE_A, 3);
	audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] = 0;
	audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
		audio_avp->ucode_mem.phys_addr;
	audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
		avp_ucode_desc[CODEC_PCM].max_mem_size - 8;
	audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
		NVE26E_CH_OPCODE_INCR(NVE276_PARAMETER_METHOD(0), 1);
	audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
		audio_avp->param_mem.phys_addr;

	ret = nvavp_pushbuffer_submit_audio(audio_avp->nvavp_client,
					    audio_avp->cmd_buf_mem.phys_addr,
					    audio_avp->cmd_buf_idx);
	if (ret < 0) {
		dev_err(audio_avp->dev, "pushbuffer_submit failed %d", ret);
		ret = -EINVAL;
		goto err_cmd_buf_mem_free;
	}
	dev_vdbg(audio_avp->dev, "Successfully loaded audio ucode");
	return 0;

err_cmd_buf_mem_free:
	tegra30_avp_mem_free(&audio_avp->cmd_buf_mem);
err_param_mem_free:
	tegra30_avp_mem_free(&audio_avp->param_mem);
err_ucode_mem_free:
	tegra30_avp_mem_free(&audio_avp->ucode_mem);
	return ret;
}

static void tegra30_avp_audio_engine_init(void)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct audio_engine_data *audio_engine = audio_avp->audio_engine;
	int i = 0;

	/* Initialize audio_engine_data */
#if defined(CONFIG_ARCH_TEGRA_14x_SOC) || defined(CONFIG_ARCH_TEGRA_12x_SOC)
	audio_engine->chip_id = NV_TEGRA_T148;
#else
	audio_engine->chip_id = NV_TEGRA_T114;
#endif
	audio_engine->device_state_target = KSSTATE_STOP;
	audio_engine->device_state_current = KSSTATE_STOP;

#ifdef DEBUG_AVP
	audio_engine->profile_state = PROFILE_RENDER_OVERALL_PROCESSING;
#endif
	audio_engine->device_format.rate = AVP_INIT_SAMPLE_RATE;
	audio_engine->device_format.bits_per_sample = 16;
	audio_engine->device_format.channels = 2;

	/* Initialize stream memory */
	for (i = 0; i < max_stream_id; i++) {
		struct tegra30_avp_stream *avp_stream;
		struct stream_data *stream;
		int j = 0;

		avp_stream = &audio_avp->avp_stream[i];
		memset(avp_stream, 0, sizeof(struct tegra30_avp_stream));

		avp_stream->id = i;

		stream = &audio_engine->stream[avp_stream->id];
		avp_stream->stream = stream;
		spin_lock_init(&avp_stream->lock);

		stream->stream_state_target = KSSTATE_STOP;
		stream->source_buffer_write_position = 0;
		stream->source_buffer_write_count = 0;
		stream->stream_params.rate = AVP_INIT_SAMPLE_RATE;

		for (j = 0; j < RENDERSW_MAX_CHANNELS; j++)
			stream->stream_volume[j] = AVP_UNITY_STREAM_VOLUME;

		stream->source_buffer_read_position = 0;
		stream->source_buffer_presentation_position = 0;
		stream->source_buffer_linear_position = 0;
		stream->source_buffer_presentation_position = 0;
		stream->source_buffer_frames_decoded = 0;
		stream->stream_state_current = KSSTATE_STOP;

		stream->stream_params.rate = AVP_INIT_SAMPLE_RATE;
		stream->stream_params.bits_per_sample = 16;
		stream->stream_params.channels = 2;

		avp_stream->audio_avp = audio_avp;
	}
}

static int tegra30_avp_audio_alloc_dma(struct tegra_offload_dma_params *params)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;
	struct audio_engine_data *audio_engine = audio_avp->audio_engine;
	dma_cap_mask_t mask;
	int ret = 0;

	dev_vdbg(audio_avp->dev, "%s : use %d",
		__func__, dma->use_count);

	dma->use_count++;
	if (dma->use_count > 1)
		return 0;

	memcpy(&dma->params, params, sizeof(struct tegra_offload_dma_params));

	dma_cap_zero(mask);
	dma_cap_set(DMA_SLAVE, mask);
	dma_cap_set(DMA_CYCLIC, mask);
	dma->chan = dma_request_channel(mask, NULL, NULL);
	if (dma->chan == NULL) {
		dev_err(audio_avp->dev, "Failed to allocate DMA chan.");
		return -ENOMEM;
	}

	/* Only playback is supported */
	dma->chan_slave_config.direction = DMA_MEM_TO_DEV;
	dma->chan_slave_config.dst_addr_width = dma->params.width;
	dma->chan_slave_config.dst_addr = dma->params.addr;
	dma->chan_slave_config.dst_maxburst = dma->params.max_burst;
	dma->chan_slave_config.slave_id = dma->params.req_sel;

	ret = dmaengine_slave_config(dma->chan, &dma->chan_slave_config);
	if (ret < 0) {
		dev_err(audio_avp->dev, "dma slave config failed.err %d.", ret);
		return ret;
	}
	audio_engine->apb_channel_handle = dma->chan->chan_id;
	return 0;
}

static void tegra30_avp_audio_free_dma(void)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;

	dev_vdbg(audio_avp->dev, "%s : use %d",
		__func__, dma->use_count);

	if (dma->use_count <= 0)
		return;

	dma->use_count--;
	if (dma->use_count)
		return;

	dma_release_channel(dma->chan);
}

static int tegra30_avp_audio_start_dma(void)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;
	struct audio_engine_data *audio_engine = audio_avp->audio_engine;

	dev_vdbg(audio_avp->dev, "%s: active %d.", __func__, dma->active_count);

	dma->active_count++;
	if (dma->active_count > 1)
		return 0;

	dma->chan_desc = dmaengine_prep_dma_cyclic(dma->chan,
				(dma_addr_t)audio_engine->device_buffer_avp,
				DEVICE_BUFFER_SIZE,
				DEVICE_BUFFER_SIZE,
				dma->chan_slave_config.direction,
				DMA_CTRL_ACK);
	if (!dma->chan_desc) {
		dev_err(audio_avp->dev, "Failed to prep cyclic dma");
		return -ENODEV;
	}
	dma->chan_cookie = dmaengine_submit(dma->chan_desc);
	dma_async_issue_pending(dma->chan);
	return 0;
}

static int tegra30_avp_audio_stop_dma(void)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;

	dev_vdbg(audio_avp->dev, "%s : active %d",
		__func__, dma->active_count);

	dma->active_count--;
	if (dma->active_count > 0)
		return 0;

	dmaengine_terminate_all(dma->chan);
	return 0;
}

static int tegra30_avp_audio_execute(void)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	uint32_t cmd_idx = audio_avp->cmd_buf_idx;
	int ret = 0;

	dev_vdbg(audio_avp->dev, "%s", __func__);

	/* Set command */
	audio_avp->cmd_buf[cmd_idx++] =
		NVE26E_CH_OPCODE_INCR(NVE276_EXECUTE, 1);
	audio_avp->cmd_buf[cmd_idx++] = 0; /* NVE276_EXECUTE_APPID */

	ret = nvavp_pushbuffer_submit_audio(audio_avp->nvavp_client,
		audio_avp->cmd_buf_mem.phys_addr,
		cmd_idx);
	if (ret < 0) {
		dev_err(audio_avp->dev, "nvavp_pushbuffer_submit_audio failed");
		return -EINVAL;
	}
	return 0;
}

static int tegra30_avp_audio_set_state(enum KSSTATE new_state)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct audio_engine_data *audio_engine = audio_avp->audio_engine;
	enum KSSTATE old_state;

	old_state = audio_engine->device_state_target;

	dev_vdbg(audio_avp->dev, "%s : state %d -> %d",
		__func__, old_state, new_state);

	if (old_state == new_state)
		return 0;

	audio_engine->device_state_target = new_state;
	/* TODO : Need a way to wait till AVP device state changes */
	if (new_state == KSSTATE_RUN)
		tegra30_avp_audio_execute();

	return 0;
}


/* Call this function with stream lock held */
static int tegra30_avp_stream_set_state(int id, enum KSSTATE new_state)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
	struct stream_data *stream = avp_stream->stream;
	enum KSSTATE old_state = stream->stream_state_target;
	int ret = 0;

	dev_vdbg(audio_avp->dev, "%s : id %d state %d -> %d", __func__, id,
		 old_state, new_state);

	if (old_state == new_state)
		return 0;

	if (old_state == KSSTATE_STOP) {
		switch (stream->stream_format) {
		case FORMAT_MP3:
			nvavp_enable_audio_clocks(audio_avp->nvavp_client,
				NVAVP_MODULE_ID_VCP);
			break;
		case FORMAT_AAC:
			nvavp_enable_audio_clocks(audio_avp->nvavp_client,
				NVAVP_MODULE_ID_VCP);
			break;
		default:
			break;
		}
	}

	stream->stream_state_target = new_state;
	/* TODO : Need a way to wait till AVP stream state changes */

	if (new_state == KSSTATE_STOP) {
		switch (stream->stream_format) {
		case FORMAT_MP3:
			nvavp_disable_audio_clocks(audio_avp->nvavp_client,
				NVAVP_MODULE_ID_VCP);
			break;
		case FORMAT_AAC:
			nvavp_disable_audio_clocks(audio_avp->nvavp_client,
				NVAVP_MODULE_ID_VCP);
			break;
		default:
			break;
		}
	}

	if (new_state == KSSTATE_STOP) {
		stream->source_buffer_write_position = 0;
		stream->source_buffer_write_count = 0;
		avp_stream->last_notification_offset = 0;
		avp_stream->notification_received = 0;
		avp_stream->source_buffer_offset = 0;
	}

	if (new_state == KSSTATE_RUN)
		tegra30_avp_audio_start_dma();
	else
		tegra30_avp_audio_stop_dma();

	return ret;
}

/* TODO : review ISR to make it more optimized if possible */
static void tegra30_avp_stream_notify(void)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream;
	struct stream_data *stream;
	int i;
	unsigned int eos = 0;

	/* dev_vdbg(audio_avp->dev, "tegra30_avp_stream_notify"); */

	for (i = 0; i < max_stream_id; i++) {
		avp_stream = &audio_avp->avp_stream[i];
		stream = avp_stream->stream;

		if (avp_stream->notify_cb &&
			(stream->stream_state_target != KSSTATE_RUN))
			continue;
		while (stream->stream_notification_request >
			avp_stream->notification_received) {
			int data_processed =
				stream->source_buffer_read_position -
				avp_stream->last_notification_offset;

			if (data_processed < 0)
				data_processed += stream->source_buffer_size;
			avp_stream->notification_received++;

			while (data_processed >= avp_stream->period_size) {
				if ((avp_stream->notification_received >=
					stream->stream_notification_request)
					&& avp_stream->is_drain_called) {
					eos = 1;
				}
				avp_stream->notify_cb(avp_stream->notify_args,
								eos);
				avp_stream->last_notification_offset +=
					avp_stream->period_size;
				avp_stream->last_notification_offset %=
					stream->source_buffer_size;
				data_processed -= avp_stream->period_size;
			}
		}
	}
}

/******************************************************************************
 * EXPORTED FUNCTIONS
 *****************************************************************************/
/* Device APIs */
static int tegra30_avp_set_hw_rate(int rate)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct audio_engine_data *audio_engine = audio_avp->audio_engine;

	dev_vdbg(audio_avp->dev, "%s rate %d", __func__, rate);

	if (!audio_engine) {
		dev_err(audio_avp->dev, "AVP platform not initialized.");
		return -ENODEV;
	}

	audio_engine->device_format.rate = rate;
	return 0;
}

static int tegra30_avp_alloc_shared_mem(struct tegra_offload_mem *mem,
					int bytes)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	int ret = 0;

	ret = tegra30_avp_mem_alloc(mem, bytes);
	if (ret < 0) {
		dev_err(audio_avp->dev, "%s: Failed. ret %d", __func__, ret);
		return ret;
	}
	return 0;
}

static void tegra30_avp_free_shared_mem(struct tegra_offload_mem *mem)
{
	tegra30_avp_mem_free(mem);
}

/* PCM APIs */
static int tegra30_avp_pcm_open(int *id)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct audio_engine_data *audio_engine = audio_avp->audio_engine;
	int ret = 0;

	dev_vdbg(audio_avp->dev, "%s", __func__);

	if (!audio_avp->nvavp_client) {
		ret = tegra_nvavp_audio_client_open(&audio_avp->nvavp_client);
		if (ret < 0) {
			dev_err(audio_avp->dev, "Failed to open nvavp.");
			return ret;
		}
	}
	if (!audio_engine) {
		ret = tegra30_avp_load_ucode();
		if (ret < 0) {
			dev_err(audio_avp->dev, "Failed to load ucode.");
			return ret;
		}
		tegra30_avp_audio_engine_init();
		nvavp_register_audio_cb(audio_avp->nvavp_client,
			tegra30_avp_stream_notify);
		audio_engine = audio_avp->audio_engine;
	}

	if (!audio_engine->stream[pcm_stream_id].stream_allocated)
		*id = pcm_stream_id;
	else if (!audio_engine->stream[pcm2_stream_id].stream_allocated)
		*id = pcm2_stream_id;
	else {
		dev_err(audio_avp->dev, "All AVP PCM streams are busy");
		return -EBUSY;
	}
	tegra30_avp_audio_set_state(KSSTATE_RUN);
	return 0;
}

static int tegra30_avp_pcm_set_params(int id,
		struct tegra_offload_pcm_params *params)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream =	&audio_avp->avp_stream[id];
	struct stream_data *stream = avp_stream->stream;
	int ret = 0;

	dev_vdbg(audio_avp->dev,
		"%s id %d rate %d chan %d bps %d period size %d buf size %d",
		 __func__, id, params->rate, params->channels,
		 params->bits_per_sample, params->period_size,
		 params->buffer_size);

	/* TODO : check validity of parameters */
	if (!stream) {
		dev_err(audio_avp->dev, "AVP platform not initialized.");
		return -ENODEV;
	}

	stream->stream_notification_interval = params->period_size;
	stream->stream_notification_enable = 1;
	stream->stream_params.rate = params->rate;
	stream->stream_params.channels = params->channels;
	stream->stream_params.bits_per_sample = params->bits_per_sample;
	avp_stream->period_size = params->period_size;

	avp_stream->notify_cb = params->period_elapsed_cb;
	avp_stream->notify_args = params->period_elapsed_args;

	stream->source_buffer_system = params->source_buf.virt_addr;
	stream->source_buffer_avp = params->source_buf.phys_addr;
	stream->source_buffer_size = params->buffer_size;

	/* Set DMA params */
	ret = tegra30_avp_audio_alloc_dma(&params->dma_params);
	if (ret < 0) {
		dev_err(audio_avp->dev, "Failed to allocate DMA. ret %d", ret);
		return ret;
	}
	return 0;
}

static int tegra30_avp_pcm_set_state(int id, int state)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream =	&audio_avp->avp_stream[id];
	struct stream_data *stream = avp_stream->stream;

	dev_vdbg(audio_avp->dev, "%s : id %d state %d", __func__, id, state);

	if (!stream) {
		dev_err(audio_avp->dev, "AVP platform not initialized.");
		return -ENODEV;
	}

	switch (state) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
	case SNDRV_PCM_TRIGGER_RESUME:
		tegra30_avp_stream_set_state(id, KSSTATE_RUN);
		return 0;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
	case SNDRV_PCM_TRIGGER_SUSPEND:
		tegra30_avp_stream_set_state(id, KSSTATE_STOP);
		return 0;
	default:
		dev_err(audio_avp->dev, "Unsupported state.");
		return -EINVAL;
	}
}

static void tegra30_avp_pcm_data_ready(int id, int bytes)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream =	&audio_avp->avp_stream[id];
	struct stream_data *stream = avp_stream->stream;

	dev_vdbg(audio_avp->dev, "%s :id %d size %d", __func__, id, bytes);

	stream->source_buffer_write_position += bytes;
	stream->source_buffer_write_position %= stream->source_buffer_size;

	avp_stream->source_buffer_offset += bytes;
	while (avp_stream->source_buffer_offset >=
		stream->stream_notification_interval) {
		stream->source_buffer_write_count++;
		avp_stream->source_buffer_offset -=
			stream->stream_notification_interval;
	}
	return;
}

static size_t tegra30_avp_pcm_get_position(int id)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream =	&audio_avp->avp_stream[id];
	struct stream_data *stream = avp_stream->stream;
	size_t pos = 0;

	pos = (size_t)stream->source_buffer_read_position;

	dev_vdbg(audio_avp->dev, "%s id %d pos %d", __func__, id, (u32)pos);

	return pos;
}

/* Compress APIs */
static int tegra30_avp_compr_open(int *id)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct audio_engine_data *audio_engine = audio_avp->audio_engine;
	int ret = 0;

	dev_vdbg(audio_avp->dev, "%s", __func__);

	if (!audio_avp->nvavp_client) {
		ret = tegra_nvavp_audio_client_open(&audio_avp->nvavp_client);
		if (ret < 0) {
			dev_err(audio_avp->dev, "Failed to open nvavp.");
			return ret;
		}
	}
	if (!audio_engine) {
		ret = tegra30_avp_load_ucode();
		if (ret < 0) {
			dev_err(audio_avp->dev, "Failed to load ucode.");
			return ret;
		}
		tegra30_avp_audio_engine_init();
		nvavp_register_audio_cb(audio_avp->nvavp_client,
			tegra30_avp_stream_notify);
		audio_engine = audio_avp->audio_engine;
	}

	if (!audio_engine->stream[decode_stream_id].stream_allocated)
		*id = decode_stream_id;
	else if (!audio_engine->stream[decode2_stream_id].stream_allocated)
		*id = decode2_stream_id;
	else {
		dev_err(audio_avp->dev, "All AVP COMPR streams are busy");
		return -EBUSY;
	}
	audio_avp->avp_stream[*id].is_drain_called = 0;
	tegra30_avp_audio_set_state(KSSTATE_RUN);
	return 0;
}

static int tegra30_avp_compr_set_params(int id,
		struct tegra_offload_compr_params *params)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
	struct stream_data *stream = avp_stream->stream;
	int ret = 0;

	dev_vdbg(audio_avp->dev,
		"%s: id %d codec %d rate %d ch %d bps %d buf %d x %d",
		 __func__, id, params->codec_type, params->rate,
		 params->channels, params->bits_per_sample,
		 params->fragment_size, params->fragments);

	/* TODO : check validity of parameters */
	if (!stream) {
		dev_err(audio_avp->dev, "AVP platform not initialized.");
		return -ENODEV;
	}

	if (params->codec_type == SND_AUDIOCODEC_MP3) {
		stream->stream_format = FORMAT_MP3;
	} else if (params->codec_type == SND_AUDIOCODEC_AAC) {
		stream->stream_format = FORMAT_AAC;

		/* AAC-LC is only supported profile*/
		stream->u.aac.audio_profile = AAC_PROFILE_LC;
		stream->u.aac.sampling_freq = params->rate;
		stream->u.aac.payload_type  = AAC_PAYLOAD_ADTS;
		switch (params->rate) {
		case 8000:
			stream->u.aac.sampling_freq_index = 0xb;
			break;
		case 11025:
			stream->u.aac.sampling_freq_index = 0xa;
			break;
		case 12000:
			stream->u.aac.sampling_freq_index = 0x9;
			break;
		case 16000:
			stream->u.aac.sampling_freq_index = 0x8;
			break;
		case 22050:
			stream->u.aac.sampling_freq_index = 0x7;
			break;
		case 24000:
			stream->u.aac.sampling_freq_index = 0x6;
			break;
		case 32000:
			stream->u.aac.sampling_freq_index = 0x5;
			break;
		case 44100:
			stream->u.aac.sampling_freq_index = 0x4;
			break;
		case 48000:
			stream->u.aac.sampling_freq_index = 0x3;
			break;
		case 64000:
			stream->u.aac.sampling_freq_index = 0x2;
			break;
		case 88200:
			stream->u.aac.sampling_freq_index = 0x1;
			break;
		case 96000:
			stream->u.aac.sampling_freq_index = 0x0;
			break;
		default:
			dev_err(audio_avp->dev, "Unsupported rate");
			return -EINVAL;
		}
		/* Only Stereo data is supported */
		stream->u.aac.channel_configuration = params->channels;
	} else {
		dev_err(audio_avp->dev, "Invalid stream/codec type.");
		return -EINVAL;
	}

	stream->stream_params.rate = params->rate;
	stream->stream_params.channels = params->channels;
	stream->stream_params.bits_per_sample = params->bits_per_sample;
	avp_stream->period_size = params->fragment_size;

	avp_stream->notify_cb = params->fragments_elapsed_cb;
	avp_stream->notify_args = params->fragments_elapsed_args;

	stream->source_buffer_size = (params->fragments *
			params->fragment_size);
	tegra30_avp_mem_alloc(&avp_stream->source_buf,
			      stream->source_buffer_size);

	stream->source_buffer_system = avp_stream->source_buf.virt_addr;
	stream->source_buffer_avp = avp_stream->source_buf.phys_addr;

	if (stream->source_buffer_size > AVP_COMPR_THRESHOLD) {
		stream->stream_notification_interval =
			stream->source_buffer_size - AVP_COMPR_THRESHOLD;
	} else {
		stream->stream_notification_interval = avp_stream->period_size;
	}
	stream->stream_notification_enable = 1;

	/* Set DMA params */
	ret = tegra30_avp_audio_alloc_dma(&params->dma_params);
	if (ret < 0) {
		dev_err(audio_avp->dev, "Failed to allocate DMA. ret %d", ret);
		return ret;
	}

	if ((params->codec_type == SND_AUDIOCODEC_MP3) ||
	    (params->codec_type == SND_AUDIOCODEC_AAC)) {
		dev_info(audio_avp->dev, "\n*** STARTING %s ULP PLAYBACK ***\n",
		   (params->codec_type == SND_AUDIOCODEC_MP3) ? "MP3" : "AAC");
	}
	return 0;
}

static int tegra30_avp_compr_set_state(int id, int state)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];

	dev_vdbg(audio_avp->dev, "%s : id %d state %d",
		 __func__, id, state);

	switch (state) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
		tegra30_avp_stream_set_state(id, KSSTATE_RUN);
		return 0;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
		tegra30_avp_stream_set_state(id, KSSTATE_STOP);
		return 0;
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		tegra30_avp_stream_set_state(id, KSSTATE_PAUSE);
		return 0;
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
		tegra30_avp_stream_set_state(id, KSSTATE_RUN);
		return 0;
	case SND_COMPR_TRIGGER_DRAIN:
		/* TODO */
		avp_stream->is_drain_called = 1;
		return 0;
	default:
		dev_err(audio_avp->dev, "Unsupported state.");
		return -EINVAL;
	}
}

static void tegra30_avp_compr_data_ready(int id, int bytes)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
	struct stream_data *stream = avp_stream->stream;

	dev_vdbg(audio_avp->dev, "%s : id %d size %d", __func__, id, bytes);

	stream->source_buffer_write_position += bytes;
	stream->source_buffer_write_position %= stream->source_buffer_size;

	avp_stream->source_buffer_offset += bytes;
	while (avp_stream->source_buffer_offset >=
		stream->stream_notification_interval) {
		stream->source_buffer_write_count++;
		avp_stream->source_buffer_offset -=
			stream->stream_notification_interval;
	}
	return;
}

static int tegra30_avp_compr_write(int id, char __user *buf, int bytes)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
	struct stream_data *stream = avp_stream->stream;
	void *dst = stream->source_buffer_system +
		stream->source_buffer_write_position;
	int avail = 0;
	int write = 0;
	int ret = 0;

	avail = stream->source_buffer_read_position -
		stream->source_buffer_write_position;
	if ((avail < 0) || (!avail &&
		(stream->source_buffer_write_count ==
		stream->stream_notification_request)))
		avail += stream->source_buffer_size;

	dev_vdbg(audio_avp->dev, "%s : id %d size %d", __func__, id, bytes);

	/* TODO: check enough free space is available before writing */
	bytes = (bytes > avail) ? avail : bytes;
	if (!bytes) {
		dev_dbg(audio_avp->dev, "No free space in ring buffer.");
		DUMP_AVP_STATUS(avp_stream);
		return bytes;
	}

	write = stream->source_buffer_size -
		stream->source_buffer_write_position;
	if (write > bytes) {
		ret = copy_from_user(dst, buf, bytes);
		if (ret < 0) {
			dev_err(audio_avp->dev, "Failed to copy user data.");
			return -EFAULT;
		}
	} else {
		ret = copy_from_user(dst, buf, write);
		if (ret < 0) {
			dev_err(audio_avp->dev, "Failed to copy user data.");
			return -EFAULT;
		}

		ret = copy_from_user(stream->source_buffer_system, buf + write,
			bytes - write);
		if (ret < 0) {
			dev_err(audio_avp->dev, "Failed to copy user data.");
			return -EFAULT;
		}
	}

	stream->source_buffer_write_position += bytes;
	stream->source_buffer_write_position %= stream->source_buffer_size;

	avp_stream->source_buffer_offset += bytes;
	while (avp_stream->source_buffer_offset >=
		stream->stream_notification_interval) {
		stream->source_buffer_write_count++;
		avp_stream->source_buffer_offset -=
			stream->stream_notification_interval;
	}
	DUMP_AVP_STATUS(avp_stream);
	return bytes;
}

static int tegra30_avp_compr_get_position(int id,
			struct snd_compr_tstamp *tstamp)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
	struct stream_data *stream = avp_stream->stream;

	tstamp->byte_offset = stream->source_buffer_write_position;
	tstamp->copied_total = stream->source_buffer_write_position +
		(stream->source_buffer_write_count *
		 stream->stream_notification_interval);
	tstamp->pcm_frames = stream->source_buffer_presentation_position;
	tstamp->pcm_io_frames = stream->source_buffer_presentation_position;
	tstamp->sampling_rate = stream->stream_params.rate;

	dev_vdbg(audio_avp->dev, "%s id %d off %d copied %d pcm %d pcm io %d",
		 __func__, id, (int)tstamp->byte_offset,
		 (int)tstamp->copied_total, (int)tstamp->pcm_frames,
		 (int)tstamp->pcm_io_frames);

	return 0;
}

static int tegra30_avp_compr_get_caps(struct snd_compr_caps *caps)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;

	dev_vdbg(audio_avp->dev, "%s : dir %d", __func__, caps->direction);

	if (caps->direction == SND_COMPRESS_PLAYBACK)
		memcpy(caps, &tegra30_avp_compr_caps[SND_COMPRESS_PLAYBACK],
			sizeof(struct snd_compr_caps));
	else
		memcpy(caps, &tegra30_avp_compr_caps[SND_COMPRESS_CAPTURE],
			sizeof(struct snd_compr_caps));
	return 0;
}

static int tegra30_avp_compr_get_codec_caps(struct snd_compr_codec_caps *codec)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;

	dev_vdbg(audio_avp->dev, "%s : codec %d", __func__, codec->codec);

	switch (codec->codec) {
	case SND_AUDIOCODEC_MP3:
		memcpy(codec, &tegra30_avp_compr_codec_caps[avp_compr_mp3],
			sizeof(struct snd_compr_codec_caps));
		return 0;
	case SND_AUDIOCODEC_AAC:
		memcpy(codec, &tegra30_avp_compr_codec_caps[avp_compr_aac],
			sizeof(struct snd_compr_codec_caps));
		return 0;
	default:
		dev_err(audio_avp->dev, "Unsupported codec %d", codec->codec);
		return -EINVAL;
	}
}

static int tegra30_avp_compr_set_volume(int id, int left, int right)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
	struct stream_data *stream = avp_stream->stream;

	dev_vdbg(audio_avp->dev, "%s id %d left vol %d right vol %d",
		 __func__, id, left, right);

	if (!stream) {
		dev_err(audio_avp->dev, "AVP platform not initialized.");
		return -ENODEV;
	}

	stream->stream_volume[0] = left;
	stream->stream_volume[1] = right;

	return 0;
}

/* Common APIs for pcm and compress */
static void tegra30_avp_stream_close(int id)
{
	struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
	struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
	struct stream_data *stream = avp_stream->stream;

	dev_vdbg(audio_avp->dev, "%s id %d", __func__, id);

	if (!stream) {
		dev_err(audio_avp->dev, "AVP platform not initialized.");
		return;
	}
	tegra30_avp_mem_free(&avp_stream->source_buf);
	tegra30_avp_audio_free_dma();
	stream->stream_allocated = 0;
	tegra30_avp_stream_set_state(id, KSSTATE_STOP);
	tegra30_avp_audio_set_state(KSSTATE_STOP);
}

static struct tegra_offload_ops avp_audio_platform = {
	.device_ops = {
		.set_hw_rate = tegra30_avp_set_hw_rate,
		.alloc_shared_mem = tegra30_avp_alloc_shared_mem,
		.free_shared_mem = tegra30_avp_free_shared_mem,
	},
	.pcm_ops = {
		.stream_open = tegra30_avp_pcm_open,
		.stream_close = tegra30_avp_stream_close,
		.set_stream_params = tegra30_avp_pcm_set_params,
		.set_stream_state = tegra30_avp_pcm_set_state,
		.get_stream_position = tegra30_avp_pcm_get_position,
		.data_ready = tegra30_avp_pcm_data_ready,
	},
	.compr_ops = {
		.stream_open = tegra30_avp_compr_open,
		.stream_close = tegra30_avp_stream_close,
		.set_stream_params = tegra30_avp_compr_set_params,
		.set_stream_state = tegra30_avp_compr_set_state,
		.get_stream_position = tegra30_avp_compr_get_position,
		.data_ready = tegra30_avp_compr_data_ready,
		.write = tegra30_avp_compr_write,
		.get_caps = tegra30_avp_compr_get_caps,
		.get_codec_caps = tegra30_avp_compr_get_codec_caps,
		.set_stream_volume = tegra30_avp_compr_set_volume,
	},
};

static u64 tegra_dma_mask = DMA_BIT_MASK(32);
static int tegra30_avp_audio_probe(struct platform_device *pdev)
{
	struct tegra30_avp_audio *audio_avp;

	pr_debug("tegra30_avp_audio_platform_probe platform probe started\n");

	audio_avp = devm_kzalloc(&pdev->dev, sizeof(*audio_avp), GFP_KERNEL);
	if (!audio_avp) {
		dev_err(&pdev->dev, "Can't allocate tegra30_avp_audio\n");
		return -ENOMEM;
	}
	dev_set_drvdata(&pdev->dev, audio_avp);
	audio_avp->dev = &pdev->dev;

	/* set the ops */
	if (tegra_register_offload_ops(&avp_audio_platform)) {
		dev_err(&pdev->dev, "Failed to register avp audio device.");
		return -EPROBE_DEFER;
	}

	spin_lock_init(&audio_avp->lock);
	pdev->dev.dma_mask = &tegra_dma_mask;
	pdev->dev.coherent_dma_mask = tegra_dma_mask;
	avp_audio_ctx = audio_avp;
	pr_info("tegra30_avp_audio_platform_probe successful.");
	return 0;
}

static int tegra30_avp_audio_remove(struct platform_device *pdev)
{
	struct tegra30_avp_audio *audio_avp = dev_get_drvdata(&pdev->dev);

	dev_vdbg(&pdev->dev, "%s", __func__);

	tegra_nvavp_audio_client_release(audio_avp->nvavp_client);
	tegra30_avp_mem_free(&audio_avp->cmd_buf_mem);
	tegra30_avp_mem_free(&audio_avp->param_mem);
	tegra30_avp_mem_free(&audio_avp->ucode_mem);

	return 0;
}

static const struct of_device_id tegra30_avp_audio_of_match[] = {
	{ .compatible = "nvidia,tegra30-avp-audio", },
	{},
};

static struct platform_driver tegra30_avp_audio_driver = {
	.driver = {
		.name = DRV_NAME,
		.owner = THIS_MODULE,
		.of_match_table = tegra30_avp_audio_of_match,
	},
	.probe = tegra30_avp_audio_probe,
	.remove = tegra30_avp_audio_remove,
};
module_platform_driver(tegra30_avp_audio_driver);

MODULE_AUTHOR("Sumit Bhattacharya <sumitb@nvidia.com>");
MODULE_DESCRIPTION("Tegra30 AVP Audio driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:" DRV_NAME);
MODULE_DEVICE_TABLE(of, tegra30_avp_audio_of_match);