Fixed compiler warnings.
[kugel-rb.git] / firmware / target / arm / as3525 / pcm-as3525.c
blob650a86f85c44bce588d5bde7de696507f1176804
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright © 2008 Rafaël Carré
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "system.h"
22 #include "audio.h"
23 #include "string.h"
24 #include "as3525.h"
25 #include "pl081.h"
26 #include "dma-target.h"
27 #include "clock-target.h"
28 #include "panic.h"
29 #include "as3514.h"
30 #include "audiohw.h"
32 #define MAX_TRANSFER (4*((1<<11)-1)) /* maximum data we can transfer via DMA
33 * i.e. 32 bits at once (size of I2SO_DATA)
34 * and the number of 32bits words has to
35 * fit in 11 bits of DMA register */
37 static unsigned char *dma_start_addr;
38 static size_t dma_size; /* in 4*32 bits */
39 static void dma_callback(void);
40 static int locked = 0;
42 /* Mask the DMA interrupt */
43 void pcm_play_lock(void)
45 if(++locked == 1)
46 VIC_INT_EN_CLEAR = INTERRUPT_DMAC;
49 /* Unmask the DMA interrupt if enabled */
50 void pcm_play_unlock(void)
52 if(--locked == 0)
53 VIC_INT_ENABLE |= INTERRUPT_DMAC;
56 static void play_start_pcm(void)
58 const unsigned char* addr = dma_start_addr;
59 size_t size = dma_size;
60 if(size > MAX_TRANSFER)
61 size = MAX_TRANSFER;
63 if((unsigned int)dma_start_addr & 3)
64 panicf("unaligned pointer!");
66 dma_size -= size;
67 dma_start_addr += size;
69 CGU_PERI |= CGU_I2SOUT_APB_CLOCK_ENABLE;
70 CGU_AUDIO |= (1<<11);
72 dma_enable_channel(1, (void*)addr, (void*)I2SOUT_DATA, DMA_PERI_I2SOUT,
73 DMAC_FLOWCTRL_DMAC_MEM_TO_PERI, true, false, size >> 2, DMA_S1,
74 dma_callback);
77 static void dma_callback(void)
79 if(!dma_size)
81 register pcm_more_callback_type get_more = pcm_callback_for_more;
82 if(get_more)
83 get_more(&dma_start_addr, &dma_size);
86 if(!dma_size)
87 pcm_play_dma_stop();
88 else
89 play_start_pcm();
92 void pcm_play_dma_start(const void *addr, size_t size)
94 dma_size = size;
95 dma_start_addr = (unsigned char*)addr;
97 dma_retain();
99 play_start_pcm();
102 void pcm_play_dma_stop(void)
104 dma_disable_channel(1);
105 dma_size = 0;
107 dma_release();
109 CGU_PERI &= ~CGU_I2SOUT_APB_CLOCK_ENABLE;
110 CGU_AUDIO &= ~(1<<11);
113 void pcm_play_dma_pause(bool pause)
115 if(pause)
116 dma_disable_channel(1);
117 else
118 play_start_pcm();
121 void pcm_play_dma_init(void)
123 CGU_PERI |= CGU_I2SOUT_APB_CLOCK_ENABLE;
125 /* clock source PLLA, minimal frequency */
126 CGU_AUDIO |= (511<<2) | (1<<0);
128 I2SOUT_CONTROL |= (1<<6) ; /* enable dma */
129 I2SOUT_CONTROL |= (1<<3) ; /* stereo */
130 I2SOUT_CONTROL &= ~(1<<2); /* 16 bit samples */
132 audiohw_preinit();
135 void pcm_postinit(void)
137 audiohw_postinit();
140 void pcm_dma_apply_settings(void)
142 unsigned long frequency = pcm_sampr;
144 const int divider = (((AS3525_PLLA_FREQ/128) + (frequency/2)) / frequency) - 1;
145 if(divider < 0 || divider > 511)
146 panicf("unsupported frequency %ld", frequency);
148 CGU_AUDIO &= ~(((511 ^ divider) << 2) /* I2SOUT */
149 /*| ((511 ^ divider) << 14) */ /* I2SIN */
153 size_t pcm_get_bytes_waiting(void)
155 return dma_size;
158 const void * pcm_play_dma_get_peak_buffer(int *count)
160 *count = dma_size >> 2;
161 return (const void*)dma_start_addr;
165 /****************************************************************************
166 ** Recording DMA transfer
168 #ifdef HAVE_RECORDING
169 void pcm_rec_lock(void)
173 void pcm_rec_unlock(void)
177 void pcm_record_more(void *start, size_t size)
179 (void)start;
180 (void)size;
183 void pcm_rec_dma_stop(void)
187 void pcm_rec_dma_start(void *addr, size_t size)
189 (void)addr;
190 (void)size;
193 void pcm_rec_dma_close(void)
198 void pcm_rec_dma_init(void)
203 const void * pcm_rec_dma_get_peak_buffer(int *count)
205 (void)count;
208 #endif /* HAVE_RECORDING */