ASoC: tegra: fix avp module remove
[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 /* Loopback APIs */
717 static int tegra30_avp_loopback_set_params(int id,
718                 struct tegra_offload_pcm_params *params)
719 {
720         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
721         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
722         struct stream_data *stream = avp_stream->stream;
723         int ret = 0;
724
725         dev_vdbg(audio_avp->dev, "%s:entry\n", __func__);
726
727         /* TODO : check validity of parameters */
728         if (!stream) {
729                 dev_err(audio_avp->dev, "AVP platform not initialized.");
730                 return -ENODEV;
731         }
732
733
734         stream->stream_notification_interval = params->period_size;
735         stream->stream_notification_enable = 1;
736         stream->stream_params.rate = params->rate;
737         stream->stream_params.channels = params->channels;
738         stream->stream_params.bits_per_sample = params->bits_per_sample;
739
740
741         avp_stream->period_size = params->period_size;
742         avp_stream->notify_cb = params->period_elapsed_cb;
743         avp_stream->notify_args = params->period_elapsed_args;
744
745         stream->source_buffer_system =
746                 (uintptr_t)(params->source_buf.virt_addr);
747         stream->source_buffer_avp = params->source_buf.phys_addr;
748         stream->source_buffer_size = params->buffer_size;
749         return ret;
750 }
751
752 static int tegra30_avp_loopback_set_state(int id, int state)
753 {
754         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
755         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
756         struct stream_data *stream = avp_stream->stream;
757
758         dev_vdbg(audio_avp->dev, "%s : id %d state %d", __func__, id, state);
759
760         if (!stream) {
761                 dev_err(audio_avp->dev, "AVP platform not initialized.");
762                 return -ENODEV;
763         }
764
765         switch (state) {
766         case SNDRV_PCM_TRIGGER_START:
767                 stream->stream_state_target = KSSTATE_RUN;
768                 return 0;
769         case SNDRV_PCM_TRIGGER_STOP:
770                 stream->stream_state_target = KSSTATE_STOP;
771                 stream->source_buffer_write_position = 0;
772                 stream->source_buffer_write_count = 0;
773                 avp_stream->last_notification_offset = 0;
774                 avp_stream->notification_received = 0;
775                 avp_stream->source_buffer_offset = 0;
776                 return 0;
777         default:
778                 dev_err(audio_avp->dev, "Unsupported state.");
779                 return -EINVAL;
780         }
781 }
782
783 static size_t tegra30_avp_loopback_get_position(int id)
784 {
785         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
786         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
787         struct stream_data *stream = avp_stream->stream;
788         size_t pos = 0;
789
790         pos = (size_t)stream->source_buffer_read_position;
791
792         dev_vdbg(audio_avp->dev, "%s id %d pos %d", __func__, id, (u32)pos);
793
794         return pos;
795 }
796
797 static void tegra30_avp_loopback_data_ready(int id, int bytes)
798 {
799         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
800         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
801         struct stream_data *stream = avp_stream->stream;
802
803         dev_vdbg(audio_avp->dev, "%s :id %d size %d", __func__, id, bytes);
804
805         stream->source_buffer_write_position += bytes;
806         stream->source_buffer_write_position %= stream->source_buffer_size;
807
808         avp_stream->source_buffer_offset += bytes;
809         while (avp_stream->source_buffer_offset >=
810                 stream->stream_notification_interval) {
811                 stream->source_buffer_write_count++;
812                 avp_stream->source_buffer_offset -=
813                 stream->stream_notification_interval;
814         }
815         return;
816 }
817
818 /* PCM APIs */
819 static int tegra30_avp_pcm_open(int *id, char *stream)
820 {
821         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
822         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
823         struct tegra30_avp_stream *avp_stream;
824         int ret = 0;
825
826         dev_vdbg(audio_avp->dev, "%s", __func__);
827
828         if (!audio_avp->nvavp_client) {
829                 ret = tegra_nvavp_audio_client_open(&audio_avp->nvavp_client);
830                 if (ret < 0) {
831                         dev_err(audio_avp->dev, "Failed to open nvavp.");
832                         return ret;
833                 }
834         }
835         if (!audio_engine) {
836                 ret = tegra30_avp_load_ucode();
837                 if (ret < 0) {
838                         dev_err(audio_avp->dev, "Failed to load ucode.");
839                         return ret;
840                 }
841                 tegra30_avp_audio_engine_init();
842                 nvavp_register_audio_cb(audio_avp->nvavp_client,
843                         tegra30_avp_stream_notify);
844                 audio_engine = audio_avp->audio_engine;
845         }
846
847         if (strcmp(stream, "pcm") == 0) {
848                 avp_stream = &audio_avp->avp_stream[1];
849                 if (!audio_engine->stream[pcm_stream_id].stream_allocated) {
850                         *id = pcm_stream_id;
851                         audio_engine->stream[*id].stream_allocated = 1;
852                         atomic_inc(&audio_avp->stream_active_count);
853                 } else if (
854                 !audio_engine->stream[pcm2_stream_id].stream_allocated) {
855                         *id = pcm2_stream_id;
856                         audio_engine->stream[*id].stream_allocated = 1;
857                         atomic_inc(&audio_avp->stream_active_count);
858                 } else {
859                         dev_err(audio_avp->dev, "All AVP PCM streams are busy");
860                         return -EBUSY;
861                 }
862         } else if (strcmp(stream, "loopback") == 0) {
863                 avp_stream = &audio_avp->avp_stream[0];
864                 if
865                 (!audio_engine->stream[loopback_stream_id].stream_allocated) {
866                         dev_vdbg(audio_avp->dev,
867                         "Assigning loopback id:%d\n", loopback_stream_id);
868                         audio_engine->stream[*id].stream_allocated = 1;
869                         *id = loopback_stream_id;
870                 } else {
871                         dev_err(audio_avp->dev, "AVP loopback streams is busy");
872                         return -EBUSY;
873                 }
874         }
875
876         tegra30_avp_audio_set_state(KSSTATE_RUN);
877         return 0;
878 }
879
880 static int tegra30_avp_pcm_set_params(int id,
881                 struct tegra_offload_pcm_params *params)
882 {
883         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
884         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
885         struct stream_data *stream = avp_stream->stream;
886         int ret = 0;
887
888         dev_vdbg(audio_avp->dev,
889                 "%s id %d rate %d chan %d bps %d period size %d buf size %d",
890                  __func__, id, params->rate, params->channels,
891                  params->bits_per_sample, params->period_size,
892                  params->buffer_size);
893
894         /* TODO : check validity of parameters */
895         if (!stream) {
896                 dev_err(audio_avp->dev, "AVP platform not initialized.");
897                 return -ENODEV;
898         }
899
900         stream->stream_notification_interval = params->period_size;
901         stream->stream_notification_enable = 1;
902         stream->stream_params.rate = params->rate;
903         stream->stream_params.channels = params->channels;
904         stream->stream_params.bits_per_sample = params->bits_per_sample;
905         avp_stream->period_size = params->period_size;
906
907         avp_stream->notify_cb = params->period_elapsed_cb;
908         avp_stream->notify_args = params->period_elapsed_args;
909
910         stream->source_buffer_system =
911                         (uintptr_t) (params->source_buf.virt_addr);
912         stream->source_buffer_avp = params->source_buf.phys_addr;
913         stream->source_buffer_size = params->buffer_size;
914
915         /* Set DMA params */
916         ret = tegra30_avp_audio_alloc_dma(&params->dma_params);
917         if (ret < 0) {
918                 dev_err(audio_avp->dev, "Failed to allocate DMA. ret %d", ret);
919                 return ret;
920         }
921         return 0;
922 }
923
924 static int tegra30_avp_pcm_set_state(int id, int state)
925 {
926         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
927         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
928         struct stream_data *stream = avp_stream->stream;
929
930         dev_vdbg(audio_avp->dev, "%s : id %d state %d", __func__, id, state);
931
932         if (!stream) {
933                 dev_err(audio_avp->dev, "AVP platform not initialized.");
934                 return -ENODEV;
935         }
936
937         switch (state) {
938         case SNDRV_PCM_TRIGGER_START:
939         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
940         case SNDRV_PCM_TRIGGER_RESUME:
941                 tegra30_avp_stream_set_state(id, KSSTATE_RUN);
942                 return 0;
943         case SNDRV_PCM_TRIGGER_STOP:
944         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
945         case SNDRV_PCM_TRIGGER_SUSPEND:
946                 tegra30_avp_stream_set_state(id, KSSTATE_STOP);
947                 return 0;
948         default:
949                 dev_err(audio_avp->dev, "Unsupported state.");
950                 return -EINVAL;
951         }
952 }
953
954 static void tegra30_avp_pcm_data_ready(int id, int bytes)
955 {
956         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
957         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
958         struct stream_data *stream = avp_stream->stream;
959
960         dev_vdbg(audio_avp->dev, "%s :id %d size %d", __func__, id, bytes);
961
962         stream->source_buffer_write_position += bytes;
963         stream->source_buffer_write_position %= stream->source_buffer_size;
964
965         avp_stream->source_buffer_offset += bytes;
966         while (avp_stream->source_buffer_offset >=
967                 stream->stream_notification_interval) {
968                 stream->source_buffer_write_count++;
969                 avp_stream->source_buffer_offset -=
970                         stream->stream_notification_interval;
971         }
972         return;
973 }
974
975 static size_t tegra30_avp_pcm_get_position(int id)
976 {
977         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
978         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
979         struct stream_data *stream = avp_stream->stream;
980         size_t pos = 0;
981
982         pos = (size_t)stream->source_buffer_read_position;
983
984         dev_vdbg(audio_avp->dev, "%s id %d pos %d", __func__, id, (u32)pos);
985
986         return pos;
987 }
988
989 /* Compress APIs */
990 static int tegra30_avp_compr_open(int *id)
991 {
992         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
993         struct audio_engine_data *audio_engine = audio_avp->audio_engine;
994         int ret = 0;
995
996         dev_vdbg(audio_avp->dev, "%s", __func__);
997
998         if (!audio_avp->nvavp_client) {
999                 ret = tegra_nvavp_audio_client_open(&audio_avp->nvavp_client);
1000                 if (ret < 0) {
1001                         dev_err(audio_avp->dev, "Failed to open nvavp.");
1002                         return ret;
1003                 }
1004         }
1005         if (!audio_engine) {
1006                 ret = tegra30_avp_load_ucode();
1007                 if (ret < 0) {
1008                         dev_err(audio_avp->dev, "Failed to load ucode.");
1009                         return ret;
1010                 }
1011                 tegra30_avp_audio_engine_init();
1012                 nvavp_register_audio_cb(audio_avp->nvavp_client,
1013                         tegra30_avp_stream_notify);
1014                 audio_engine = audio_avp->audio_engine;
1015         }
1016
1017         if (!audio_engine->stream[decode_stream_id].stream_allocated)
1018                 *id = decode_stream_id;
1019         else if (!audio_engine->stream[decode2_stream_id].stream_allocated)
1020                 *id = decode2_stream_id;
1021         else {
1022                 dev_err(audio_avp->dev, "All AVP COMPR streams are busy");
1023                 return -EBUSY;
1024         }
1025         audio_avp->avp_stream[*id].is_drain_called = 0;
1026         audio_engine->stream[*id].stream_allocated = 1;
1027
1028         atomic_inc(&audio_avp->stream_active_count);
1029         tegra30_avp_audio_set_state(KSSTATE_RUN);
1030
1031         return 0;
1032 }
1033
1034 static int tegra30_avp_compr_set_params(int id,
1035                 struct tegra_offload_compr_params *params)
1036 {
1037         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1038         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1039         struct stream_data *stream = avp_stream->stream;
1040         int ret = 0;
1041
1042         dev_vdbg(audio_avp->dev,
1043                 "%s: id %d codec %d rate %d ch %d bps %d buf %d x %d",
1044                  __func__, id, params->codec_type, params->rate,
1045                  params->channels, params->bits_per_sample,
1046                  params->fragment_size, params->fragments);
1047
1048         /* TODO : check validity of parameters */
1049         if (!stream) {
1050                 dev_err(audio_avp->dev, "AVP platform not initialized.");
1051                 return -ENODEV;
1052         }
1053
1054         if (params->codec_type == SND_AUDIOCODEC_MP3) {
1055                 stream->stream_format = FORMAT_MP3;
1056         } else if (params->codec_type == SND_AUDIOCODEC_AAC) {
1057                 stream->stream_format = FORMAT_AAC;
1058
1059                 /* AAC-LC is only supported profile*/
1060                 stream->u.aac.audio_profile = AAC_PROFILE_LC;
1061                 stream->u.aac.sampling_freq = params->rate;
1062                 stream->u.aac.payload_type  = AAC_PAYLOAD_RAW;
1063                 switch (params->rate) {
1064                 case 8000:
1065                         stream->u.aac.sampling_freq_index = 0xb;
1066                         break;
1067                 case 11025:
1068                         stream->u.aac.sampling_freq_index = 0xa;
1069                         break;
1070                 case 12000:
1071                         stream->u.aac.sampling_freq_index = 0x9;
1072                         break;
1073                 case 16000:
1074                         stream->u.aac.sampling_freq_index = 0x8;
1075                         break;
1076                 case 22050:
1077                         stream->u.aac.sampling_freq_index = 0x7;
1078                         break;
1079                 case 24000:
1080                         stream->u.aac.sampling_freq_index = 0x6;
1081                         break;
1082                 case 32000:
1083                         stream->u.aac.sampling_freq_index = 0x5;
1084                         break;
1085                 case 44100:
1086                         stream->u.aac.sampling_freq_index = 0x4;
1087                         break;
1088                 case 48000:
1089                         stream->u.aac.sampling_freq_index = 0x3;
1090                         break;
1091                 case 64000:
1092                         stream->u.aac.sampling_freq_index = 0x2;
1093                         break;
1094                 case 88200:
1095                         stream->u.aac.sampling_freq_index = 0x1;
1096                         break;
1097                 case 96000:
1098                         stream->u.aac.sampling_freq_index = 0x0;
1099                         break;
1100                 default:
1101                         dev_err(audio_avp->dev, "Unsupported rate");
1102                         return -EINVAL;
1103                 }
1104                 /* Only Stereo data is supported */
1105                 stream->u.aac.channel_configuration = params->channels;
1106         } else {
1107                 dev_err(audio_avp->dev, "Invalid stream/codec type.");
1108                 return -EINVAL;
1109         }
1110
1111         stream->stream_params.rate = params->rate;
1112         stream->stream_params.channels = params->channels;
1113         stream->stream_params.bits_per_sample = params->bits_per_sample;
1114         avp_stream->period_size = params->fragment_size;
1115
1116         avp_stream->notify_cb = params->fragments_elapsed_cb;
1117         avp_stream->notify_args = params->fragments_elapsed_args;
1118
1119         stream->source_buffer_size = (params->fragments *
1120                         params->fragment_size);
1121         ret = tegra30_avp_mem_alloc(&avp_stream->source_buf,
1122                               stream->source_buffer_size);
1123         if (ret < 0) {
1124                 dev_err(audio_avp->dev, "Failed to allocate source buf memory");
1125                 return ret;
1126         }
1127
1128         stream->source_buffer_system =
1129                         (uintptr_t) avp_stream->source_buf.virt_addr;
1130         stream->source_buffer_avp = avp_stream->source_buf.phys_addr;
1131
1132         if (stream->source_buffer_size > AVP_COMPR_THRESHOLD) {
1133                 stream->stream_notification_interval =
1134                         stream->source_buffer_size - AVP_COMPR_THRESHOLD;
1135         } else {
1136                 stream->stream_notification_interval = avp_stream->period_size;
1137         }
1138         stream->stream_notification_enable = 1;
1139
1140         /* Set DMA params */
1141         ret = tegra30_avp_audio_alloc_dma(&params->dma_params);
1142         if (ret < 0) {
1143                 dev_err(audio_avp->dev, "Failed to allocate DMA. ret %d", ret);
1144                 return ret;
1145         }
1146
1147         if ((params->codec_type == SND_AUDIOCODEC_MP3) ||
1148             (params->codec_type == SND_AUDIOCODEC_AAC)) {
1149                 dev_info(audio_avp->dev, "\n*** STARTING %s Offload PLAYBACK ***\n",
1150                    (params->codec_type == SND_AUDIOCODEC_MP3) ? "MP3" : "AAC");
1151         }
1152         return 0;
1153 }
1154
1155 static int tegra30_avp_compr_set_state(int id, int state)
1156 {
1157         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1158         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1159
1160         dev_vdbg(audio_avp->dev, "%s : id %d state %d",
1161                  __func__, id, state);
1162
1163         switch (state) {
1164         case SNDRV_PCM_TRIGGER_START:
1165         case SNDRV_PCM_TRIGGER_RESUME:
1166                 tegra30_avp_stream_set_state(id, KSSTATE_RUN);
1167                 return 0;
1168         case SNDRV_PCM_TRIGGER_STOP:
1169         case SNDRV_PCM_TRIGGER_SUSPEND:
1170                 tegra30_avp_stream_set_state(id, KSSTATE_STOP);
1171                 return 0;
1172         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1173                 tegra30_avp_stream_set_state(id, KSSTATE_PAUSE);
1174                 return 0;
1175         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1176                 tegra30_avp_stream_set_state(id, KSSTATE_RUN);
1177                 return 0;
1178         case SND_COMPR_TRIGGER_DRAIN:
1179         case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
1180                 avp_stream->is_drain_called = 1;
1181                 return 0;
1182         default:
1183                 dev_err(audio_avp->dev, "Unsupported state.");
1184                 return -EINVAL;
1185         }
1186 }
1187
1188 static void tegra30_avp_compr_data_ready(int id, int bytes)
1189 {
1190         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1191         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1192         struct stream_data *stream = avp_stream->stream;
1193
1194         dev_vdbg(audio_avp->dev, "%s : id %d size %d", __func__, id, bytes);
1195
1196         stream->source_buffer_write_position += bytes;
1197         stream->source_buffer_write_position %= stream->source_buffer_size;
1198
1199         avp_stream->source_buffer_offset += bytes;
1200         while (avp_stream->source_buffer_offset >=
1201                 stream->stream_notification_interval) {
1202                 stream->source_buffer_write_count++;
1203                 avp_stream->source_buffer_offset -=
1204                         stream->stream_notification_interval;
1205         }
1206         return;
1207 }
1208
1209 static int tegra30_avp_compr_write(int id, char __user *buf, int bytes)
1210 {
1211         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1212         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1213         struct stream_data *stream = avp_stream->stream;
1214         void *dst = (char *)(uintptr_t)stream->source_buffer_system +
1215                 stream->source_buffer_write_position;
1216         int avail = 0;
1217         int write = 0;
1218         int ret = 0;
1219
1220         avail = stream->source_buffer_read_position -
1221                 stream->source_buffer_write_position;
1222         if ((avail < 0) || (!avail &&
1223                 (stream->source_buffer_write_count ==
1224                 stream->stream_notification_request)))
1225                 avail += stream->source_buffer_size;
1226
1227         dev_vdbg(audio_avp->dev, "%s : id %d size %d", __func__, id, bytes);
1228
1229         /* TODO: check enough free space is available before writing */
1230         bytes = (bytes > avail) ? avail : bytes;
1231         if (!bytes) {
1232                 dev_dbg(audio_avp->dev, "No free space in ring buffer.");
1233                 DUMP_AVP_STATUS(avp_stream);
1234                 return bytes;
1235         }
1236
1237         write = stream->source_buffer_size -
1238                 stream->source_buffer_write_position;
1239         if (write > bytes) {
1240                 ret = copy_from_user(dst, buf, bytes);
1241                 if (ret < 0) {
1242                         dev_err(audio_avp->dev, "Failed to copy user data.");
1243                         return -EFAULT;
1244                 }
1245         } else {
1246                 ret = copy_from_user(dst, buf, write);
1247                 if (ret < 0) {
1248                         dev_err(audio_avp->dev, "Failed to copy user data.");
1249                         return -EFAULT;
1250                 }
1251
1252                 ret = copy_from_user((void *)(uintptr_t)stream->source_buffer_system,
1253                                 buf + write, bytes - write);
1254                 if (ret < 0) {
1255                         dev_err(audio_avp->dev, "Failed to copy user data.");
1256                         return -EFAULT;
1257                 }
1258         }
1259
1260         stream->source_buffer_write_position += bytes;
1261         stream->source_buffer_write_position %= stream->source_buffer_size;
1262
1263         avp_stream->source_buffer_offset += bytes;
1264         while (avp_stream->source_buffer_offset >=
1265                 stream->stream_notification_interval) {
1266                 stream->source_buffer_write_count++;
1267                 avp_stream->source_buffer_offset -=
1268                         stream->stream_notification_interval;
1269         }
1270         DUMP_AVP_STATUS(avp_stream);
1271         return bytes;
1272 }
1273
1274 static int tegra30_avp_compr_get_position(int id,
1275                         struct snd_compr_tstamp *tstamp)
1276 {
1277         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1278         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1279         struct stream_data *stream = avp_stream->stream;
1280
1281         tstamp->byte_offset = stream->source_buffer_write_position;
1282         tstamp->copied_total = stream->source_buffer_write_position +
1283                 (stream->source_buffer_write_count *
1284                  stream->stream_notification_interval);
1285         tstamp->pcm_frames = stream->source_buffer_presentation_position;
1286         tstamp->pcm_io_frames = stream->source_buffer_presentation_position;
1287         tstamp->sampling_rate = stream->stream_params.rate;
1288
1289         dev_vdbg(audio_avp->dev, "%s id %d off %d copied %d pcm %d pcm io %d",
1290                  __func__, id, (int)tstamp->byte_offset,
1291                  (int)tstamp->copied_total, (int)tstamp->pcm_frames,
1292                  (int)tstamp->pcm_io_frames);
1293
1294         return 0;
1295 }
1296
1297 static int tegra30_avp_compr_get_caps(struct snd_compr_caps *caps)
1298 {
1299         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1300
1301         dev_vdbg(audio_avp->dev, "%s : dir %d", __func__, caps->direction);
1302
1303         if (caps->direction == SND_COMPRESS_PLAYBACK)
1304                 memcpy(caps, &tegra30_avp_compr_caps[SND_COMPRESS_PLAYBACK],
1305                         sizeof(struct snd_compr_caps));
1306         else
1307                 memcpy(caps, &tegra30_avp_compr_caps[SND_COMPRESS_CAPTURE],
1308                         sizeof(struct snd_compr_caps));
1309         return 0;
1310 }
1311
1312 static int tegra30_avp_compr_get_codec_caps(struct snd_compr_codec_caps *codec)
1313 {
1314         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1315
1316         dev_vdbg(audio_avp->dev, "%s : codec %d", __func__, codec->codec);
1317
1318         switch (codec->codec) {
1319         case SND_AUDIOCODEC_MP3:
1320                 memcpy(codec, &tegra30_avp_compr_codec_caps[avp_compr_mp3],
1321                         sizeof(struct snd_compr_codec_caps));
1322                 return 0;
1323         case SND_AUDIOCODEC_AAC:
1324                 memcpy(codec, &tegra30_avp_compr_codec_caps[avp_compr_aac],
1325                         sizeof(struct snd_compr_codec_caps));
1326                 return 0;
1327         default:
1328                 dev_err(audio_avp->dev, "Unsupported codec %d", codec->codec);
1329                 return -EINVAL;
1330         }
1331 }
1332
1333 static int tegra30_avp_compr_set_volume(int id, int left, int right)
1334 {
1335         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1336         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1337         struct stream_data *stream = avp_stream->stream;
1338
1339         dev_vdbg(audio_avp->dev, "%s id %d left vol %d right vol %d",
1340                  __func__, id, left, right);
1341
1342         if (!stream) {
1343                 dev_err(audio_avp->dev, "AVP platform not initialized.");
1344                 return -ENODEV;
1345         }
1346
1347         stream->stream_volume[0] = left;
1348         stream->stream_volume[1] = right;
1349
1350         return 0;
1351 }
1352
1353 /* Common APIs for pcm and compress */
1354 static void tegra30_avp_stream_close(int id)
1355 {
1356         struct tegra30_avp_audio *audio_avp = avp_audio_ctx;
1357         struct tegra30_avp_stream *avp_stream = &audio_avp->avp_stream[id];
1358         struct stream_data *stream = avp_stream->stream;
1359
1360         dev_vdbg(audio_avp->dev, "%s id %d", __func__, id);
1361
1362         if (!stream) {
1363                 dev_err(audio_avp->dev, "AVP platform not initialized.");
1364                 return;
1365         }
1366         tegra30_avp_mem_free(&avp_stream->source_buf);
1367         stream->stream_allocated = 0;
1368         tegra30_avp_stream_set_state(id, KSSTATE_STOP);
1369
1370         if (id == loopback_stream_id)
1371                 return;
1372
1373         if (atomic_dec_and_test(&audio_avp->stream_active_count)) {
1374                 tegra30_avp_audio_free_dma();
1375                 tegra30_avp_audio_set_state(KSSTATE_STOP);
1376         }
1377 }
1378
1379 static struct tegra_offload_ops avp_audio_platform = {
1380         .device_ops = {
1381                 .set_hw_rate = tegra30_avp_set_hw_rate,
1382                 .alloc_shared_mem = tegra30_avp_alloc_shared_mem,
1383                 .free_shared_mem = tegra30_avp_free_shared_mem,
1384         },
1385         .pcm_ops = {
1386                 .stream_open = tegra30_avp_pcm_open,
1387                 .stream_close = tegra30_avp_stream_close,
1388                 .set_stream_params = tegra30_avp_pcm_set_params,
1389                 .set_stream_state = tegra30_avp_pcm_set_state,
1390                 .get_stream_position = tegra30_avp_pcm_get_position,
1391                 .data_ready = tegra30_avp_pcm_data_ready,
1392         },
1393         .loopback_ops = {
1394                 .stream_open = tegra30_avp_pcm_open,
1395                 .stream_close = tegra30_avp_stream_close,
1396                 .set_stream_params = tegra30_avp_loopback_set_params,
1397                 .set_stream_state = tegra30_avp_loopback_set_state,
1398                 .get_stream_position = tegra30_avp_loopback_get_position,
1399                 .data_ready = tegra30_avp_loopback_data_ready,
1400         },
1401         .compr_ops = {
1402                 .stream_open = tegra30_avp_compr_open,
1403                 .stream_close = tegra30_avp_stream_close,
1404                 .set_stream_params = tegra30_avp_compr_set_params,
1405                 .set_stream_state = tegra30_avp_compr_set_state,
1406                 .get_stream_position = tegra30_avp_compr_get_position,
1407                 .data_ready = tegra30_avp_compr_data_ready,
1408                 .write = tegra30_avp_compr_write,
1409                 .get_caps = tegra30_avp_compr_get_caps,
1410                 .get_codec_caps = tegra30_avp_compr_get_codec_caps,
1411                 .set_stream_volume = tegra30_avp_compr_set_volume,
1412         },
1413 };
1414
1415 static u64 tegra_dma_mask = DMA_BIT_MASK(32);
1416 static int tegra30_avp_audio_probe(struct platform_device *pdev)
1417 {
1418         struct tegra30_avp_audio *audio_avp;
1419
1420         pr_debug("tegra30_avp_audio_platform_probe platform probe started\n");
1421
1422         audio_avp = devm_kzalloc(&pdev->dev, sizeof(*audio_avp), GFP_KERNEL);
1423         if (!audio_avp) {
1424                 dev_err(&pdev->dev, "Can't allocate tegra30_avp_audio\n");
1425                 return -ENOMEM;
1426         }
1427         dev_set_drvdata(&pdev->dev, audio_avp);
1428         audio_avp->dev = &pdev->dev;
1429
1430         /* set the ops */
1431         if (tegra_register_offload_ops(&avp_audio_platform)) {
1432                 dev_err(&pdev->dev, "Failed to register avp audio device.");
1433                 return -EPROBE_DEFER;
1434         }
1435
1436         spin_lock_init(&audio_avp->lock);
1437         pdev->dev.dma_mask = &tegra_dma_mask;
1438         pdev->dev.coherent_dma_mask = tegra_dma_mask;
1439         avp_audio_ctx = audio_avp;
1440         pr_info("tegra30_avp_audio_platform_probe successful.");
1441         return 0;
1442 }
1443
1444 static int tegra30_avp_audio_remove(struct platform_device *pdev)
1445 {
1446         struct tegra30_avp_audio *audio_avp = dev_get_drvdata(&pdev->dev);
1447
1448         dev_vdbg(&pdev->dev, "%s", __func__);
1449
1450         tegra_deregister_offload_ops();
1451         if (audio_avp->nvavp_client)
1452                 tegra_nvavp_audio_client_release(audio_avp->nvavp_client);
1453         tegra30_avp_mem_free(&audio_avp->cmd_buf_mem);
1454         tegra30_avp_mem_free(&audio_avp->param_mem);
1455         tegra30_avp_mem_free(&audio_avp->ucode_mem);
1456
1457         return 0;
1458 }
1459
1460 static const struct of_device_id tegra30_avp_audio_of_match[] = {
1461         { .compatible = "nvidia,tegra30-avp-audio", },
1462         {},
1463 };
1464
1465 static struct platform_driver tegra30_avp_audio_driver = {
1466         .driver = {
1467                 .name = DRV_NAME,
1468                 .owner = THIS_MODULE,
1469                 .of_match_table = tegra30_avp_audio_of_match,
1470         },
1471         .probe = tegra30_avp_audio_probe,
1472         .remove = tegra30_avp_audio_remove,
1473 };
1474 module_platform_driver(tegra30_avp_audio_driver);
1475
1476 MODULE_AUTHOR("Sumit Bhattacharya <sumitb@nvidia.com>");
1477 MODULE_DESCRIPTION("Tegra30 AVP Audio driver");
1478 MODULE_LICENSE("GPL");
1479 MODULE_ALIAS("platform:" DRV_NAME);
1480 MODULE_DEVICE_TABLE(of, tegra30_avp_audio_of_match);