ASoC: tegra: check ucode upper limit
[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-2018, 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 = {
176                                         [0] = SNDRV_PCM_RATE_44100 |
177                                                 SNDRV_PCM_RATE_48000,
178                                 },
179                                 .bit_rate = {
180                                         [0] = 32000,
181                                         [1] = 64000,
182                                         [2] = 128000,
183                                         [3] = 256000,
184                                         [4] = 320000,
185                                 },
186                                 .num_bitrates = 5,
187                                 .rate_control =
188                                         SND_RATECONTROLMODE_CONSTANTBITRATE |
189                                         SND_RATECONTROLMODE_VARIABLEBITRATE,
190                                 .profiles = 0,
191                                 .modes = SND_AUDIOCHANMODE_MP3_STEREO,
192                                 .formats = SND_AUDIOSTREAMFORMAT_UNDEFINED,
193                                 .min_buffer = 1024,
194                         },
195                 },
196         },
197         [avp_compr_aac] = {
198                 .codec = SND_AUDIOCODEC_AAC,
199                 .num_descriptors = 1,
200                 .descriptor = {
201                         [0] = {
202                                 .max_ch = 2,
203                                 .sample_rates = {
204                                         [0] = SNDRV_PCM_RATE_44100 |
205                                                 SNDRV_PCM_RATE_48000,
206                                 },
207                                 .bit_rate = {
208                                         [0] = 32000,
209                                         [1] = 64000,
210                                         [2] = 128000,
211                                         [3] = 256000,
212                                         [4] = 320000,
213                                 },
214                                 .num_bitrates = 5,
215                                 .rate_control =
216                                         SND_RATECONTROLMODE_CONSTANTBITRATE |
217                                         SND_RATECONTROLMODE_VARIABLEBITRATE,
218                                 .profiles = SND_AUDIOPROFILE_AAC,
219                                 .modes = SND_AUDIOMODE_AAC_LC,
220                                 .formats = SND_AUDIOSTREAMFORMAT_RAW,
221                                 .min_buffer = 1024,
222                         },
223                 },
224         },
225 };
226
227 static struct tegra30_avp_audio *avp_audio_ctx;
228
229 /******************************************************************************
230  * PRIVATE FUNCTIONS
231  *****************************************************************************/
232
233 static int tegra30_avp_mem_alloc(struct tegra_offload_mem *mem, size_t size)
234 {
235         mem->virt_addr = dma_alloc_coherent(avp_audio_ctx->dev, size,
236                                                 &mem->phys_addr, GFP_KERNEL);
237         if (!mem->virt_addr) {
238                 dev_err(avp_audio_ctx->dev, "Failed to allocate memory");
239                 return -ENOMEM;
240         }
241         mem->bytes = size;
242         mem->dev = avp_audio_ctx->dev;
243         memset(mem->virt_addr, 0, mem->bytes);
244
245         dev_vdbg(mem->dev, "%s::virt %p phys %llx size %d\n", __func__,
246                 mem->virt_addr, (u64)mem->phys_addr, (int)size);
247         return 0;
248 }
249
250 static void tegra30_avp_mem_free(struct tegra_offload_mem *mem)
251 {
252         if (mem->virt_addr)
253                 dma_free_coherent(mem->dev, mem->bytes,
254                         mem->virt_addr, mem->phys_addr);
255 }
256
257 static int tegra30_avp_load_ucode(void)
258 {
259         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
260         struct audio_engine_data *audio_engine;
261         const struct firmware *ucode_fw;
262         const struct tegra30_avp_ucode_desc *ucode_desc;
263         ssize_t ucode_size = 0, ucode_offset = 0, total_ucode_size = 0;
264         int i, ret = 0;
265
266         dev_vdbg(audio_avp->dev, "%s", __func__);
267
268         for (i = 0; i < ARRAY_SIZE(avp_ucode_desc); i++)
269                 total_ucode_size += avp_ucode_desc[i].max_mem_size;
270
271         /* allocate memory for Ucode */
272         ret = tegra30_avp_mem_alloc(&audio_avp->ucode_mem, total_ucode_size);
273         if (ret < 0) {
274                 dev_err(audio_avp->dev, "Failed to allocate ucode memory");
275                 return ret;
276         }
277
278         /* allocate memory for parameters */
279         ret = tegra30_avp_mem_alloc(&audio_avp->param_mem,
280                 sizeof(struct audio_engine_data));
281         if (ret < 0) {
282                 dev_err(audio_avp->dev, "Failed to allocate param memory");
283                 goto err_ucode_mem_free;
284         }
285         audio_engine =
286                 (struct audio_engine_data *)audio_avp->param_mem.virt_addr;
287         audio_avp->audio_engine = audio_engine;
288         audio_engine->codec_ucode_base_address =
289                 audio_avp->ucode_mem.phys_addr;
290
291         for (i = 0; i < ARRAY_SIZE(avp_ucode_desc); i++) {
292                 ucode_desc = &avp_ucode_desc[i];
293
294                 ret = request_firmware(&ucode_fw, ucode_desc->bin_name,
295                         audio_avp->dev);
296                 if (ret < 0) {
297                         dev_err(audio_avp->dev, "cannot read audio firmware %s",
298                                 ucode_desc->bin_name);
299                         goto err_param_mem_free;
300                 }
301
302                 ucode_size = ucode_fw->size;
303                 if (ucode_size <= 0 ||
304                         ucode_size > avp_ucode_desc[i].max_mem_size) {
305                         dev_err(audio_avp->dev, "Invalid ucode size.");
306                         ret = -EINVAL;
307                         release_firmware(ucode_fw);
308                         goto err_param_mem_free;
309                 }
310                 dev_vdbg(audio_avp->dev, "%s ucode size = %zd bytes",
311                         ucode_desc->bin_name, ucode_size);
312
313                 /* Read ucode */
314                 if (strncmp((const char *)ucode_fw->data, AVP_UCODE_HEADER,
315                         AVP_UCODE_HEADER_SIZE)) {
316                         dev_err(audio_avp->dev, "ucode Header mismatch");
317                         ret = -EINVAL;
318                         release_firmware(ucode_fw);
319                         goto err_param_mem_free;
320                 }
321
322                 memcpy((char *)audio_avp->ucode_mem.virt_addr + ucode_offset,
323                         ucode_fw->data + AVP_UCODE_HEADER_SIZE,
324                         ucode_size - AVP_UCODE_HEADER_SIZE);
325
326                 audio_engine->codec_ucode_desc[i][UCODE_DESC_OFFSET] =
327                                                                 ucode_offset;
328                 audio_engine->codec_ucode_desc[i][UCODE_DESC_SIZE] =
329                                                 ucode_desc->max_mem_size;
330                 ucode_offset += ucode_desc->max_mem_size;
331
332                 release_firmware(ucode_fw);
333         }
334
335         /* allocate memory for command buffer */
336         ret = tegra30_avp_mem_alloc(&audio_avp->cmd_buf_mem,
337                         AVP_CMD_BUFFER_SIZE);
338         if (ret < 0) {
339                 dev_err(audio_avp->dev, "Failed to allocate cmd buffer memory");
340                 goto err_param_mem_free;
341         }
342         audio_avp->cmd_buf = (unsigned int *)audio_avp->cmd_buf_mem.virt_addr;
343
344         /* Set command */
345         audio_avp->cmd_buf_idx = 0;
346         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
347                 NVE26E_CH_OPCODE_INCR(NVE276_SET_MICROCODE_A, 3);
348         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] = 0;
349         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
350                 audio_avp->ucode_mem.phys_addr;
351         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
352                 avp_ucode_desc[CODEC_PCM].max_mem_size - 8;
353         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
354                 NVE26E_CH_OPCODE_INCR(NVE276_PARAMETER_METHOD(0), 1);
355         audio_avp->cmd_buf[audio_avp->cmd_buf_idx++] =
356                 audio_avp->param_mem.phys_addr;
357
358         ret = nvavp_pushbuffer_submit_audio(audio_avp->nvavp_client,
359                                             audio_avp->cmd_buf_mem.phys_addr,
360                                             audio_avp->cmd_buf_idx);
361         if (ret < 0) {
362                 dev_err(audio_avp->dev, "pushbuffer_submit failed %d", ret);
363                 ret = -EINVAL;
364                 goto err_cmd_buf_mem_free;
365         }
366         dev_vdbg(audio_avp->dev, "Successfully loaded audio ucode");
367         return 0;
368
369 err_cmd_buf_mem_free:
370         tegra30_avp_mem_free(&audio_avp->cmd_buf_mem);
371 err_param_mem_free:
372         audio_avp->audio_engine = NULL;
373         tegra30_avp_mem_free(&audio_avp->param_mem);
374 err_ucode_mem_free:
375         tegra30_avp_mem_free(&audio_avp->ucode_mem);
376         return ret;
377 }
378
379 static void tegra30_avp_audio_engine_init(void)
380 {
381         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
382         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
383         int i = 0;
384
385         /* Initialize audio_engine_data */
386 #if defined(CONFIG_ARCH_TEGRA_14x_SOC) || defined(CONFIG_ARCH_TEGRA_12x_SOC)
387         audio_engine->chip_id = NV_TEGRA_T148;
388 #else
389         audio_engine->chip_id = NV_TEGRA_T114;
390 #endif
391         audio_engine->device_state_target = KSSTATE_STOP;
392         audio_engine->device_state_current = KSSTATE_STOP;
393
394 #ifdef DEBUG_AVP
395         audio_engine->profile_state = PROFILE_RENDER_OVERALL_PROCESSING;
396 #endif
397         audio_engine->device_format.rate = AVP_INIT_SAMPLE_RATE;
398         audio_engine->device_format.bits_per_sample = 16;
399         audio_engine->device_format.channels = 2;
400
401         /* Initialize stream memory */
402         for (i = 0; i < max_stream_id; i++) {
403                 struct tegra30_avp_stream *avp_stream;
404                 struct stream_data *stream;
405                 int j = 0;
406
407                 avp_stream = &audio_avp->avp_stream[i];
408                 memset(avp_stream, 0, sizeof(struct tegra30_avp_stream));
409
410                 avp_stream->id = i;
411
412                 stream = &audio_engine->stream[avp_stream->id];
413                 avp_stream->stream = stream;
414                 spin_lock_init(&avp_stream->lock);
415
416                 stream->stream_state_target = KSSTATE_STOP;
417                 stream->source_buffer_write_position = 0;
418                 stream->source_buffer_write_count = 0;
419                 stream->stream_params.rate = AVP_INIT_SAMPLE_RATE;
420
421                 for (j = 0; j < RENDERSW_MAX_CHANNELS; j++)
422                         stream->stream_volume[j] = AVP_UNITY_STREAM_VOLUME;
423
424                 stream->source_buffer_read_position = 0;
425                 stream->source_buffer_presentation_position = 0;
426                 stream->source_buffer_linear_position = 0;
427                 stream->source_buffer_presentation_position = 0;
428                 stream->source_buffer_frames_decoded = 0;
429                 stream->stream_state_current = KSSTATE_STOP;
430
431                 stream->stream_params.rate = AVP_INIT_SAMPLE_RATE;
432                 stream->stream_params.bits_per_sample = 16;
433                 stream->stream_params.channels = 2;
434
435                 avp_stream->audio_avp = audio_avp;
436         }
437 }
438
439 static int tegra30_avp_audio_alloc_dma(struct tegra_offload_dma_params *params)
440 {
441         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
442         struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;
443         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
444         dma_cap_mask_t mask;
445         int ret = 0;
446
447         dev_vdbg(audio_avp->dev, "%s: is_dma_allocated %d",
448                         __func__, atomic_read(&dma->is_dma_allocated));
449
450         if (atomic_read(&dma->is_dma_allocated) == 1)
451                 return 0;
452
453         memcpy(&dma->params, params, sizeof(struct tegra_offload_dma_params));
454
455         dma_cap_zero(mask);
456         dma_cap_set(DMA_SLAVE, mask);
457         dma_cap_set(DMA_CYCLIC, mask);
458         dma->chan = dma_request_channel(mask, NULL, NULL);
459         if (dma->chan == NULL) {
460                 dev_err(audio_avp->dev, "Failed to allocate DMA chan.");
461                 return -ENOMEM;
462         }
463
464         /* Only playback is supported */
465         dma->chan_slave_config.direction = DMA_MEM_TO_DEV;
466         dma->chan_slave_config.dst_addr_width = dma->params.width;
467         dma->chan_slave_config.dst_addr = dma->params.addr;
468         dma->chan_slave_config.dst_maxburst = dma->params.max_burst;
469         dma->chan_slave_config.slave_id = dma->params.req_sel;
470
471         ret = dmaengine_slave_config(dma->chan, &dma->chan_slave_config);
472         if (ret < 0) {
473                 dev_err(audio_avp->dev, "dma slave config failed.err %d.", ret);
474                 return ret;
475         }
476         audio_engine->apb_channel_handle = dma->chan->chan_id;
477         atomic_set(&dma->is_dma_allocated, 1);
478
479         return 0;
480 }
481
482 static void tegra30_avp_audio_free_dma(void)
483 {
484         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
485         struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;
486
487         dev_vdbg(audio_avp->dev, "%s: is_dma_allocated %d",
488                         __func__, atomic_read(&dma->is_dma_allocated));
489
490         if (atomic_read(&dma->is_dma_allocated) == 1) {
491                 dma_release_channel(dma->chan);
492                 atomic_set(&dma->is_dma_allocated, 0);
493         }
494
495         return;
496 }
497
498 static int tegra30_avp_audio_start_dma(void)
499 {
500         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
501         struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;
502         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
503
504         dev_vdbg(audio_avp->dev, "%s: active %d", __func__,
505                         atomic_read(&dma->active_count));
506
507         if (atomic_inc_return(&dma->active_count) > 1)
508                 return 0;
509
510         dma->chan_desc = dmaengine_prep_dma_cyclic(dma->chan,
511                                 (dma_addr_t)audio_engine->device_buffer_avp,
512                                 DEVICE_BUFFER_SIZE,
513                                 DEVICE_BUFFER_SIZE,
514                                 dma->chan_slave_config.direction,
515                                 DMA_CTRL_ACK);
516         if (!dma->chan_desc) {
517                 dev_err(audio_avp->dev, "Failed to prep cyclic dma");
518                 return -ENODEV;
519         }
520         dma->chan_cookie = dmaengine_submit(dma->chan_desc);
521         dma_async_issue_pending(dma->chan);
522         return 0;
523 }
524
525 static int tegra30_avp_audio_stop_dma(void)
526 {
527         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
528         struct tegra30_avp_audio_dma *dma = &audio_avp->audio_dma;
529
530         dev_vdbg(audio_avp->dev, "%s: active %d.", __func__,
531                         atomic_read(&dma->active_count));
532
533         if (atomic_dec_and_test(&dma->active_count))
534                 dmaengine_terminate_all(dma->chan);
535
536         return 0;
537 }
538
539 static int tegra30_avp_audio_execute(void)
540 {
541         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
542         uint32_t cmd_idx = audio_avp->cmd_buf_idx;
543         int ret = 0;
544
545         dev_vdbg(audio_avp->dev, "%s", __func__);
546
547         /* Set command */
548         audio_avp->cmd_buf[cmd_idx++] =
549                 NVE26E_CH_OPCODE_INCR(NVE276_EXECUTE, 1);
550         audio_avp->cmd_buf[cmd_idx++] = 0; /* NVE276_EXECUTE_APPID */
551
552         ret = nvavp_pushbuffer_submit_audio(audio_avp->nvavp_client,
553                 audio_avp->cmd_buf_mem.phys_addr,
554                 cmd_idx);
555         if (ret < 0) {
556                 dev_err(audio_avp->dev, "nvavp_pushbuffer_submit_audio failed");
557                 return -EINVAL;
558         }
559         return 0;
560 }
561
562 static int tegra30_avp_audio_set_state(enum KSSTATE new_state)
563 {
564         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
565         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
566         enum KSSTATE old_state;
567
568         old_state = audio_engine->device_state_target;
569
570         dev_vdbg(audio_avp->dev, "%s : state %d -> %d",
571                 __func__, old_state, new_state);
572
573         if (old_state == new_state)
574                 return 0;
575
576         audio_engine->device_state_target = new_state;
577         /* TODO : Need a way to wait till AVP device state changes */
578         if (new_state == KSSTATE_RUN)
579                 tegra30_avp_audio_execute();
580
581         return 0;
582 }
583
584
585 /* Call this function with stream lock held */
586 static int tegra30_avp_stream_set_state(int id, enum KSSTATE new_state)
587 {
588         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
589         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
590         struct stream_data *stream = avp_stream->stream;
591         enum KSSTATE old_state = stream->stream_state_target;
592         int ret = 0;
593
594         dev_vdbg(audio_avp->dev, "%s : id %d state %d -> %d", __func__, id,
595                  old_state, new_state);
596
597         if (old_state == new_state)
598                 return 0;
599
600         if (old_state == KSSTATE_STOP) {
601                 switch (stream->stream_format) {
602                 case FORMAT_MP3:
603                         nvavp_enable_audio_clocks(audio_avp->nvavp_client,
604                                 NVAVP_MODULE_ID_VCP);
605                         break;
606                 case FORMAT_AAC:
607                         nvavp_enable_audio_clocks(audio_avp->nvavp_client,
608                                 NVAVP_MODULE_ID_VCP);
609                         break;
610                 default:
611                         break;
612                 }
613         }
614
615         stream->stream_state_target = new_state;
616         /* TODO : Need a way to wait till AVP stream state changes */
617
618         if (new_state == KSSTATE_STOP) {
619                 switch (stream->stream_format) {
620                 case FORMAT_MP3:
621                         nvavp_disable_audio_clocks(audio_avp->nvavp_client,
622                                 NVAVP_MODULE_ID_VCP);
623                         break;
624                 case FORMAT_AAC:
625                         nvavp_disable_audio_clocks(audio_avp->nvavp_client,
626                                 NVAVP_MODULE_ID_VCP);
627                         break;
628                 default:
629                         break;
630                 }
631         }
632
633         if (new_state == KSSTATE_STOP) {
634                 stream->source_buffer_write_position = 0;
635                 stream->source_buffer_write_count = 0;
636                 avp_stream->last_notification_offset = 0;
637                 avp_stream->notification_received = 0;
638                 avp_stream->source_buffer_offset = 0;
639         }
640
641         if (new_state == KSSTATE_RUN)
642                 tegra30_avp_audio_start_dma();
643         else if (old_state == KSSTATE_RUN)
644                 tegra30_avp_audio_stop_dma();
645
646         return ret;
647 }
648
649 /* TODO : review ISR to make it more optimized if possible */
650 static void tegra30_avp_stream_notify(void)
651 {
652         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
653         struct tegra30_avp_stream *avp_stream;
654         struct stream_data *stream;
655         int i;
656
657         /* dev_vdbg(audio_avp->dev, "tegra30_avp_stream_notify"); */
658
659         for (i = 0; i < max_stream_id; i++) {
660                 avp_stream = &audio_avp->avp_stream[i];
661                 stream = avp_stream->stream;
662
663                 if (!stream->stream_allocated)
664                         continue;
665
666                 if (avp_stream->is_drain_called &&
667                    (stream->source_buffer_read_position ==
668                         stream->source_buffer_write_position) &&
669                    (avp_stream->notification_received >=
670                         stream->stream_notification_request)) {
671                         /* End of stream occured and noitfy same with value 1 */
672                         avp_stream->notify_cb(avp_stream->notify_args, 1);
673                         tegra30_avp_stream_set_state(i, KSSTATE_STOP);
674                 } else if (stream->stream_notification_request >
675                         avp_stream->notification_received) {
676                         avp_stream->notification_received++;
677
678                         avp_stream->notify_cb(avp_stream->notify_args, 0);
679                 }
680         }
681 }
682
683 /******************************************************************************
684  * EXPORTED FUNCTIONS
685  *****************************************************************************/
686 /* Device APIs */
687 static int tegra30_avp_set_hw_rate(int rate)
688 {
689         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
690         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
691
692         dev_vdbg(audio_avp->dev, "%s rate %d", __func__, rate);
693
694         if (!audio_engine) {
695                 dev_err(audio_avp->dev, "AVP platform not initialized.");
696                 return -ENODEV;
697         }
698
699         audio_engine->device_format.rate = rate;
700         return 0;
701 }
702
703 static int tegra30_avp_alloc_shared_mem(struct tegra_offload_mem *mem,
704                                         int bytes)
705 {
706         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
707         int ret = 0;
708
709         ret = tegra30_avp_mem_alloc(mem, bytes);
710         if (ret < 0) {
711                 dev_err(audio_avp->dev, "%s: Failed. ret %d", __func__, ret);
712                 return ret;
713         }
714         return 0;
715 }
716
717 static void tegra30_avp_free_shared_mem(struct tegra_offload_mem *mem)
718 {
719         tegra30_avp_mem_free(mem);
720 }
721
722 /* Loopback APIs */
723 static int tegra30_avp_loopback_set_params(int id,
724                 struct tegra_offload_pcm_params *params)
725 {
726         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
727         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
728         struct stream_data *stream = avp_stream->stream;
729         int ret = 0;
730
731         dev_vdbg(audio_avp->dev, "%s:entry\n", __func__);
732
733         /* TODO : check validity of parameters */
734         if (!stream) {
735                 dev_err(audio_avp->dev, "AVP platform not initialized.");
736                 return -ENODEV;
737         }
738
739
740         stream->stream_notification_interval = params->period_size;
741         stream->stream_notification_enable = 1;
742         stream->stream_params.rate = params->rate;
743         stream->stream_params.channels = params->channels;
744         stream->stream_params.bits_per_sample = params->bits_per_sample;
745
746
747         avp_stream->period_size = params->period_size;
748         avp_stream->notify_cb = params->period_elapsed_cb;
749         avp_stream->notify_args = params->period_elapsed_args;
750
751         stream->source_buffer_system =
752                 (uintptr_t)(params->source_buf.virt_addr);
753         stream->source_buffer_avp = params->source_buf.phys_addr;
754         stream->source_buffer_size = params->buffer_size;
755         return ret;
756 }
757
758 static int tegra30_avp_loopback_set_state(int id, int state)
759 {
760         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
761         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
762         struct stream_data *stream = avp_stream->stream;
763
764         dev_vdbg(audio_avp->dev, "%s : id %d state %d", __func__, id, state);
765
766         if (!stream) {
767                 dev_err(audio_avp->dev, "AVP platform not initialized.");
768                 return -ENODEV;
769         }
770
771         switch (state) {
772         case SNDRV_PCM_TRIGGER_START:
773                 stream->stream_state_target = KSSTATE_RUN;
774                 return 0;
775         case SNDRV_PCM_TRIGGER_STOP:
776                 stream->stream_state_target = KSSTATE_STOP;
777                 stream->source_buffer_write_position = 0;
778                 stream->source_buffer_write_count = 0;
779                 avp_stream->last_notification_offset = 0;
780                 avp_stream->notification_received = 0;
781                 avp_stream->source_buffer_offset = 0;
782                 return 0;
783         default:
784                 dev_err(audio_avp->dev, "Unsupported state.");
785                 return -EINVAL;
786         }
787 }
788
789 static size_t tegra30_avp_loopback_get_position(int id)
790 {
791         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
792         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
793         struct stream_data *stream = avp_stream->stream;
794         size_t pos = 0;
795
796         pos = (size_t)stream->source_buffer_read_position;
797
798         dev_vdbg(audio_avp->dev, "%s id %d pos %d", __func__, id, (u32)pos);
799
800         return pos;
801 }
802
803 static void tegra30_avp_loopback_data_ready(int id, int bytes)
804 {
805         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
806         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
807         struct stream_data *stream = avp_stream->stream;
808
809         dev_vdbg(audio_avp->dev, "%s :id %d size %d", __func__, id, bytes);
810
811         stream->source_buffer_write_position += bytes;
812         stream->source_buffer_write_position %= stream->source_buffer_size;
813
814         avp_stream->source_buffer_offset += bytes;
815         while (avp_stream->source_buffer_offset >=
816                 stream->stream_notification_interval) {
817                 stream->source_buffer_write_count++;
818                 avp_stream->source_buffer_offset -=
819                 stream->stream_notification_interval;
820         }
821         return;
822 }
823
824 /* PCM APIs */
825 static int tegra30_avp_pcm_open(int *id, char *stream)
826 {
827         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
828         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
829         struct tegra30_avp_stream *avp_stream;
830         int ret = 0;
831
832         dev_vdbg(audio_avp->dev, "%s", __func__);
833
834         if (!audio_avp->nvavp_client) {
835                 ret = tegra_nvavp_audio_client_open(&audio_avp->nvavp_client);
836                 if (ret < 0) {
837                         dev_err(audio_avp->dev, "Failed to open nvavp.");
838                         return ret;
839                 }
840         }
841         if (!audio_engine) {
842                 ret = tegra30_avp_load_ucode();
843                 if (ret < 0) {
844                         dev_err(audio_avp->dev, "Failed to load ucode.");
845                         return ret;
846                 }
847                 tegra30_avp_audio_engine_init();
848                 nvavp_register_audio_cb(audio_avp->nvavp_client,
849                         tegra30_avp_stream_notify);
850                 audio_engine = audio_avp->audio_engine;
851         }
852
853         if (strcmp(stream, "pcm") == 0) {
854                 avp_stream = &audio_avp->avp_stream[1];
855                 if (!audio_engine->stream[pcm_stream_id].stream_allocated) {
856                         *id = pcm_stream_id;
857                         audio_engine->stream[*id].stream_allocated = 1;
858                         atomic_inc(&audio_avp->stream_active_count);
859                 } else if (
860                 !audio_engine->stream[pcm2_stream_id].stream_allocated) {
861                         *id = pcm2_stream_id;
862                         audio_engine->stream[*id].stream_allocated = 1;
863                         atomic_inc(&audio_avp->stream_active_count);
864                 } else {
865                         dev_err(audio_avp->dev, "All AVP PCM streams are busy");
866                         return -EBUSY;
867                 }
868         } else if (strcmp(stream, "loopback") == 0) {
869                 avp_stream = &audio_avp->avp_stream[0];
870                 if
871                 (!audio_engine->stream[loopback_stream_id].stream_allocated) {
872                         dev_vdbg(audio_avp->dev,
873                         "Assigning loopback id:%d\n", loopback_stream_id);
874                         audio_engine->stream[*id].stream_allocated = 1;
875                         *id = loopback_stream_id;
876                 } else {
877                         dev_err(audio_avp->dev, "AVP loopback streams is busy");
878                         return -EBUSY;
879                 }
880         }
881
882         tegra30_avp_audio_set_state(KSSTATE_RUN);
883         return 0;
884 }
885
886 static int tegra30_avp_pcm_set_params(int id,
887                 struct tegra_offload_pcm_params *params)
888 {
889         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
890         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
891         struct stream_data *stream = avp_stream->stream;
892         int ret = 0;
893
894         dev_vdbg(audio_avp->dev,
895                 "%s id %d rate %d chan %d bps %d period size %d buf size %d",
896                  __func__, id, params->rate, params->channels,
897                  params->bits_per_sample, params->period_size,
898                  params->buffer_size);
899
900         /* TODO : check validity of parameters */
901         if (!stream) {
902                 dev_err(audio_avp->dev, "AVP platform not initialized.");
903                 return -ENODEV;
904         }
905
906         stream->stream_notification_interval = params->period_size;
907         stream->stream_notification_enable = 1;
908         stream->stream_params.rate = params->rate;
909         stream->stream_params.channels = params->channels;
910         stream->stream_params.bits_per_sample = params->bits_per_sample;
911         avp_stream->period_size = params->period_size;
912
913         avp_stream->notify_cb = params->period_elapsed_cb;
914         avp_stream->notify_args = params->period_elapsed_args;
915
916         stream->source_buffer_system =
917                         (uintptr_t) (params->source_buf.virt_addr);
918         stream->source_buffer_avp = params->source_buf.phys_addr;
919         stream->source_buffer_size = params->buffer_size;
920
921         /* Set DMA params */
922         ret = tegra30_avp_audio_alloc_dma(&params->dma_params);
923         if (ret < 0) {
924                 dev_err(audio_avp->dev, "Failed to allocate DMA. ret %d", ret);
925                 return ret;
926         }
927         return 0;
928 }
929
930 static int tegra30_avp_pcm_set_state(int id, int state)
931 {
932         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
933         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
934         struct stream_data *stream = avp_stream->stream;
935
936         dev_vdbg(audio_avp->dev, "%s : id %d state %d", __func__, id, state);
937
938         if (!stream) {
939                 dev_err(audio_avp->dev, "AVP platform not initialized.");
940                 return -ENODEV;
941         }
942
943         switch (state) {
944         case SNDRV_PCM_TRIGGER_START:
945         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
946         case SNDRV_PCM_TRIGGER_RESUME:
947                 tegra30_avp_stream_set_state(id, KSSTATE_RUN);
948                 return 0;
949         case SNDRV_PCM_TRIGGER_STOP:
950         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
951         case SNDRV_PCM_TRIGGER_SUSPEND:
952                 tegra30_avp_stream_set_state(id, KSSTATE_STOP);
953                 return 0;
954         default:
955                 dev_err(audio_avp->dev, "Unsupported state.");
956                 return -EINVAL;
957         }
958 }
959
960 static void tegra30_avp_pcm_data_ready(int id, int bytes)
961 {
962         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
963         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
964         struct stream_data *stream = avp_stream->stream;
965
966         dev_vdbg(audio_avp->dev, "%s :id %d size %d", __func__, id, bytes);
967
968         stream->source_buffer_write_position += bytes;
969         stream->source_buffer_write_position %= stream->source_buffer_size;
970
971         avp_stream->source_buffer_offset += bytes;
972         while (avp_stream->source_buffer_offset >=
973                 stream->stream_notification_interval) {
974                 stream->source_buffer_write_count++;
975                 avp_stream->source_buffer_offset -=
976                         stream->stream_notification_interval;
977         }
978         return;
979 }
980
981 static size_t tegra30_avp_pcm_get_position(int id)
982 {
983         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
984         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
985         struct stream_data *stream = avp_stream->stream;
986         size_t pos = 0;
987
988         pos = (size_t)stream->source_buffer_read_position;
989
990         dev_vdbg(audio_avp->dev, "%s id %d pos %d", __func__, id, (u32)pos);
991
992         return pos;
993 }
994
995 /* Compress APIs */
996 static int tegra30_avp_compr_open(int *id)
997 {
998         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
999         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
1000         int ret = 0;
1001
1002         dev_vdbg(audio_avp->dev, "%s", __func__);
1003
1004         if (!audio_avp->nvavp_client) {
1005                 ret = tegra_nvavp_audio_client_open(&audio_avp->nvavp_client);
1006                 if (ret < 0) {
1007                         dev_err(audio_avp->dev, "Failed to open nvavp.");
1008                         return ret;
1009                 }
1010         }
1011         if (!audio_engine) {
1012                 ret = tegra30_avp_load_ucode();
1013                 if (ret < 0) {
1014                         dev_err(audio_avp->dev, "Failed to load ucode.");
1015                         return ret;
1016                 }
1017                 tegra30_avp_audio_engine_init();
1018                 nvavp_register_audio_cb(audio_avp->nvavp_client,
1019                         tegra30_avp_stream_notify);
1020                 audio_engine = audio_avp->audio_engine;
1021         }
1022
1023         if (!audio_engine->stream[decode_stream_id].stream_allocated)
1024                 *id = decode_stream_id;
1025         else if (!audio_engine->stream[decode2_stream_id].stream_allocated)
1026                 *id = decode2_stream_id;
1027         else {
1028                 dev_err(audio_avp->dev, "All AVP COMPR streams are busy");
1029                 return -EBUSY;
1030         }
1031         audio_avp->avp_stream[*id].is_drain_called = 0;
1032         audio_engine->stream[*id].stream_allocated = 1;
1033
1034         atomic_inc(&audio_avp->stream_active_count);
1035         tegra30_avp_audio_set_state(KSSTATE_RUN);
1036
1037         return 0;
1038 }
1039
1040 static int tegra30_avp_compr_set_params(int id,
1041                 struct tegra_offload_compr_params *params)
1042 {
1043         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1044         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1045         struct stream_data *stream = avp_stream->stream;
1046         int ret = 0;
1047
1048         dev_vdbg(audio_avp->dev,
1049                 "%s: id %d codec %d rate %d ch %d bps %d buf %d x %d",
1050                  __func__, id, params->codec_type, params->rate,
1051                  params->channels, params->bits_per_sample,
1052                  params->fragment_size, params->fragments);
1053
1054         /* TODO : check validity of parameters */
1055         if (!stream) {
1056                 dev_err(audio_avp->dev, "AVP platform not initialized.");
1057                 return -ENODEV;
1058         }
1059
1060         if (params->codec_type == SND_AUDIOCODEC_MP3) {
1061                 stream->stream_format = FORMAT_MP3;
1062         } else if (params->codec_type == SND_AUDIOCODEC_AAC) {
1063                 stream->stream_format = FORMAT_AAC;
1064
1065                 /* AAC-LC is only supported profile*/
1066                 stream->u.aac.audio_profile = AAC_PROFILE_LC;
1067                 stream->u.aac.sampling_freq = params->rate;
1068                 stream->u.aac.payload_type  = AAC_PAYLOAD_RAW;
1069                 switch (params->rate) {
1070                 case 8000:
1071                         stream->u.aac.sampling_freq_index = 0xb;
1072                         break;
1073                 case 11025:
1074                         stream->u.aac.sampling_freq_index = 0xa;
1075                         break;
1076                 case 12000:
1077                         stream->u.aac.sampling_freq_index = 0x9;
1078                         break;
1079                 case 16000:
1080                         stream->u.aac.sampling_freq_index = 0x8;
1081                         break;
1082                 case 22050:
1083                         stream->u.aac.sampling_freq_index = 0x7;
1084                         break;
1085                 case 24000:
1086                         stream->u.aac.sampling_freq_index = 0x6;
1087                         break;
1088                 case 32000:
1089                         stream->u.aac.sampling_freq_index = 0x5;
1090                         break;
1091                 case 44100:
1092                         stream->u.aac.sampling_freq_index = 0x4;
1093                         break;
1094                 case 48000:
1095                         stream->u.aac.sampling_freq_index = 0x3;
1096                         break;
1097                 case 64000:
1098                         stream->u.aac.sampling_freq_index = 0x2;
1099                         break;
1100                 case 88200:
1101                         stream->u.aac.sampling_freq_index = 0x1;
1102                         break;
1103                 case 96000:
1104                         stream->u.aac.sampling_freq_index = 0x0;
1105                         break;
1106                 default:
1107                         dev_err(audio_avp->dev, "Unsupported rate");
1108                         return -EINVAL;
1109                 }
1110                 /* Only Stereo data is supported */
1111                 stream->u.aac.channel_configuration = params->channels;
1112         } else {
1113                 dev_err(audio_avp->dev, "Invalid stream/codec type.");
1114                 return -EINVAL;
1115         }
1116
1117         stream->stream_params.rate = params->rate;
1118         stream->stream_params.channels = params->channels;
1119         stream->stream_params.bits_per_sample = params->bits_per_sample;
1120         avp_stream->period_size = params->fragment_size;
1121
1122         avp_stream->notify_cb = params->fragments_elapsed_cb;
1123         avp_stream->notify_args = params->fragments_elapsed_args;
1124
1125         stream->source_buffer_size = (params->fragments *
1126                         params->fragment_size);
1127         ret = tegra30_avp_mem_alloc(&avp_stream->source_buf,
1128                               stream->source_buffer_size);
1129         if (ret < 0) {
1130                 dev_err(audio_avp->dev, "Failed to allocate source buf memory");
1131                 return ret;
1132         }
1133
1134         stream->source_buffer_system =
1135                         (uintptr_t) avp_stream->source_buf.virt_addr;
1136         stream->source_buffer_avp = avp_stream->source_buf.phys_addr;
1137
1138         if (stream->source_buffer_size > AVP_COMPR_THRESHOLD) {
1139                 stream->stream_notification_interval =
1140                         stream->source_buffer_size - AVP_COMPR_THRESHOLD;
1141         } else {
1142                 stream->stream_notification_interval = avp_stream->period_size;
1143         }
1144         stream->stream_notification_enable = 1;
1145
1146         /* Set DMA params */
1147         ret = tegra30_avp_audio_alloc_dma(&params->dma_params);
1148         if (ret < 0) {
1149                 dev_err(audio_avp->dev, "Failed to allocate DMA. ret %d", ret);
1150                 return ret;
1151         }
1152
1153         if ((params->codec_type == SND_AUDIOCODEC_MP3) ||
1154             (params->codec_type == SND_AUDIOCODEC_AAC)) {
1155                 dev_info(audio_avp->dev, "\n*** STARTING %s Offload PLAYBACK ***\n",
1156                    (params->codec_type == SND_AUDIOCODEC_MP3) ? "MP3" : "AAC");
1157         }
1158         return 0;
1159 }
1160
1161 static int tegra30_avp_compr_set_state(int id, int state)
1162 {
1163         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1164         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1165
1166         dev_vdbg(audio_avp->dev, "%s : id %d state %d",
1167                  __func__, id, state);
1168
1169         switch (state) {
1170         case SNDRV_PCM_TRIGGER_START:
1171         case SNDRV_PCM_TRIGGER_RESUME:
1172                 tegra30_avp_stream_set_state(id, KSSTATE_RUN);
1173                 return 0;
1174         case SNDRV_PCM_TRIGGER_STOP:
1175         case SNDRV_PCM_TRIGGER_SUSPEND:
1176                 tegra30_avp_stream_set_state(id, KSSTATE_STOP);
1177                 return 0;
1178         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1179                 tegra30_avp_stream_set_state(id, KSSTATE_PAUSE);
1180                 return 0;
1181         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1182                 tegra30_avp_stream_set_state(id, KSSTATE_RUN);
1183                 return 0;
1184         case SND_COMPR_TRIGGER_DRAIN:
1185         case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
1186                 avp_stream->is_drain_called = 1;
1187                 return 0;
1188         default:
1189                 dev_err(audio_avp->dev, "Unsupported state.");
1190                 return -EINVAL;
1191         }
1192 }
1193
1194 static void tegra30_avp_compr_data_ready(int id, int bytes)
1195 {
1196         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1197         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1198         struct stream_data *stream = avp_stream->stream;
1199
1200         dev_vdbg(audio_avp->dev, "%s : id %d size %d", __func__, id, bytes);
1201
1202         stream->source_buffer_write_position += bytes;
1203         stream->source_buffer_write_position %= stream->source_buffer_size;
1204
1205         avp_stream->source_buffer_offset += bytes;
1206         while (avp_stream->source_buffer_offset >=
1207                 stream->stream_notification_interval) {
1208                 stream->source_buffer_write_count++;
1209                 avp_stream->source_buffer_offset -=
1210                         stream->stream_notification_interval;
1211         }
1212         return;
1213 }
1214
1215 static int tegra30_avp_compr_write(int id, char __user *buf, int bytes)
1216 {
1217         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1218         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1219         struct stream_data *stream = avp_stream->stream;
1220         void *dst = (char *)(uintptr_t)stream->source_buffer_system +
1221                 stream->source_buffer_write_position;
1222         int avail = 0;
1223         int write = 0;
1224         int ret = 0;
1225
1226         avail = stream->source_buffer_read_position -
1227                 stream->source_buffer_write_position;
1228         if ((avail < 0) || (!avail &&
1229                 (stream->source_buffer_write_count ==
1230                 stream->stream_notification_request)))
1231                 avail += stream->source_buffer_size;
1232
1233         dev_vdbg(audio_avp->dev, "%s : id %d size %d", __func__, id, bytes);
1234
1235         /* TODO: check enough free space is available before writing */
1236         bytes = (bytes > avail) ? avail : bytes;
1237         if (!bytes) {
1238                 dev_dbg(audio_avp->dev, "No free space in ring buffer.");
1239                 DUMP_AVP_STATUS(avp_stream);
1240                 return bytes;
1241         }
1242
1243         write = stream->source_buffer_size -
1244                 stream->source_buffer_write_position;
1245         if (write > bytes) {
1246                 ret = copy_from_user(dst, buf, bytes);
1247                 if (ret < 0) {
1248                         dev_err(audio_avp->dev, "Failed to copy user data.");
1249                         return -EFAULT;
1250                 }
1251         } else {
1252                 ret = copy_from_user(dst, buf, write);
1253                 if (ret < 0) {
1254                         dev_err(audio_avp->dev, "Failed to copy user data.");
1255                         return -EFAULT;
1256                 }
1257
1258                 ret = copy_from_user((void *)(uintptr_t)stream->source_buffer_system,
1259                                 buf + write, bytes - write);
1260                 if (ret < 0) {
1261                         dev_err(audio_avp->dev, "Failed to copy user data.");
1262                         return -EFAULT;
1263                 }
1264         }
1265
1266         stream->source_buffer_write_position += bytes;
1267         stream->source_buffer_write_position %= stream->source_buffer_size;
1268
1269         avp_stream->source_buffer_offset += bytes;
1270         while (avp_stream->source_buffer_offset >=
1271                 stream->stream_notification_interval) {
1272                 stream->source_buffer_write_count++;
1273                 avp_stream->source_buffer_offset -=
1274                         stream->stream_notification_interval;
1275         }
1276         DUMP_AVP_STATUS(avp_stream);
1277         return bytes;
1278 }
1279
1280 static int tegra30_avp_compr_get_position(int id,
1281                         struct snd_compr_tstamp *tstamp)
1282 {
1283         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1284         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1285         struct stream_data *stream = avp_stream->stream;
1286
1287         tstamp->byte_offset = stream->source_buffer_write_position;
1288         tstamp->copied_total = stream->source_buffer_write_position +
1289                 (stream->source_buffer_write_count *
1290                  stream->stream_notification_interval);
1291         tstamp->pcm_frames = stream->source_buffer_presentation_position;
1292         tstamp->pcm_io_frames = stream->source_buffer_presentation_position;
1293         tstamp->sampling_rate = stream->stream_params.rate;
1294
1295         dev_vdbg(audio_avp->dev, "%s id %d off %d copied %d pcm %d pcm io %d",
1296                  __func__, id, (int)tstamp->byte_offset,
1297                  (int)tstamp->copied_total, (int)tstamp->pcm_frames,
1298                  (int)tstamp->pcm_io_frames);
1299
1300         return 0;
1301 }
1302
1303 static int tegra30_avp_compr_get_caps(struct snd_compr_caps *caps)
1304 {
1305         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1306
1307         dev_vdbg(audio_avp->dev, "%s : dir %d", __func__, caps->direction);
1308
1309         if (caps->direction == SND_COMPRESS_PLAYBACK)
1310                 memcpy(caps, &tegra30_avp_compr_caps[SND_COMPRESS_PLAYBACK],
1311                         sizeof(struct snd_compr_caps));
1312         else
1313                 memcpy(caps, &tegra30_avp_compr_caps[SND_COMPRESS_CAPTURE],
1314                         sizeof(struct snd_compr_caps));
1315         return 0;
1316 }
1317
1318 static int tegra30_avp_compr_get_codec_caps(struct snd_compr_codec_caps *codec)
1319 {
1320         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1321
1322         dev_vdbg(audio_avp->dev, "%s : codec %d", __func__, codec->codec);
1323
1324         switch (codec->codec) {
1325         case SND_AUDIOCODEC_MP3:
1326                 memcpy(codec, &tegra30_avp_compr_codec_caps[avp_compr_mp3],
1327                         sizeof(struct snd_compr_codec_caps));
1328                 return 0;
1329         case SND_AUDIOCODEC_AAC:
1330                 memcpy(codec, &tegra30_avp_compr_codec_caps[avp_compr_aac],
1331                         sizeof(struct snd_compr_codec_caps));
1332                 return 0;
1333         default:
1334                 dev_err(audio_avp->dev, "Unsupported codec %d", codec->codec);
1335                 return -EINVAL;
1336         }
1337 }
1338
1339 static int tegra30_avp_compr_set_volume(int id, int left, int right)
1340 {
1341         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1342         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1343         struct stream_data *stream = avp_stream->stream;
1344
1345         dev_vdbg(audio_avp->dev, "%s id %d left vol %d right vol %d",
1346                  __func__, id, left, right);
1347
1348         if (!stream) {
1349                 dev_err(audio_avp->dev, "AVP platform not initialized.");
1350                 return -ENODEV;
1351         }
1352
1353         stream->stream_volume[0] = left;
1354         stream->stream_volume[1] = right;
1355
1356         return 0;
1357 }
1358
1359 /* Common APIs for pcm and compress */
1360 static void tegra30_avp_stream_close(int id)
1361 {
1362         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1363         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1364         struct stream_data *stream = avp_stream->stream;
1365
1366         dev_vdbg(audio_avp->dev, "%s id %d", __func__, id);
1367
1368         if (!stream) {
1369                 dev_err(audio_avp->dev, "AVP platform not initialized.");
1370                 return;
1371         }
1372         tegra30_avp_mem_free(&avp_stream->source_buf);
1373         stream->stream_allocated = 0;
1374         tegra30_avp_stream_set_state(id, KSSTATE_STOP);
1375
1376         if (id == loopback_stream_id)
1377                 return;
1378
1379         if (atomic_dec_and_test(&audio_avp->stream_active_count)) {
1380                 tegra30_avp_audio_free_dma();
1381                 tegra30_avp_audio_set_state(KSSTATE_STOP);
1382         }
1383 }
1384
1385 static struct tegra_offload_ops avp_audio_platform = {
1386         .device_ops = {
1387                 .set_hw_rate = tegra30_avp_set_hw_rate,
1388                 .alloc_shared_mem = tegra30_avp_alloc_shared_mem,
1389                 .free_shared_mem = tegra30_avp_free_shared_mem,
1390         },
1391         .pcm_ops = {
1392                 .stream_open = tegra30_avp_pcm_open,
1393                 .stream_close = tegra30_avp_stream_close,
1394                 .set_stream_params = tegra30_avp_pcm_set_params,
1395                 .set_stream_state = tegra30_avp_pcm_set_state,
1396                 .get_stream_position = tegra30_avp_pcm_get_position,
1397                 .data_ready = tegra30_avp_pcm_data_ready,
1398         },
1399         .loopback_ops = {
1400                 .stream_open = tegra30_avp_pcm_open,
1401                 .stream_close = tegra30_avp_stream_close,
1402                 .set_stream_params = tegra30_avp_loopback_set_params,
1403                 .set_stream_state = tegra30_avp_loopback_set_state,
1404                 .get_stream_position = tegra30_avp_loopback_get_position,
1405                 .data_ready = tegra30_avp_loopback_data_ready,
1406         },
1407         .compr_ops = {
1408                 .stream_open = tegra30_avp_compr_open,
1409                 .stream_close = tegra30_avp_stream_close,
1410                 .set_stream_params = tegra30_avp_compr_set_params,
1411                 .set_stream_state = tegra30_avp_compr_set_state,
1412                 .get_stream_position = tegra30_avp_compr_get_position,
1413                 .data_ready = tegra30_avp_compr_data_ready,
1414                 .write = tegra30_avp_compr_write,
1415                 .get_caps = tegra30_avp_compr_get_caps,
1416                 .get_codec_caps = tegra30_avp_compr_get_codec_caps,
1417                 .set_stream_volume = tegra30_avp_compr_set_volume,
1418         },
1419 };
1420
1421 static u64 tegra_dma_mask = DMA_BIT_MASK(32);
1422 static int tegra30_avp_audio_probe(struct platform_device *pdev)
1423 {
1424         struct tegra30_avp_audio *audio_avp;
1425
1426         pr_debug("tegra30_avp_audio_platform_probe platform probe started\n");
1427
1428         audio_avp = devm_kzalloc(&pdev->dev, sizeof(*audio_avp), GFP_KERNEL);
1429         if (!audio_avp) {
1430                 dev_err(&pdev->dev, "Can't allocate tegra30_avp_audio\n");
1431                 return -ENOMEM;
1432         }
1433         dev_set_drvdata(&pdev->dev, audio_avp);
1434         audio_avp->dev = &pdev->dev;
1435
1436         /* set the ops */
1437         if (tegra_register_offload_ops(&avp_audio_platform)) {
1438                 dev_err(&pdev->dev, "Failed to register avp audio device.");
1439                 return -EPROBE_DEFER;
1440         }
1441
1442         spin_lock_init(&audio_avp->lock);
1443         pdev->dev.dma_mask = &tegra_dma_mask;
1444         pdev->dev.coherent_dma_mask = tegra_dma_mask;
1445         avp_audio_ctx = audio_avp;
1446         pr_info("tegra30_avp_audio_platform_probe successful.");
1447         return 0;
1448 }
1449
1450 static int tegra30_avp_audio_remove(struct platform_device *pdev)
1451 {
1452         struct tegra30_avp_audio *audio_avp = dev_get_drvdata(&pdev->dev);
1453
1454         dev_vdbg(&pdev->dev, "%s", __func__);
1455
1456         tegra_deregister_offload_ops();
1457         if (audio_avp->nvavp_client)
1458                 tegra_nvavp_audio_client_release(audio_avp->nvavp_client);
1459         tegra30_avp_mem_free(&audio_avp->cmd_buf_mem);
1460         tegra30_avp_mem_free(&audio_avp->param_mem);
1461         tegra30_avp_mem_free(&audio_avp->ucode_mem);
1462
1463         return 0;
1464 }
1465
1466 static const struct of_device_id tegra30_avp_audio_of_match[] = {
1467         { .compatible = "nvidia,tegra30-avp-audio", },
1468         {},
1469 };
1470
1471 static struct platform_driver tegra30_avp_audio_driver = {
1472         .driver = {
1473                 .name = DRV_NAME,
1474                 .owner = THIS_MODULE,
1475                 .of_match_table = tegra30_avp_audio_of_match,
1476         },
1477         .probe = tegra30_avp_audio_probe,
1478         .remove = tegra30_avp_audio_remove,
1479 };
1480 module_platform_driver(tegra30_avp_audio_driver);
1481
1482 MODULE_AUTHOR("Sumit Bhattacharya <sumitb@nvidia.com>");
1483 MODULE_DESCRIPTION("Tegra30 AVP Audio driver");
1484 MODULE_LICENSE("GPL");
1485 MODULE_ALIAS("platform:" DRV_NAME);
1486 MODULE_DEVICE_TABLE(of, tegra30_avp_audio_of_match);