Add platform file for Iaudio M5.
[Rockbox.git] / firmware / pcm_playback.c
blobeb5404c1a80ab4abff0febe9250c417deb567ff7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "system.h"
20 #include "kernel.h"
21 #include "logf.h"
22 #include "audio.h"
23 #if defined(HAVE_WM8975)
24 #include "wm8975.h"
25 #elif defined(HAVE_WM8758)
26 #include "wm8758.h"
27 #elif defined(HAVE_WM8731) || defined(HAVE_WM8721)
28 #include "wm8731l.h"
29 #elif CONFIG_CPU == PNX0101
30 #include "string.h"
31 #include "pnx0101.h"
32 #endif
34 /**
35 * APIs implemented in the target-specific portion:
36 * Public -
37 * pcm_init
38 * pcm_get_bytes_waiting
39 * pcm_calculate_peaks
40 * Semi-private -
41 * pcm_play_dma_start
42 * pcm_play_dma_stop
43 * pcm_play_pause_pause
44 * pcm_play_pause_unpause
47 /** These items may be implemented target specifically or need to
48 be shared semi-privately **/
50 /* the registered callback function to ask for more mp3 data */
51 volatile pcm_more_callback_type pcm_callback_for_more = NULL;
52 volatile bool pcm_playing = false;
53 volatile bool pcm_paused = false;
55 void pcm_play_dma_start(const void *addr, size_t size);
56 void pcm_play_dma_stop(void);
57 void pcm_play_pause_pause(void);
58 void pcm_play_pause_unpause(void);
60 /** Functions that require targeted implementation **/
62 #if !defined(CPU_COLDFIRE) && (CONFIG_CPU != S3C2440)
64 #if (CONFIG_CPU == PNX0101)
66 #define DMA_BUF_SAMPLES 0x100
68 short __attribute__((section(".dmabuf"))) dma_buf_left[DMA_BUF_SAMPLES];
69 short __attribute__((section(".dmabuf"))) dma_buf_right[DMA_BUF_SAMPLES];
71 static int pcm_freq = HW_SAMPR_DEFAULT; /* 44.1 is default */
73 unsigned short* p IBSS_ATTR;
74 size_t p_size IBSS_ATTR;
76 void pcm_play_dma_start(const void *addr, size_t size)
78 p = (unsigned short*)addr;
79 p_size = size;
81 pcm_playing = true;
84 void pcm_play_dma_stop(void)
86 pcm_playing = false;
89 void pcm_play_pause_pause(void)
93 void pcm_play_pause_unpause(void)
97 static inline void fill_dma_buf(int offset)
99 short *l, *r, *lend;
101 l = dma_buf_left + offset;
102 lend = l + DMA_BUF_SAMPLES / 2;
103 r = dma_buf_right + offset;
105 if (pcm_playing && !pcm_paused)
109 int count;
110 unsigned short *tmp_p;
111 count = MIN(p_size / 4, (size_t)(lend - l));
112 tmp_p = p;
113 p_size -= count * 4;
115 if ((int)l & 3)
117 *l++ = *tmp_p++;
118 *r++ = *tmp_p++;
119 count--;
121 while (count >= 4)
123 asm("ldmia %0!, {r0, r1, r2, r3}\n\t"
124 "and r4, r0, %3\n\t"
125 "orr r4, r4, r1, lsl #16\n\t"
126 "and r5, r2, %3\n\t"
127 "orr r5, r5, r3, lsl #16\n\t"
128 "stmia %1!, {r4, r5}\n\t"
129 "bic r4, r1, %3\n\t"
130 "orr r4, r4, r0, lsr #16\n\t"
131 "bic r5, r3, %3\n\t"
132 "orr r5, r5, r2, lsr #16\n\t"
133 "stmia %2!, {r4, r5}"
134 : "+r" (tmp_p), "+r" (l), "+r" (r)
135 : "r" (0xffff)
136 : "r0", "r1", "r2", "r3", "r4", "r5", "memory");
137 count -= 4;
139 while (count > 0)
141 *l++ = *tmp_p++;
142 *r++ = *tmp_p++;
143 count--;
145 p = tmp_p;
146 if (l >= lend)
147 return;
148 else if (pcm_callback_for_more)
149 pcm_callback_for_more((unsigned char**)&p,
150 &p_size);
152 while (p_size);
153 pcm_playing = false;
156 if (l < lend)
158 memset(l, 0, sizeof(short) * (lend - l));
159 memset(r, 0, sizeof(short) * (lend - l));
163 static void audio_irq(void)
165 unsigned long st = DMAINTSTAT & ~DMAINTEN;
166 int i;
167 for (i = 0; i < 2; i++)
168 if (st & (1 << i))
170 fill_dma_buf((i == 1) ? 0 : DMA_BUF_SAMPLES / 2);
171 DMAINTSTAT = 1 << i;
175 unsigned long physical_address(void *p)
177 unsigned long adr = (unsigned long)p;
178 return (MMUBLOCK((adr >> 21) & 0xf) << 21) | (adr & ((1 << 21) - 1));
181 void pcm_init(void)
183 int i;
185 pcm_playing = false;
186 pcm_paused = false;
187 pcm_callback_for_more = NULL;
189 memset(dma_buf_left, 0, sizeof(dma_buf_left));
190 memset(dma_buf_right, 0, sizeof(dma_buf_right));
192 for (i = 0; i < 8; i++)
194 DMASRC(i) = 0;
195 DMADEST(i) = 0;
196 DMALEN(i) = 0x1ffff;
197 DMAR0C(i) = 0;
198 DMAR10(i) = 0;
199 DMAR1C(i) = 0;
202 DMAINTSTAT = 0xc000ffff;
203 DMAINTEN = 0xc000ffff;
205 DMASRC(0) = physical_address(dma_buf_left);
206 DMADEST(0) = 0x80200280;
207 DMALEN(0) = 0xff;
208 DMAR1C(0) = 0;
209 DMAR0C(0) = 0x40408;
211 DMASRC(1) = physical_address(dma_buf_right);
212 DMADEST(1) = 0x80200284;
213 DMALEN(1) = 0xff;
214 DMAR1C(1) = 0;
215 DMAR0C(1) = 0x40409;
217 irq_set_int_handler(0x1b, audio_irq);
218 irq_enable_int(0x1b);
220 DMAINTSTAT = 1;
221 DMAINTSTAT = 2;
222 DMAINTEN &= ~3;
223 DMAR10(0) |= 1;
224 DMAR10(1) |= 1;
227 void pcm_set_frequency(unsigned int frequency)
229 (void)frequency;
230 pcm_freq = HW_SAMPR_DEFAULT;
232 size_t pcm_get_bytes_waiting(void)
234 return p_size;
236 #endif /* CONFIG_CPU == */
238 /* dummy functions for those not actually supporting all this yet */
239 void pcm_apply_settings(void)
242 /** **/
244 void pcm_mute(bool mute)
246 #if defined(HAVE_WM8975) || defined(HAVE_WM8758) \
247 || defined(HAVE_WM8731) || defined(HAVE_WM8721)
248 audiohw_mute(mute);
249 #endif
250 if (mute)
251 sleep(HZ/16);
253 #if !defined(CPU_PP)
255 * This function goes directly into the DMA buffer to calculate the left and
256 * right peak values. To avoid missing peaks it tries to look forward two full
257 * peek periods (2/HZ sec, 100% overlap), although it's always possible that
258 * the entire period will not be visible. To reduce CPU load it only looks at
259 * every third sample, and this can be reduced even further if needed (even
260 * every tenth sample would still be pretty accurate).
263 /* Check for a peak every PEAK_STRIDE samples */
264 #define PEAK_STRIDE 3
265 /* Up to 1/50th of a second of audio for peak calculation */
266 /* This should use NATIVE_FREQUENCY, or eventually an adjustable freq. value */
267 #define PEAK_SAMPLES (44100/50)
268 void pcm_calculate_peaks(int *left, int *right)
270 #if (CONFIG_CPU == S3C2440)
271 (void)left;
272 (void)right;
273 #else
274 short *addr;
275 short *end;
277 #if CONFIG_CPU == PNX0101
278 size_t samples = p_size / 4;
279 addr = p;
280 #endif
282 if (samples > PEAK_SAMPLES)
283 samples = PEAK_SAMPLES - (PEAK_STRIDE - 1);
284 else
285 samples -= MIN(PEAK_STRIDE - 1, samples);
287 end = &addr[samples * 2];
290 if (left && right) {
291 int left_peak = 0, right_peak = 0;
293 while (addr < end) {
294 int value;
295 if ((value = addr [0]) > left_peak)
296 left_peak = value;
297 else if (-value > left_peak)
298 left_peak = -value;
300 if ((value = addr [PEAK_STRIDE | 1]) > right_peak)
301 right_peak = value;
302 else if (-value > right_peak)
303 right_peak = -value;
305 addr = &addr[PEAK_STRIDE * 2];
308 *left = left_peak;
309 *right = right_peak;
311 else if (left || right) {
312 int peak_value = 0, value;
314 if (right)
315 addr += (PEAK_STRIDE | 1);
317 while (addr < end) {
318 if ((value = addr [0]) > peak_value)
319 peak_value = value;
320 else if (-value > peak_value)
321 peak_value = -value;
323 addr += PEAK_STRIDE * 2;
326 if (left)
327 *left = peak_value;
328 else
329 *right = peak_value;
331 #endif
333 #endif
334 #endif /* CPU_COLDFIRE */
336 /****************************************************************************
337 * Functions that do not require targeted implementation but only a targeted
338 * interface
341 /* Common code to pcm_play_data and pcm_play_pause
342 Returns true if DMA playback was started, else false. */
343 bool pcm_play_data_start(pcm_more_callback_type get_more,
344 unsigned char *start, size_t size)
346 if (!(start && size))
348 size = 0;
349 if (get_more)
350 get_more(&start, &size);
353 if (start && size)
355 pcm_play_dma_start(start, size);
356 return true;
359 return false;
362 void pcm_play_data(pcm_more_callback_type get_more,
363 unsigned char *start, size_t size)
365 pcm_callback_for_more = get_more;
367 if (pcm_play_data_start(get_more, start, size) && pcm_paused)
369 pcm_paused = false;
370 pcm_play_pause(false);
374 void pcm_play_pause(bool play)
376 bool needs_change = pcm_paused == play;
378 /* This needs to be done ahead of the rest to prevent infinite
379 recursion from pcm_play_data */
380 pcm_paused = !play;
382 if (pcm_playing && needs_change)
384 if (play)
386 if (pcm_get_bytes_waiting())
388 logf("unpause");
389 pcm_play_pause_unpause();
391 else
393 logf("unpause, no data waiting");
394 if (!pcm_play_data_start(pcm_callback_for_more, NULL, 0))
396 pcm_play_dma_stop();
397 logf("unpause attempted, no data");
401 else
403 logf("pause");
404 pcm_play_pause_pause();
406 } /* pcm_playing && needs_change */
409 void pcm_play_stop(void)
411 if (pcm_playing)
412 pcm_play_dma_stop();
415 bool pcm_is_playing(void)
417 return pcm_playing;
420 bool pcm_is_paused(void)
422 return pcm_paused;