as3525: add some comments in the microphone channel copy loop
[kugel-rb.git] / firmware / target / arm / as3525 / pcm-as3525.c
blob4033a9b88db028ca4bbf107c8c50419e96edbce3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright © 2008-2009 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"
31 #include "mmu-arm.h"
33 #define MAX_TRANSFER (4*((1<<11)-1)) /* maximum data we can transfer via DMA
34 * i.e. 32 bits at once (size of I2SO_DATA)
35 * and the number of 32bits words has to
36 * fit in 11 bits of DMA register */
38 static unsigned char *dma_start_addr;
39 static size_t dma_size; /* in 4*32 bits */
40 static void dma_callback(void);
41 static int locked = 0;
43 /* Mask the DMA interrupt */
44 void pcm_play_lock(void)
46 if(++locked == 1)
47 VIC_INT_EN_CLEAR = INTERRUPT_DMAC;
50 /* Unmask the DMA interrupt if enabled */
51 void pcm_play_unlock(void)
53 if(--locked == 0)
54 VIC_INT_ENABLE = INTERRUPT_DMAC;
57 static void play_start_pcm(void)
59 const unsigned char* addr = dma_start_addr;
60 size_t size = dma_size;
61 if(size > MAX_TRANSFER)
62 size = MAX_TRANSFER;
64 dma_size -= size;
65 dma_start_addr += size;
67 clean_dcache_range((void*)addr, size); /* force write back */
68 dma_enable_channel(1, (void*)addr, (void*)I2SOUT_DATA, DMA_PERI_I2SOUT,
69 DMAC_FLOWCTRL_DMAC_MEM_TO_PERI, true, false, size >> 2, DMA_S1,
70 dma_callback);
73 static void dma_callback(void)
75 if(!dma_size)
77 register pcm_more_callback_type get_more = pcm_callback_for_more;
78 if(get_more)
79 get_more(&dma_start_addr, &dma_size);
82 if(!dma_size)
84 pcm_play_dma_stop();
85 pcm_play_dma_stopped_callback();
87 else
88 play_start_pcm();
91 void pcm_play_dma_start(const void *addr, size_t size)
93 dma_size = size;
94 dma_start_addr = (unsigned char*)addr;
96 CGU_PERI |= CGU_I2SOUT_APB_CLOCK_ENABLE;
97 CGU_AUDIO |= (1<<11);
99 dma_retain();
101 play_start_pcm();
104 void pcm_play_dma_stop(void)
106 dma_disable_channel(1);
107 dma_size = 0;
109 dma_release();
111 CGU_PERI &= ~CGU_I2SOUT_APB_CLOCK_ENABLE;
112 CGU_AUDIO &= ~(1<<11);
115 void pcm_play_dma_pause(bool pause)
117 if(pause)
118 dma_disable_channel(1);
119 else
120 play_start_pcm();
123 void pcm_play_dma_init(void)
125 CGU_PERI |= CGU_I2SOUT_APB_CLOCK_ENABLE;
127 I2SOUT_CONTROL = (1<<6)|(1<<3) /* enable dma, stereo */;
129 audiohw_preinit();
132 void pcm_postinit(void)
134 audiohw_postinit();
137 /* divider is 9 bits but the highest one (for 8kHz) fit in 8 bits */
138 static const unsigned char divider[SAMPR_NUM_FREQ] = {
139 [HW_FREQ_96] = ((AS3525_MCLK_FREQ/128 + SAMPR_96/2) / SAMPR_96) - 1,
140 [HW_FREQ_88] = ((AS3525_MCLK_FREQ/128 + SAMPR_88/2) / SAMPR_88) - 1,
141 [HW_FREQ_64] = ((AS3525_MCLK_FREQ/128 + SAMPR_64/2) / SAMPR_64) - 1,
142 [HW_FREQ_48] = ((AS3525_MCLK_FREQ/128 + SAMPR_48/2) / SAMPR_48) - 1,
143 [HW_FREQ_44] = ((AS3525_MCLK_FREQ/128 + SAMPR_44/2) / SAMPR_44) - 1,
144 [HW_FREQ_32] = ((AS3525_MCLK_FREQ/128 + SAMPR_32/2) / SAMPR_32) - 1,
145 [HW_FREQ_24] = ((AS3525_MCLK_FREQ/128 + SAMPR_24/2) / SAMPR_24) - 1,
146 [HW_FREQ_22] = ((AS3525_MCLK_FREQ/128 + SAMPR_22/2) / SAMPR_22) - 1,
147 [HW_FREQ_16] = ((AS3525_MCLK_FREQ/128 + SAMPR_16/2) / SAMPR_16) - 1,
148 [HW_FREQ_12] = ((AS3525_MCLK_FREQ/128 + SAMPR_12/2) / SAMPR_12) - 1,
149 [HW_FREQ_11] = ((AS3525_MCLK_FREQ/128 + SAMPR_11/2) / SAMPR_11) - 1,
150 [HW_FREQ_8 ] = ((AS3525_MCLK_FREQ/128 + SAMPR_8 /2) / SAMPR_8 ) - 1,
153 static inline unsigned char mclk_divider(void)
155 return divider[pcm_fsel];
158 void pcm_dma_apply_settings(void)
160 int cgu_audio = CGU_AUDIO; /* read register */
161 cgu_audio &= ~(3 << 0); /* clear i2sout MCLK_SEL */
162 cgu_audio |= (AS3525_MCLK_SEL << 0); /* set i2sout MCLK_SEL */
163 cgu_audio &= ~(0x1ff << 2); /* clear i2sout divider */
164 cgu_audio |= mclk_divider() << 2; /* set new i2sout divider */
165 CGU_AUDIO = cgu_audio; /* write back register */
168 size_t pcm_get_bytes_waiting(void)
170 return dma_size;
173 const void * pcm_play_dma_get_peak_buffer(int *count)
175 *count = dma_size >> 2;
176 return (const void*)dma_start_addr;
179 #ifdef HAVE_PCM_DMA_ADDRESS
180 void * pcm_dma_addr(void *addr)
182 if (addr != NULL)
183 addr = UNCACHED_ADDR(addr);
184 return addr;
186 #endif
189 /****************************************************************************
190 ** Recording DMA transfer
192 #ifdef HAVE_RECORDING
194 static int rec_locked = 0;
195 static unsigned char *rec_dma_start_addr;
196 static size_t rec_dma_size, rec_dma_transfer_size;
197 static void rec_dma_callback(void);
198 #if CONFIG_CPU == AS3525
199 /* points to the samples which need to be duplicated into the right channel */
200 static int16_t *mono_samples;
201 #endif
204 void pcm_rec_lock(void)
206 if(++rec_locked == 1)
207 VIC_INT_EN_CLEAR = INTERRUPT_DMAC;
211 void pcm_rec_unlock(void)
213 if(--rec_locked == 0)
214 VIC_INT_ENABLE = INTERRUPT_DMAC;
218 static void rec_dma_start(void)
220 rec_dma_transfer_size = rec_dma_size;
222 /* We are limited to 8188 DMA transfers, and the recording core asks for
223 * 8192 bytes. Avoid splitting 8192 bytes transfers in 8188 + 4 */
224 if(rec_dma_transfer_size > 4096)
225 rec_dma_transfer_size = 4096;
227 dma_enable_channel(1, (void*)I2SIN_DATA, rec_dma_start_addr, DMA_PERI_I2SIN,
228 DMAC_FLOWCTRL_DMAC_PERI_TO_MEM, false, true,
229 rec_dma_transfer_size >> 2, DMA_S4, rec_dma_callback);
233 /* if needed, duplicate samples of the working channel until the given bound */
234 static inline void mono2stereo(int16_t *end)
236 #if CONFIG_CPU == AS3525
237 if(audio_channels != 1) /* only for microphone */
238 return;
239 #if 0
240 /* load pointer in a register and avoid updating it in each loop */
241 register int16_t *samples = mono_samples;
243 do {
244 int16_t left = *samples++; // load 1 sample of the left-channel
245 *samples++ = left; // copy it in the right-channel
246 } while(samples != end);
248 mono_samples = samples; /* update pointer */
249 #else
250 /* gcc doesn't use pre indexing : let's save 1 cycle */
251 int16_t left;
252 asm (
253 "1: ldrh %0, [%1], #2 \n" // load 1 sample of the left-channel
254 " strh %0, [%1], #2 \n" // copy it in the right-channel
255 " cmp %1, %2 \n" // are we finished?
256 " bne 1b \n"
257 : "=r"(left), "+r"(mono_samples)
258 : "r"(end)
259 : "memory"
261 #endif /* C / ASM */
262 #else
263 /* microphone recording is stereo on as3525v2 */
264 (void)end;
265 #endif
268 static void rec_dma_callback(void)
270 rec_dma_size -= rec_dma_transfer_size;
271 rec_dma_start_addr += rec_dma_transfer_size;
273 /* the 2nd channel is silent when recording microphone on as3525v1 */
274 mono2stereo(UNCACHED_ADDR((int16_t*)rec_dma_start_addr));
276 if(!rec_dma_size)
278 register pcm_more_callback_type2 more_ready = pcm_callback_more_ready;
279 if (!more_ready || more_ready(0) < 0)
281 /* Finished recording */
282 pcm_rec_dma_stop();
283 pcm_rec_dma_stopped_callback();
284 return;
288 rec_dma_start();
292 void pcm_rec_dma_record_more(void *start, size_t size)
294 dump_dcache_range(start, size);
295 rec_dma_start_addr = start;
296 #if CONFIG_CPU == AS3525
297 mono_samples = UNCACHED_ADDR(start);
298 #endif
299 rec_dma_size = size;
303 void pcm_rec_dma_stop(void)
305 dma_disable_channel(1);
306 dma_release();
307 rec_dma_size = 0;
309 I2SOUT_CONTROL &= ~(1<<5); /* source = i2soutif fifo */
310 I2SIN_CONTROL &= ~(1<<11); /* disable dma */
312 CGU_AUDIO &= ~((1<<23)|(1<<11));
313 CGU_PERI &= ~(CGU_I2SIN_APB_CLOCK_ENABLE|CGU_I2SOUT_APB_CLOCK_ENABLE);
317 void pcm_rec_dma_start(void *addr, size_t size)
319 dump_dcache_range(addr, size);
320 rec_dma_start_addr = addr;
321 #if CONFIG_CPU == AS3525
322 mono_samples = UNCACHED_ADDR(addr);
323 #endif
324 rec_dma_size = size;
326 dma_retain();
328 CGU_PERI |= CGU_I2SIN_APB_CLOCK_ENABLE|CGU_I2SOUT_APB_CLOCK_ENABLE;
329 CGU_AUDIO |= ((1<<23)|(1<<11));
331 I2SOUT_CONTROL |= 1<<5; /* source = loopback from i2sin fifo */
333 I2SIN_CONTROL |= (1<<11)|(1<<5); /* enable dma, 14bits samples */
335 rec_dma_start();
339 void pcm_rec_dma_close(void)
344 void pcm_rec_dma_init(void)
346 int cgu_audio = CGU_AUDIO; /* read register */
347 cgu_audio &= ~(3 << 12); /* clear i2sin MCLK_SEL */
348 cgu_audio |= (AS3525_MCLK_SEL << 12); /* set i2sin MCLK_SEL */
349 cgu_audio &= ~(0x1ff << 14); /* clear i2sin divider */
350 cgu_audio |= mclk_divider() << 14; /* set new i2sin divider */
351 CGU_AUDIO = cgu_audio; /* write back register */
353 /* i2c clk src = I2SOUTIF, sdata src = AFE,
354 * data valid at positive edge of SCLK */
355 I2SIN_CONTROL = (1<<2);
359 const void * pcm_rec_dma_get_peak_buffer(void)
361 pcm_rec_lock();
362 int16_t *addr = UNCACHED_ADDR((int16_t *)DMAC_CH_DST_ADDR(1));
363 mono2stereo(addr);
364 pcm_rec_unlock();
366 return addr;
369 #endif /* HAVE_RECORDING */