d28e25ab057cc2a48f83647f787a2f7f0e050f4a
[linux-3.10.git] / sound / soc / tegra / tegra30_avp.c
1 /*
2  * tegra30_avp.c - Tegra AVP audio driver
3  *
4  * Author: Sumit Bhattacharya <sumitb@nvidia.com>
5  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  */
16
17 /*
18 #define VERBOSE_DEBUG 1
19 #define DEBUG 1
20 #define DEBUG_AVP
21 */
22
23 #include <linux/module.h>
24 #include <linux/fs.h>
25 #include <linux/completion.h>
26 #include <linux/uaccess.h>
27 #include <linux/delay.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/platform_device.h>
30 #include <linux/firmware.h>
31 #include <linux/kthread.h>
32
33 #include <linux/dmaengine.h>
34
35 #include <sound/compress_offload.h>
36 #include <sound/compress_params.h>
37 #include <sound/pcm.h>
38
39 #include <linux/tegra_avp_audio.h>
40 #include "tegra_offload.h"
41
42 #include <linux/tegra_nvavp.h>
43 #include "../../drivers/media/platform/tegra/nvavp/nvavp_os.h"
44
45 #define DRV_NAME "tegra30-avp-audio"
46
47 /******************************************************************************
48  * DEFINES, ENUMS & GLOBALS
49  *****************************************************************************/
50 #define AVP_UCODE_HEADER                "NVAVPAPP"
51 #define AVP_UCODE_HEADER_SIZE           (sizeof(AVP_UCODE_HEADER) - 1)
52 #define AVP_INIT_SAMPLE_RATE            48000
53
54 #define AVP_COMPR_THRESHOLD             (4 * 1024)
55 #define AVP_UNITY_STREAM_VOLUME         0x10000
56
57 #define AVP_CMD_BUFFER_SIZE             256
58
59 enum avp_compr_formats {
60         avp_compr_mp3,
61         avp_compr_aac,
62         avp_compr_max,
63 };
64
65 #ifdef DEBUG_AVP
66 #define DUMP_AVP_STATUS(s) \
67         pr_info("%s %d : id %d:: SS %d Halt %d RP %d %d PP %d LP %d FD %d "\
68                 "WP %d WC %d DS %d DBWP %d SR %d DBRP %d\n",\
69                 __func__, __LINE__,\
70                 s->id,\
71                 (int)s->stream->stream_state_current,\
72                 s->stream->halted,\
73                 s->stream->source_buffer_read_position,\
74                 s->stream->source_buffer_read_position_fraction,\
75                 s->stream->source_buffer_presentation_position,\
76                 s->stream->source_buffer_linear_position,\
77                 s->stream->source_buffer_frames_decoded,\
78                 s->stream->source_buffer_write_position,\
79                 s->stream->source_buffer_write_count,\
80                 (int)s->audio_avp->audio_engine->device_state_current,\
81                 s->audio_avp->audio_engine->device_buffer_write_position,\
82                 s->stream->stream_notification_request, \
83                 s->audio_avp->audio_engine->device_buffer_read_position);
84 #else
85 #define DUMP_AVP_STATUS(s)
86 #endif
87
88 /******************************************************************************
89  * STRUCTURES
90  *****************************************************************************/
91
92 static const struct tegra30_avp_ucode_desc {
93         int max_mem_size;
94         const char *bin_name;
95 } avp_ucode_desc[] = {
96         [CODEC_PCM] = {25000, "nvavp_aud_ucode.bin" },
97         [CODEC_MP3] = {45000, "nvavp_mp3dec_ucode.bin" },
98         [CODEC_AAC] = {80000, "nvavp_aacdec_ucode.bin" },
99 };
100
101 struct tegra30_avp_audio_dma {
102         struct tegra_offload_dma_params params;
103         struct dma_chan                 *chan;
104         struct dma_async_tx_descriptor  *chan_desc;
105         struct dma_slave_config         chan_slave_config;
106         dma_cookie_t                    chan_cookie;
107
108         atomic_t                        is_dma_allocated;
109         atomic_t                        active_count;
110 };
111
112 struct tegra30_avp_stream {
113         struct tegra30_avp_audio        *audio_avp;
114         struct tegra_offload_mem        source_buf;
115         struct stream_data              *stream;
116         enum avp_audio_stream_id        id;
117         int                             period_size;
118
119         /* TODO : Use spinlock in appropriate places */
120         spinlock_t                      lock;
121
122         unsigned int                    last_notification_offset;
123         unsigned int                    notification_received;
124         unsigned int                    source_buffer_offset;
125
126         void            (*notify_cb)(void *args, unsigned int is_eos);
127         void            *notify_args;
128         unsigned int    is_drain_called;
129 };
130
131 struct tegra30_avp_audio {
132         struct device                   *dev;
133
134         nvavp_clientctx_t               nvavp_client;
135         struct audio_engine_data        *audio_engine;
136         struct tegra30_avp_stream       avp_stream[RENDERSW_MAX_STREAMS];
137
138         struct tegra_offload_mem        ucode_mem;
139         struct tegra_offload_mem        cmd_buf_mem;
140         struct tegra_offload_mem        param_mem;
141
142         unsigned int                    *cmd_buf;
143         int                             cmd_buf_idx;
144         atomic_t                stream_active_count;
145         struct tegra30_avp_audio_dma    audio_dma;
146         spinlock_t                      lock;
147 };
148
149 struct snd_compr_caps tegra30_avp_compr_caps[SND_COMPRESS_CAPTURE + 1] = {
150         [SND_COMPRESS_PLAYBACK] = {
151                 .num_codecs = avp_compr_max,
152                 .direction = SND_COMPRESS_PLAYBACK,
153                 .min_fragment_size = 1024,
154                 .max_fragment_size = 1024 * 1024,       /* 1 MB */
155                 .min_fragments = 2,
156                 .max_fragments = 1024,
157                 .codecs = {
158                         [0] = SND_AUDIOCODEC_MP3,
159                         [1] = SND_AUDIOCODEC_AAC,
160                 },
161         },
162         [SND_COMPRESS_CAPTURE] = {
163                 .num_codecs = 0,
164                 .direction = SND_COMPRESS_CAPTURE,
165         },
166 };
167
168 struct snd_compr_codec_caps tegra30_avp_compr_codec_caps[] = {
169         [avp_compr_mp3] = {
170                 .codec = SND_AUDIOCODEC_MP3,
171                 .num_descriptors = 1,
172                 .descriptor = {
173                         [0] = {
174                                 .max_ch = 2,
175                                 .sample_rates = SNDRV_PCM_RATE_44100 |
176                                                 SNDRV_PCM_RATE_48000,
177                                 .bit_rate = {
178                                         [0] = 32000,
179                                         [1] = 64000,
180                                         [2] = 128000,
181                                         [3] = 256000,
182                                         [4] = 320000,
183                                 },
184                                 .num_bitrates = 5,
185                                 .rate_control =
186                                         SND_RATECONTROLMODE_CONSTANTBITRATE |
187                                         SND_RATECONTROLMODE_VARIABLEBITRATE,
188                                 .profiles = 0,
189                                 .modes = SND_AUDIOCHANMODE_MP3_STEREO,
190                                 .formats = SND_AUDIOSTREAMFORMAT_UNDEFINED,
191                                 .min_buffer = 1024,
192                         },
193                 },
194         },
195         [avp_compr_aac] = {
196                 .codec = SND_AUDIOCODEC_AAC,
197                 .num_descriptors = 1,
198                 .descriptor = {
199                         [0] = {
200                                 .max_ch = 2,
201                                 .sample_rates = SNDRV_PCM_RATE_44100 |
202                                                 SNDRV_PCM_RATE_48000,
203                                 .bit_rate = {
204                                         [0] = 32000,
205                                         [1] = 64000,
206                                         [2] = 128000,
207                                         [3] = 256000,
208                                         [4] = 320000,
209                                 },
210                                 .num_bitrates = 5,
211                                 .rate_control =
212                                         SND_RATECONTROLMODE_CONSTANTBITRATE |
213                                         SND_RATECONTROLMODE_VARIABLEBITRATE,
214                                 .profiles = SND_AUDIOPROFILE_AAC,
215                                 .modes = SND_AUDIOMODE_AAC_LC,
216                                 .formats = SND_AUDIOSTREAMFORMAT_RAW,
217                                 .min_buffer = 1024,
218                         },
219                 },
220         },
221 };
222
223 static struct tegra30_avp_audio *avp_audio_ctx;
224
225 /******************************************************************************
226  * PRIVATE FUNCTIONS
227  *****************************************************************************/
228
229 static int tegra30_avp_mem_alloc(struct tegra_offload_mem *mem, size_t size)
230 {
231         mem->virt_addr = dma_alloc_coherent(avp_audio_ctx->dev, size,
232                                                 &mem->phys_addr, GFP_KERNEL);
233         if (!mem->virt_addr) {
234                 dev_err(avp_audio_ctx->dev, "Failed to allocate memory");
235                 return -ENOMEM;
236         }
237         mem->bytes = size;
238         mem->dev = avp_audio_ctx->dev;
239         memset(mem->virt_addr, 0, mem->bytes);
240
241         dev_vdbg(mem->dev, "%s::virt %p phys %llx size %d\n", __func__,
242                 mem->virt_addr, (u64)mem->phys_addr, (int)size);
243         return 0;
244 }
245
246 static void tegra30_avp_mem_free(struct tegra_offload_mem *mem)
247 {
248         if (mem->virt_addr)
249                 dma_free_coherent(mem->dev, mem->bytes,
250                         mem->virt_addr, mem->phys_addr);
251 }
252
253 static int tegra30_avp_load_ucode(void)
254 {
255         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
256         struct audio_engine_data *audio_engine;
257         const struct firmware *ucode_fw;
258         const struct tegra30_avp_ucode_desc *ucode_desc;
259         int ucode_size = 0, ucode_offset = 0, total_ucode_size = 0;
260         int i, ret = 0;
261
262         dev_vdbg(audio_avp->dev, "%s", __func__);
263
264         for (i = 0; i < ARRAY_SIZE(avp_ucode_desc); i++)
265                 total_ucode_size += avp_ucode_desc[i].max_mem_size;
266
267         /* allocate memory for Ucode */
268         ret = tegra30_avp_mem_alloc(&audio_avp->ucode_mem, total_ucode_size);
269         if (ret < 0) {
270                 dev_err(audio_avp->dev, "Failed to allocate ucode memory");
271                 return ret;
272         }
273
274         /* allocate memory for parameters */
275         ret = tegra30_avp_mem_alloc(&audio_avp->param_mem,
276                 sizeof(struct audio_engine_data));
277         if (ret < 0) {
278                 dev_err(audio_avp->dev, "Failed to allocate param memory");
279                 goto err_ucode_mem_free;
280         }
281         audio_engine =
282                 (struct audio_engine_data *)audio_avp->param_mem.virt_addr;
283         audio_avp->audio_engine = audio_engine;
284         audio_engine->codec_ucode_base_address =
285                 audio_avp->ucode_mem.phys_addr;
286
287         for (i = 0; i < ARRAY_SIZE(avp_ucode_desc); i++) {
288                 ucode_desc = &avp_ucode_desc[i];
289
290                 ret = request_firmware(&ucode_fw, ucode_desc->bin_name,
291                         audio_avp->dev);
292                 if (ret < 0) {
293                         dev_err(audio_avp->dev, "cannot read audio firmware %s",
294                                 ucode_desc->bin_name);
295                         goto err_param_mem_free;
296                 }
297
298                 ucode_size = ucode_fw->size;
299                 if (ucode_size <= 0) {
300                         dev_err(audio_avp->dev, "Invalid ucode size.");
301                         ret = -EINVAL;
302                         release_firmware(ucode_fw);
303                         goto err_param_mem_free;
304                 }
305                 dev_vdbg(audio_avp->dev, "%s ucode size = %d bytes",
306                         ucode_desc->bin_name, ucode_size);
307
308                 /* Read ucode */
309                 if (strncmp((const char *)ucode_fw->data, AVP_UCODE_HEADER,
310                         AVP_UCODE_HEADER_SIZE)) {
311                         dev_err(audio_avp->dev, "ucode Header mismatch");
312                         ret = -EINVAL;
313                         release_firmware(ucode_fw);
314                         goto err_param_mem_free;
315                 }
316
317                 memcpy((char *)audio_avp->ucode_mem.virt_addr + ucode_offset,
318                         ucode_fw->data + AVP_UCODE_HEADER_SIZE,
319                         ucode_size - AVP_UCODE_HEADER_SIZE);
320
321                 audio_engine->codec_ucode_desc[i][UCODE_DESC_OFFSET] =
322                                                                 ucode_offset;
323                 audio_engine->codec_ucode_desc[i][UCODE_DESC_SIZE] =
324                                                 ucode_desc->max_mem_size;
325                 ucode_offset += ucode_desc->max_mem_size;
326
327                 release_firmware(ucode_fw);
328         }
329
330         /* allocate memory for command buffer */
331         ret = tegra30_avp_mem_alloc(&audio_avp->cmd_buf_mem,
332                         AVP_CMD_BUFFER_SIZE);
333         if (ret < 0) {
334                 dev_err(audio_avp->dev, "Failed to allocate cmd buffer memory");
335                 goto err_param_mem_free;
336         }
337         audio_avp->cmd_buf = (unsigned int *)audio_avp->cmd_buf_mem.virt_addr;
338
339         /* Set command */
340         audio_avp->cmd_buf_idx = 0;
341         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
342                 NVE26E_CH_OPCODE_INCR(NVE276_SET_MICROCODE_A, 3);
343         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] = 0;
344         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
345                 audio_avp->ucode_mem.phys_addr;
346         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
347                 avp_ucode_desc[CODEC_PCM].max_mem_size - 8;
348         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
349                 NVE26E_CH_OPCODE_INCR(NVE276_PARAMETER_METHOD(0), 1);
350         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
351                 audio_avp->param_mem.phys_addr;
352
353         ret = nvavp_pushbuffer_submit_audio(audio_avp->nvavp_client,
354                                             audio_avp->cmd_buf_mem.phys_addr,
355                                             audio_avp->cmd_buf_idx);
356         if (ret < 0) {
357                 dev_err(audio_avp->dev, "pushbuffer_submit failed %d", ret);
358                 ret = -EINVAL;
359                 goto err_cmd_buf_mem_free;
360         }
361         dev_vdbg(audio_avp->dev, "Successfully loaded audio ucode");
362         return 0;
363
364 err_cmd_buf_mem_free:
365         tegra30_avp_mem_free(&audio_avp->cmd_buf_mem);
366 err_param_mem_free:
367         tegra30_avp_mem_free(&audio_avp->param_mem);
368 err_ucode_mem_free:
369         tegra30_avp_mem_free(&audio_avp->ucode_mem);
370         return ret;
371 }
372
373 static void tegra30_avp_audio_engine_init(void)
374 {
375         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
376         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
377         int i = 0;
378
379         /* Initialize audio_engine_data */
380 #if defined(CONFIG_ARCH_TEGRA_14x_SOC) || defined(CONFIG_ARCH_TEGRA_12x_SOC)
381         audio_engine->chip_id = NV_TEGRA_T148;
382 #else
383         audio_engine->chip_id = NV_TEGRA_T114;
384 #endif
385         audio_engine->device_state_target = KSSTATE_STOP;
386         audio_engine->device_state_current = KSSTATE_STOP;
387
388 #ifdef DEBUG_AVP
389         audio_engine->profile_state = PROFILE_RENDER_OVERALL_PROCESSING;
390 #endif
391         audio_engine->device_format.rate = AVP_INIT_SAMPLE_RATE;
392         audio_engine->device_format.bits_per_sample = 16;
393         audio_engine->device_format.channels = 2;
394
395         /* Initialize stream memory */
396         for (i = 0; i < max_stream_id; i++) {
397                 struct tegra30_avp_stream *avp_stream;
398                 struct stream_data *stream;
399                 int j = 0;
400
401                 avp_stream = &audio_avp->avp_stream[i];
402                 memset(avp_stream, 0, sizeof(struct tegra30_avp_stream));
403
404                 avp_stream->id = i;
405
406                 stream = &audio_engine->stream[avp_stream->id];
407                 avp_stream->stream = stream;
408                 spin_lock_init(&avp_stream->lock);
409
410                 stream->stream_state_target = KSSTATE_STOP;
411                 stream->source_buffer_write_position = 0;
412                 stream->source_buffer_write_count = 0;
413                 stream->stream_params.rate = AVP_INIT_SAMPLE_RATE;
414
415                 for (j = 0; j < RENDERSW_MAX_CHANNELS; j++)
416                         stream->stream_volume[j] = AVP_UNITY_STREAM_VOLUME;
417
418                 stream->source_buffer_read_position = 0;
419                 stream->source_buffer_presentation_position = 0;
420                 stream->source_buffer_linear_position = 0;
421                 stream->source_buffer_presentation_position = 0;
422                 stream->source_buffer_frames_decoded = 0;
423                 stream->stream_state_current = KSSTATE_STOP;
424
425                 stream->stream_params.rate = AVP_INIT_SAMPLE_RATE;
426                 stream->stream_params.bits_per_sample = 16;
427                 stream->stream_params.channels = 2;
428
429                 avp_stream->audio_avp = audio_avp;
430         }
431 }
432
433 static int tegra30_avp_audio_alloc_dma(struct tegra_offload_dma_params *params)
434 {
435         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
436         struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;
437         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
438         dma_cap_mask_t mask;
439         int ret = 0;
440
441         dev_vdbg(audio_avp->dev, "%s: is_dma_allocated %d",
442                         __func__, atomic_read(&dma->is_dma_allocated));
443
444         if (atomic_read(&dma->is_dma_allocated) == 1)
445                 return 0;
446
447         memcpy(&dma->params, params, sizeof(struct tegra_offload_dma_params));
448
449         dma_cap_zero(mask);
450         dma_cap_set(DMA_SLAVE, mask);
451         dma_cap_set(DMA_CYCLIC, mask);
452         dma->chan = dma_request_channel(mask, NULL, NULL);
453         if (dma->chan == NULL) {
454                 dev_err(audio_avp->dev, "Failed to allocate DMA chan.");
455                 return -ENOMEM;
456         }
457
458         /* Only playback is supported */
459         dma->chan_slave_config.direction = DMA_MEM_TO_DEV;
460         dma->chan_slave_config.dst_addr_width = dma->params.width;
461         dma->chan_slave_config.dst_addr = dma->params.addr;
462         dma->chan_slave_config.dst_maxburst = dma->params.max_burst;
463         dma->chan_slave_config.slave_id = dma->params.req_sel;
464
465         ret = dmaengine_slave_config(dma->chan, &dma->chan_slave_config);
466         if (ret < 0) {
467                 dev_err(audio_avp->dev, "dma slave config failed.err %d.", ret);
468                 return ret;
469         }
470         audio_engine->apb_channel_handle = dma->chan->chan_id;
471         atomic_set(&dma->is_dma_allocated, 1);
472
473         return 0;
474 }
475
476 static void tegra30_avp_audio_free_dma(void)
477 {
478         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
479         struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;
480
481         dev_vdbg(audio_avp->dev, "%s: is_dma_allocated %d",
482                         __func__, atomic_read(&dma->is_dma_allocated));
483
484         if (atomic_read(&dma->is_dma_allocated) == 1) {
485                 dma_release_channel(dma->chan);
486                 atomic_set(&dma->is_dma_allocated, 0);
487         }
488
489         return;
490 }
491
492 static int tegra30_avp_audio_start_dma(void)
493 {
494         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
495         struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;
496         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
497
498         dev_vdbg(audio_avp->dev, "%s: active %d", __func__,
499                         atomic_read(&dma->active_count));
500
501         if (atomic_inc_return(&dma->active_count) > 1)
502                 return 0;
503
504         dma->chan_desc = dmaengine_prep_dma_cyclic(dma->chan,
505                                 (dma_addr_t)audio_engine->device_buffer_avp,
506                                 DEVICE_BUFFER_SIZE,
507                                 DEVICE_BUFFER_SIZE,
508                                 dma->chan_slave_config.direction,
509                                 DMA_CTRL_ACK);
510         if (!dma->chan_desc) {
511                 dev_err(audio_avp->dev, "Failed to prep cyclic dma");
512                 return -ENODEV;
513         }
514         dma->chan_cookie = dmaengine_submit(dma->chan_desc);
515         dma_async_issue_pending(dma->chan);
516         return 0;
517 }
518
519 static int tegra30_avp_audio_stop_dma(void)
520 {
521         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
522         struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;
523
524         dev_vdbg(audio_avp->dev, "%s: active %d.", __func__,
525                         atomic_read(&dma->active_count));
526
527         if (atomic_dec_and_test(&dma->active_count))
528                 dmaengine_terminate_all(dma->chan);
529
530         return 0;
531 }
532
533 static int tegra30_avp_audio_execute(void)
534 {
535         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
536         uint32_t cmd_idx = audio_avp->cmd_buf_idx;
537         int ret = 0;
538
539         dev_vdbg(audio_avp->dev, "%s", __func__);
540
541         /* Set command */
542         audio_avp->cmd_buf[cmd_idx++] =
543                 NVE26E_CH_OPCODE_INCR(NVE276_EXECUTE, 1);
544         audio_avp->cmd_buf[cmd_idx++] = 0; /* NVE276_EXECUTE_APPID */
545
546         ret = nvavp_pushbuffer_submit_audio(audio_avp->nvavp_client,
547                 audio_avp->cmd_buf_mem.phys_addr,
548                 cmd_idx);
549         if (ret < 0) {
550                 dev_err(audio_avp->dev, "nvavp_pushbuffer_submit_audio failed");
551                 return -EINVAL;
552         }
553         return 0;
554 }
555
556 static int tegra30_avp_audio_set_state(enum KSSTATE new_state)
557 {
558         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
559         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
560         enum KSSTATE old_state;
561
562         old_state = audio_engine->device_state_target;
563
564         dev_vdbg(audio_avp->dev, "%s : state %d -> %d",
565                 __func__, old_state, new_state);
566
567         if (old_state == new_state)
568                 return 0;
569
570         audio_engine->device_state_target = new_state;
571         /* TODO : Need a way to wait till AVP device state changes */
572         if (new_state == KSSTATE_RUN)
573                 tegra30_avp_audio_execute();
574
575         return 0;
576 }
577
578
579 /* Call this function with stream lock held */
580 static int tegra30_avp_stream_set_state(int id, enum KSSTATE new_state)
581 {
582         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
583         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
584         struct stream_data *stream = avp_stream->stream;
585         enum KSSTATE old_state = stream->stream_state_target;
586         int ret = 0;
587
588         dev_vdbg(audio_avp->dev, "%s : id %d state %d -> %d", __func__, id,
589                  old_state, new_state);
590
591         if (old_state == new_state)
592                 return 0;
593
594         if (old_state == KSSTATE_STOP) {
595                 switch (stream->stream_format) {
596                 case FORMAT_MP3:
597                         nvavp_enable_audio_clocks(audio_avp->nvavp_client,
598                                 NVAVP_MODULE_ID_VCP);
599                         break;
600                 case FORMAT_AAC:
601                         nvavp_enable_audio_clocks(audio_avp->nvavp_client,
602                                 NVAVP_MODULE_ID_VCP);
603                         break;
604                 default:
605                         break;
606                 }
607         }
608
609         stream->stream_state_target = new_state;
610         /* TODO : Need a way to wait till AVP stream state changes */
611
612         if (new_state == KSSTATE_STOP) {
613                 switch (stream->stream_format) {
614                 case FORMAT_MP3:
615                         nvavp_disable_audio_clocks(audio_avp->nvavp_client,
616                                 NVAVP_MODULE_ID_VCP);
617                         break;
618                 case FORMAT_AAC:
619                         nvavp_disable_audio_clocks(audio_avp->nvavp_client,
620                                 NVAVP_MODULE_ID_VCP);
621                         break;
622                 default:
623                         break;
624                 }
625         }
626
627         if (new_state == KSSTATE_STOP) {
628                 stream->source_buffer_write_position = 0;
629                 stream->source_buffer_write_count = 0;
630                 avp_stream->last_notification_offset = 0;
631                 avp_stream->notification_received = 0;
632                 avp_stream->source_buffer_offset = 0;
633         }
634
635         if (new_state == KSSTATE_RUN)
636                 tegra30_avp_audio_start_dma();
637         else if (old_state == KSSTATE_RUN)
638                 tegra30_avp_audio_stop_dma();
639
640         return ret;
641 }
642
643 /* TODO : review ISR to make it more optimized if possible */
644 static void tegra30_avp_stream_notify(void)
645 {
646         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
647         struct tegra30_avp_stream *avp_stream;
648         struct stream_data *stream;
649         int i;
650
651         /* dev_vdbg(audio_avp->dev, "tegra30_avp_stream_notify"); */
652
653         for (i = 0; i < max_stream_id; i++) {
654                 avp_stream = &audio_avp->avp_stream[i];
655                 stream = avp_stream->stream;
656
657                 if (!stream->stream_allocated)
658                         continue;
659
660                 if (avp_stream->is_drain_called &&
661                    (stream->source_buffer_read_position ==
662                         stream->source_buffer_write_position) &&
663                    (avp_stream->notification_received >=
664                         stream->stream_notification_request)) {
665                         /* End of stream occured and noitfy same with value 1 */
666                         avp_stream->notify_cb(avp_stream->notify_args, 1);
667                         tegra30_avp_stream_set_state(i, KSSTATE_STOP);
668                 } else if (stream->stream_notification_request >
669                         avp_stream->notification_received) {
670                         avp_stream->notification_received++;
671
672                         avp_stream->notify_cb(avp_stream->notify_args, 0);
673                 }
674         }
675 }
676
677 /******************************************************************************
678  * EXPORTED FUNCTIONS
679  *****************************************************************************/
680 /* Device APIs */
681 static int tegra30_avp_set_hw_rate(int rate)
682 {
683         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
684         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
685
686         dev_vdbg(audio_avp->dev, "%s rate %d", __func__, rate);
687
688         if (!audio_engine) {
689                 dev_err(audio_avp->dev, "AVP platform not initialized.");
690                 return -ENODEV;
691         }
692
693         audio_engine->device_format.rate = rate;
694         return 0;
695 }
696
697 static int tegra30_avp_alloc_shared_mem(struct tegra_offload_mem *mem,
698                                         int bytes)
699 {
700         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
701         int ret = 0;
702
703         ret = tegra30_avp_mem_alloc(mem, bytes);
704         if (ret < 0) {
705                 dev_err(audio_avp->dev, "%s: Failed. ret %d", __func__, ret);
706                 return ret;
707         }
708         return 0;
709 }
710
711 static void tegra30_avp_free_shared_mem(struct tegra_offload_mem *mem)
712 {
713         tegra30_avp_mem_free(mem);
714 }
715
716 /* PCM APIs */
717 static int tegra30_avp_pcm_open(int *id)
718 {
719         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
720         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
721         int ret = 0;
722
723         dev_vdbg(audio_avp->dev, "%s", __func__);
724
725         if (!audio_avp->nvavp_client) {
726                 ret = tegra_nvavp_audio_client_open(&audio_avp->nvavp_client);
727                 if (ret < 0) {
728                         dev_err(audio_avp->dev, "Failed to open nvavp.");
729                         return ret;
730                 }
731         }
732         if (!audio_engine) {
733                 ret = tegra30_avp_load_ucode();
734                 if (ret < 0) {
735                         dev_err(audio_avp->dev, "Failed to load ucode.");
736                         return ret;
737                 }
738                 tegra30_avp_audio_engine_init();
739                 nvavp_register_audio_cb(audio_avp->nvavp_client,
740                         tegra30_avp_stream_notify);
741                 audio_engine = audio_avp->audio_engine;
742         }
743
744         if (!audio_engine->stream[pcm_stream_id].stream_allocated)
745                 *id = pcm_stream_id;
746         else if (!audio_engine->stream[pcm2_stream_id].stream_allocated)
747                 *id = pcm2_stream_id;
748         else {
749                 dev_err(audio_avp->dev, "All AVP PCM streams are busy");
750                 return -EBUSY;
751         }
752
753         audio_engine->stream[*id].stream_allocated = 1;
754
755         atomic_inc(&audio_avp->stream_active_count);
756         tegra30_avp_audio_set_state(KSSTATE_RUN);
757
758         return 0;
759 }
760
761 static int tegra30_avp_pcm_set_params(int id,
762                 struct tegra_offload_pcm_params *params)
763 {
764         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
765         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
766         struct stream_data *stream = avp_stream->stream;
767         int ret = 0;
768
769         dev_vdbg(audio_avp->dev,
770                 "%s id %d rate %d chan %d bps %d period size %d buf size %d",
771                  __func__, id, params->rate, params->channels,
772                  params->bits_per_sample, params->period_size,
773                  params->buffer_size);
774
775         /* TODO : check validity of parameters */
776         if (!stream) {
777                 dev_err(audio_avp->dev, "AVP platform not initialized.");
778                 return -ENODEV;
779         }
780
781         stream->stream_notification_interval = params->period_size;
782         stream->stream_notification_enable = 1;
783         stream->stream_params.rate = params->rate;
784         stream->stream_params.channels = params->channels;
785         stream->stream_params.bits_per_sample = params->bits_per_sample;
786         avp_stream->period_size = params->period_size;
787
788         avp_stream->notify_cb = params->period_elapsed_cb;
789         avp_stream->notify_args = params->period_elapsed_args;
790
791         stream->source_buffer_system =
792                         (uintptr_t) (params->source_buf.virt_addr);
793         stream->source_buffer_avp = params->source_buf.phys_addr;
794         stream->source_buffer_size = params->buffer_size;
795
796         /* Set DMA params */
797         ret = tegra30_avp_audio_alloc_dma(&params->dma_params);
798         if (ret < 0) {
799                 dev_err(audio_avp->dev, "Failed to allocate DMA. ret %d", ret);
800                 return ret;
801         }
802         return 0;
803 }
804
805 static int tegra30_avp_pcm_set_state(int id, int state)
806 {
807         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
808         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
809         struct stream_data *stream = avp_stream->stream;
810
811         dev_vdbg(audio_avp->dev, "%s : id %d state %d", __func__, id, state);
812
813         if (!stream) {
814                 dev_err(audio_avp->dev, "AVP platform not initialized.");
815                 return -ENODEV;
816         }
817
818         switch (state) {
819         case SNDRV_PCM_TRIGGER_START:
820         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
821         case SNDRV_PCM_TRIGGER_RESUME:
822                 tegra30_avp_stream_set_state(id, KSSTATE_RUN);
823                 return 0;
824         case SNDRV_PCM_TRIGGER_STOP:
825         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
826         case SNDRV_PCM_TRIGGER_SUSPEND:
827                 tegra30_avp_stream_set_state(id, KSSTATE_STOP);
828                 return 0;
829         default:
830                 dev_err(audio_avp->dev, "Unsupported state.");
831                 return -EINVAL;
832         }
833 }
834
835 static void tegra30_avp_pcm_data_ready(int id, int bytes)
836 {
837         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
838         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
839         struct stream_data *stream = avp_stream->stream;
840
841         dev_vdbg(audio_avp->dev, "%s :id %d size %d", __func__, id, bytes);
842
843         stream->source_buffer_write_position += bytes;
844         stream->source_buffer_write_position %= stream->source_buffer_size;
845
846         avp_stream->source_buffer_offset += bytes;
847         while (avp_stream->source_buffer_offset >=
848                 stream->stream_notification_interval) {
849                 stream->source_buffer_write_count++;
850                 avp_stream->source_buffer_offset -=
851                         stream->stream_notification_interval;
852         }
853         return;
854 }
855
856 static size_t tegra30_avp_pcm_get_position(int id)
857 {
858         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
859         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
860         struct stream_data *stream = avp_stream->stream;
861         size_t pos = 0;
862
863         pos = (size_t)stream->source_buffer_read_position;
864
865         dev_vdbg(audio_avp->dev, "%s id %d pos %d", __func__, id, (u32)pos);
866
867         return pos;
868 }
869
870 /* Compress APIs */
871 static int tegra30_avp_compr_open(int *id)
872 {
873         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
874         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
875         int ret = 0;
876
877         dev_vdbg(audio_avp->dev, "%s", __func__);
878
879         if (!audio_avp->nvavp_client) {
880                 ret = tegra_nvavp_audio_client_open(&audio_avp->nvavp_client);
881                 if (ret < 0) {
882                         dev_err(audio_avp->dev, "Failed to open nvavp.");
883                         return ret;
884                 }
885         }
886         if (!audio_engine) {
887                 ret = tegra30_avp_load_ucode();
888                 if (ret < 0) {
889                         dev_err(audio_avp->dev, "Failed to load ucode.");
890                         return ret;
891                 }
892                 tegra30_avp_audio_engine_init();
893                 nvavp_register_audio_cb(audio_avp->nvavp_client,
894                         tegra30_avp_stream_notify);
895                 audio_engine = audio_avp->audio_engine;
896         }
897
898         if (!audio_engine->stream[decode_stream_id].stream_allocated)
899                 *id = decode_stream_id;
900         else if (!audio_engine->stream[decode2_stream_id].stream_allocated)
901                 *id = decode2_stream_id;
902         else {
903                 dev_err(audio_avp->dev, "All AVP COMPR streams are busy");
904                 return -EBUSY;
905         }
906         audio_avp->avp_stream[*id].is_drain_called = 0;
907         audio_engine->stream[*id].stream_allocated = 1;
908
909         atomic_inc(&audio_avp->stream_active_count);
910         tegra30_avp_audio_set_state(KSSTATE_RUN);
911
912         return 0;
913 }
914
915 static int tegra30_avp_compr_set_params(int id,
916                 struct tegra_offload_compr_params *params)
917 {
918         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
919         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
920         struct stream_data *stream = avp_stream->stream;
921         int ret = 0;
922
923         dev_vdbg(audio_avp->dev,
924                 "%s: id %d codec %d rate %d ch %d bps %d buf %d x %d",
925                  __func__, id, params->codec_type, params->rate,
926                  params->channels, params->bits_per_sample,
927                  params->fragment_size, params->fragments);
928
929         /* TODO : check validity of parameters */
930         if (!stream) {
931                 dev_err(audio_avp->dev, "AVP platform not initialized.");
932                 return -ENODEV;
933         }
934
935         if (params->codec_type == SND_AUDIOCODEC_MP3) {
936                 stream->stream_format = FORMAT_MP3;
937         } else if (params->codec_type == SND_AUDIOCODEC_AAC) {
938                 stream->stream_format = FORMAT_AAC;
939
940                 /* AAC-LC is only supported profile*/
941                 stream->u.aac.audio_profile = AAC_PROFILE_LC;
942                 stream->u.aac.sampling_freq = params->rate;
943                 stream->u.aac.payload_type  = AAC_PAYLOAD_RAW;
944                 switch (params->rate) {
945                 case 8000:
946                         stream->u.aac.sampling_freq_index = 0xb;
947                         break;
948                 case 11025:
949                         stream->u.aac.sampling_freq_index = 0xa;
950                         break;
951                 case 12000:
952                         stream->u.aac.sampling_freq_index = 0x9;
953                         break;
954                 case 16000:
955                         stream->u.aac.sampling_freq_index = 0x8;
956                         break;
957                 case 22050:
958                         stream->u.aac.sampling_freq_index = 0x7;
959                         break;
960                 case 24000:
961                         stream->u.aac.sampling_freq_index = 0x6;
962                         break;
963                 case 32000:
964                         stream->u.aac.sampling_freq_index = 0x5;
965                         break;
966                 case 44100:
967                         stream->u.aac.sampling_freq_index = 0x4;
968                         break;
969                 case 48000:
970                         stream->u.aac.sampling_freq_index = 0x3;
971                         break;
972                 case 64000:
973                         stream->u.aac.sampling_freq_index = 0x2;
974                         break;
975                 case 88200:
976                         stream->u.aac.sampling_freq_index = 0x1;
977                         break;
978                 case 96000:
979                         stream->u.aac.sampling_freq_index = 0x0;
980                         break;
981                 default:
982                         dev_err(audio_avp->dev, "Unsupported rate");
983                         return -EINVAL;
984                 }
985                 /* Only Stereo data is supported */
986                 stream->u.aac.channel_configuration = params->channels;
987         } else {
988                 dev_err(audio_avp->dev, "Invalid stream/codec type.");
989                 return -EINVAL;
990         }
991
992         stream->stream_params.rate = params->rate;
993         stream->stream_params.channels = params->channels;
994         stream->stream_params.bits_per_sample = params->bits_per_sample;
995         avp_stream->period_size = params->fragment_size;
996
997         avp_stream->notify_cb = params->fragments_elapsed_cb;
998         avp_stream->notify_args = params->fragments_elapsed_args;
999
1000         stream->source_buffer_size = (params->fragments *
1001                         params->fragment_size);
1002         ret = tegra30_avp_mem_alloc(&avp_stream->source_buf,
1003                               stream->source_buffer_size);
1004         if (ret < 0) {
1005                 dev_err(audio_avp->dev, "Failed to allocate source buf memory");
1006                 return ret;
1007         }
1008
1009         stream->source_buffer_system =
1010                         (uintptr_t) avp_stream->source_buf.virt_addr;
1011         stream->source_buffer_avp = avp_stream->source_buf.phys_addr;
1012
1013         if (stream->source_buffer_size > AVP_COMPR_THRESHOLD) {
1014                 stream->stream_notification_interval =
1015                         stream->source_buffer_size - AVP_COMPR_THRESHOLD;
1016         } else {
1017                 stream->stream_notification_interval = avp_stream->period_size;
1018         }
1019         stream->stream_notification_enable = 1;
1020
1021         /* Set DMA params */
1022         ret = tegra30_avp_audio_alloc_dma(&params->dma_params);
1023         if (ret < 0) {
1024                 dev_err(audio_avp->dev, "Failed to allocate DMA. ret %d", ret);
1025                 return ret;
1026         }
1027
1028         if ((params->codec_type == SND_AUDIOCODEC_MP3) ||
1029             (params->codec_type == SND_AUDIOCODEC_AAC)) {
1030                 dev_info(audio_avp->dev, "\n*** STARTING %s Offload PLAYBACK ***\n",
1031                    (params->codec_type == SND_AUDIOCODEC_MP3) ? "MP3" : "AAC");
1032         }
1033         return 0;
1034 }
1035
1036 static int tegra30_avp_compr_set_state(int id, int state)
1037 {
1038         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1039         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1040
1041         dev_vdbg(audio_avp->dev, "%s : id %d state %d",
1042                  __func__, id, state);
1043
1044         switch (state) {
1045         case SNDRV_PCM_TRIGGER_START:
1046         case SNDRV_PCM_TRIGGER_RESUME:
1047                 tegra30_avp_stream_set_state(id, KSSTATE_RUN);
1048                 return 0;
1049         case SNDRV_PCM_TRIGGER_STOP:
1050         case SNDRV_PCM_TRIGGER_SUSPEND:
1051                 tegra30_avp_stream_set_state(id, KSSTATE_STOP);
1052                 return 0;
1053         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1054                 tegra30_avp_stream_set_state(id, KSSTATE_PAUSE);
1055                 return 0;
1056         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1057                 tegra30_avp_stream_set_state(id, KSSTATE_RUN);
1058                 return 0;
1059         case SND_COMPR_TRIGGER_DRAIN:
1060         case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
1061                 avp_stream->is_drain_called = 1;
1062                 return 0;
1063         default:
1064                 dev_err(audio_avp->dev, "Unsupported state.");
1065                 return -EINVAL;
1066         }
1067 }
1068
1069 static void tegra30_avp_compr_data_ready(int id, int bytes)
1070 {
1071         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1072         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1073         struct stream_data *stream = avp_stream->stream;
1074
1075         dev_vdbg(audio_avp->dev, "%s : id %d size %d", __func__, id, bytes);
1076
1077         stream->source_buffer_write_position += bytes;
1078         stream->source_buffer_write_position %= stream->source_buffer_size;
1079
1080         avp_stream->source_buffer_offset += bytes;
1081         while (avp_stream->source_buffer_offset >=
1082                 stream->stream_notification_interval) {
1083                 stream->source_buffer_write_count++;
1084                 avp_stream->source_buffer_offset -=
1085                         stream->stream_notification_interval;
1086         }
1087         return;
1088 }
1089
1090 static int tegra30_avp_compr_write(int id, char __user *buf, int bytes)
1091 {
1092         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1093         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1094         struct stream_data *stream = avp_stream->stream;
1095         void *dst = (char *)(uintptr_t)stream->source_buffer_system +
1096                 stream->source_buffer_write_position;
1097         int avail = 0;
1098         int write = 0;
1099         int ret = 0;
1100
1101         avail = stream->source_buffer_read_position -
1102                 stream->source_buffer_write_position;
1103         if ((avail < 0) || (!avail &&
1104                 (stream->source_buffer_write_count ==
1105                 stream->stream_notification_request)))
1106                 avail += stream->source_buffer_size;
1107
1108         dev_vdbg(audio_avp->dev, "%s : id %d size %d", __func__, id, bytes);
1109
1110         /* TODO: check enough free space is available before writing */
1111         bytes = (bytes > avail) ? avail : bytes;
1112         if (!bytes) {
1113                 dev_dbg(audio_avp->dev, "No free space in ring buffer.");
1114                 DUMP_AVP_STATUS(avp_stream);
1115                 return bytes;
1116         }
1117
1118         write = stream->source_buffer_size -
1119                 stream->source_buffer_write_position;
1120         if (write > bytes) {
1121                 ret = copy_from_user(dst, buf, bytes);
1122                 if (ret < 0) {
1123                         dev_err(audio_avp->dev, "Failed to copy user data.");
1124                         return -EFAULT;
1125                 }
1126         } else {
1127                 ret = copy_from_user(dst, buf, write);
1128                 if (ret < 0) {
1129                         dev_err(audio_avp->dev, "Failed to copy user data.");
1130                         return -EFAULT;
1131                 }
1132
1133                 ret = copy_from_user((void *)(uintptr_t)stream->source_buffer_system,
1134                                 buf + write, bytes - write);
1135                 if (ret < 0) {
1136                         dev_err(audio_avp->dev, "Failed to copy user data.");
1137                         return -EFAULT;
1138                 }
1139         }
1140
1141         stream->source_buffer_write_position += bytes;
1142         stream->source_buffer_write_position %= stream->source_buffer_size;
1143
1144         avp_stream->source_buffer_offset += bytes;
1145         while (avp_stream->source_buffer_offset >=
1146                 stream->stream_notification_interval) {
1147                 stream->source_buffer_write_count++;
1148                 avp_stream->source_buffer_offset -=
1149                         stream->stream_notification_interval;
1150         }
1151         DUMP_AVP_STATUS(avp_stream);
1152         return bytes;
1153 }
1154
1155 static int tegra30_avp_compr_get_position(int id,
1156                         struct snd_compr_tstamp *tstamp)
1157 {
1158         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1159         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1160         struct stream_data *stream = avp_stream->stream;
1161
1162         tstamp->byte_offset = stream->source_buffer_write_position;
1163         tstamp->copied_total = stream->source_buffer_write_position +
1164                 (stream->source_buffer_write_count *
1165                  stream->stream_notification_interval);
1166         tstamp->pcm_frames = stream->source_buffer_presentation_position;
1167         tstamp->pcm_io_frames = stream->source_buffer_presentation_position;
1168         tstamp->sampling_rate = stream->stream_params.rate;
1169
1170         dev_vdbg(audio_avp->dev, "%s id %d off %d copied %d pcm %d pcm io %d",
1171                  __func__, id, (int)tstamp->byte_offset,
1172                  (int)tstamp->copied_total, (int)tstamp->pcm_frames,
1173                  (int)tstamp->pcm_io_frames);
1174
1175         return 0;
1176 }
1177
1178 static int tegra30_avp_compr_get_caps(struct snd_compr_caps *caps)
1179 {
1180         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1181
1182         dev_vdbg(audio_avp->dev, "%s : dir %d", __func__, caps->direction);
1183
1184         if (caps->direction == SND_COMPRESS_PLAYBACK)
1185                 memcpy(caps, &tegra30_avp_compr_caps[SND_COMPRESS_PLAYBACK],
1186                         sizeof(struct snd_compr_caps));
1187         else
1188                 memcpy(caps, &tegra30_avp_compr_caps[SND_COMPRESS_CAPTURE],
1189                         sizeof(struct snd_compr_caps));
1190         return 0;
1191 }
1192
1193 static int tegra30_avp_compr_get_codec_caps(struct snd_compr_codec_caps *codec)
1194 {
1195         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1196
1197         dev_vdbg(audio_avp->dev, "%s : codec %d", __func__, codec->codec);
1198
1199         switch (codec->codec) {
1200         case SND_AUDIOCODEC_MP3:
1201                 memcpy(codec, &tegra30_avp_compr_codec_caps[avp_compr_mp3],
1202                         sizeof(struct snd_compr_codec_caps));
1203                 return 0;
1204         case SND_AUDIOCODEC_AAC:
1205                 memcpy(codec, &tegra30_avp_compr_codec_caps[avp_compr_aac],
1206                         sizeof(struct snd_compr_codec_caps));
1207                 return 0;
1208         default:
1209                 dev_err(audio_avp->dev, "Unsupported codec %d", codec->codec);
1210                 return -EINVAL;
1211         }
1212 }
1213
1214 static int tegra30_avp_compr_set_volume(int id, int left, int right)
1215 {
1216         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1217         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1218         struct stream_data *stream = avp_stream->stream;
1219
1220         dev_vdbg(audio_avp->dev, "%s id %d left vol %d right vol %d",
1221                  __func__, id, left, right);
1222
1223         if (!stream) {
1224                 dev_err(audio_avp->dev, "AVP platform not initialized.");
1225                 return -ENODEV;
1226         }
1227
1228         stream->stream_volume[0] = left;
1229         stream->stream_volume[1] = right;
1230
1231         return 0;
1232 }
1233
1234 /* Common APIs for pcm and compress */
1235 static void tegra30_avp_stream_close(int id)
1236 {
1237         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1238         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1239         struct stream_data *stream = avp_stream->stream;
1240
1241         dev_vdbg(audio_avp->dev, "%s id %d", __func__, id);
1242
1243         if (!stream) {
1244                 dev_err(audio_avp->dev, "AVP platform not initialized.");
1245                 return;
1246         }
1247         tegra30_avp_mem_free(&avp_stream->source_buf);
1248         stream->stream_allocated = 0;
1249         tegra30_avp_stream_set_state(id, KSSTATE_STOP);
1250
1251         if (atomic_dec_and_test(&audio_avp->stream_active_count)) {
1252                 tegra30_avp_audio_free_dma();
1253                 tegra30_avp_audio_set_state(KSSTATE_STOP);
1254         }
1255 }
1256
1257 static struct tegra_offload_ops avp_audio_platform = {
1258         .device_ops = {
1259                 .set_hw_rate = tegra30_avp_set_hw_rate,
1260                 .alloc_shared_mem = tegra30_avp_alloc_shared_mem,
1261                 .free_shared_mem = tegra30_avp_free_shared_mem,
1262         },
1263         .pcm_ops = {
1264                 .stream_open = tegra30_avp_pcm_open,
1265                 .stream_close = tegra30_avp_stream_close,
1266                 .set_stream_params = tegra30_avp_pcm_set_params,
1267                 .set_stream_state = tegra30_avp_pcm_set_state,
1268                 .get_stream_position = tegra30_avp_pcm_get_position,
1269                 .data_ready = tegra30_avp_pcm_data_ready,
1270         },
1271         .compr_ops = {
1272                 .stream_open = tegra30_avp_compr_open,
1273                 .stream_close = tegra30_avp_stream_close,
1274                 .set_stream_params = tegra30_avp_compr_set_params,
1275                 .set_stream_state = tegra30_avp_compr_set_state,
1276                 .get_stream_position = tegra30_avp_compr_get_position,
1277                 .data_ready = tegra30_avp_compr_data_ready,
1278                 .write = tegra30_avp_compr_write,
1279                 .get_caps = tegra30_avp_compr_get_caps,
1280                 .get_codec_caps = tegra30_avp_compr_get_codec_caps,
1281                 .set_stream_volume = tegra30_avp_compr_set_volume,
1282         },
1283 };
1284
1285 static u64 tegra_dma_mask = DMA_BIT_MASK(32);
1286 static int tegra30_avp_audio_probe(struct platform_device *pdev)
1287 {
1288         struct tegra30_avp_audio *audio_avp;
1289
1290         pr_debug("tegra30_avp_audio_platform_probe platform probe started\n");
1291
1292         audio_avp = devm_kzalloc(&pdev->dev, sizeof(*audio_avp), GFP_KERNEL);
1293         if (!audio_avp) {
1294                 dev_err(&pdev->dev, "Can't allocate tegra30_avp_audio\n");
1295                 return -ENOMEM;
1296         }
1297         dev_set_drvdata(&pdev->dev, audio_avp);
1298         audio_avp->dev = &pdev->dev;
1299
1300         /* set the ops */
1301         if (tegra_register_offload_ops(&avp_audio_platform)) {
1302                 dev_err(&pdev->dev, "Failed to register avp audio device.");
1303                 return -EPROBE_DEFER;
1304         }
1305
1306         spin_lock_init(&audio_avp->lock);
1307         pdev->dev.dma_mask = &tegra_dma_mask;
1308         pdev->dev.coherent_dma_mask = tegra_dma_mask;
1309         avp_audio_ctx = audio_avp;
1310         pr_info("tegra30_avp_audio_platform_probe successful.");
1311         return 0;
1312 }
1313
1314 static int tegra30_avp_audio_remove(struct platform_device *pdev)
1315 {
1316         struct tegra30_avp_audio *audio_avp = dev_get_drvdata(&pdev->dev);
1317
1318         dev_vdbg(&pdev->dev, "%s", __func__);
1319
1320         tegra_nvavp_audio_client_release(audio_avp->nvavp_client);
1321         tegra30_avp_mem_free(&audio_avp->cmd_buf_mem);
1322         tegra30_avp_mem_free(&audio_avp->param_mem);
1323         tegra30_avp_mem_free(&audio_avp->ucode_mem);
1324
1325         return 0;
1326 }
1327
1328 static const struct of_device_id tegra30_avp_audio_of_match[] = {
1329         { .compatible = "nvidia,tegra30-avp-audio", },
1330         {},
1331 };
1332
1333 static struct platform_driver tegra30_avp_audio_driver = {
1334         .driver = {
1335                 .name = DRV_NAME,
1336                 .owner = THIS_MODULE,
1337                 .of_match_table = tegra30_avp_audio_of_match,
1338         },
1339         .probe = tegra30_avp_audio_probe,
1340         .remove = tegra30_avp_audio_remove,
1341 };
1342 module_platform_driver(tegra30_avp_audio_driver);
1343
1344 MODULE_AUTHOR("Sumit Bhattacharya <sumitb@nvidia.com>");
1345 MODULE_DESCRIPTION("Tegra30 AVP Audio driver");
1346 MODULE_LICENSE("GPL");
1347 MODULE_ALIAS("platform:" DRV_NAME);
1348 MODULE_DEVICE_TABLE(of, tegra30_avp_audio_of_match);