1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
24 #if CONFIG_CPU == PNX0101
26 #endif /* CONFIG_CPU == PNX0101 */
29 * APIs implemented in the target-specific portion:
32 * pcm_get_bytes_waiting
37 * pcm_play_pause_pause
38 * pcm_play_pause_unpause
41 /** These items may be implemented target specifically or need to
42 be shared semi-privately **/
44 /* the registered callback function to ask for more mp3 data */
45 volatile pcm_more_callback_type pcm_callback_for_more
= NULL
;
46 volatile bool pcm_playing
= false;
47 volatile bool pcm_paused
= false;
49 void pcm_play_dma_start(const void *addr
, size_t size
);
50 void pcm_play_dma_stop(void);
51 void pcm_play_pause_pause(void);
52 void pcm_play_pause_unpause(void);
54 /** Functions that require targeted implementation **/
56 #if !defined(CPU_COLDFIRE) && (CONFIG_CPU != S3C2440)
58 #if (CONFIG_CPU == PNX0101)
60 #define DMA_BUF_SAMPLES 0x100
62 short __attribute__((section(".dmabuf"))) dma_buf_left
[DMA_BUF_SAMPLES
];
63 short __attribute__((section(".dmabuf"))) dma_buf_right
[DMA_BUF_SAMPLES
];
65 static int pcm_freq
= HW_SAMPR_DEFAULT
; /* 44.1 is default */
67 unsigned short* p IBSS_ATTR
;
68 size_t p_size IBSS_ATTR
;
70 void pcm_play_dma_start(const void *addr
, size_t size
)
72 p
= (unsigned short*)addr
;
78 void pcm_play_dma_stop(void)
84 void pcm_play_pause_pause(void)
88 void pcm_play_pause_unpause(void)
92 static inline void fill_dma_buf(int offset
)
96 l
= dma_buf_left
+ offset
;
97 lend
= l
+ DMA_BUF_SAMPLES
/ 2;
98 r
= dma_buf_right
+ offset
;
100 if (pcm_playing
&& !pcm_paused
)
105 unsigned short *tmp_p
;
106 count
= MIN(p_size
/ 4, (size_t)(lend
- l
));
118 asm("ldmia %0!, {r0, r1, r2, r3}\n\t"
120 "orr r4, r4, r1, lsl #16\n\t"
122 "orr r5, r5, r3, lsl #16\n\t"
123 "stmia %1!, {r4, r5}\n\t"
125 "orr r4, r4, r0, lsr #16\n\t"
127 "orr r5, r5, r2, lsr #16\n\t"
128 "stmia %2!, {r4, r5}"
129 : "+r" (tmp_p
), "+r" (l
), "+r" (r
)
131 : "r0", "r1", "r2", "r3", "r4", "r5", "memory");
143 else if (pcm_callback_for_more
)
144 pcm_callback_for_more((unsigned char**)&p
,
153 memset(l
, 0, sizeof(short) * (lend
- l
));
154 memset(r
, 0, sizeof(short) * (lend
- l
));
158 static void audio_irq(void)
160 unsigned long st
= DMAINTSTAT
& ~DMAINTEN
;
162 for (i
= 0; i
< 2; i
++)
165 fill_dma_buf((i
== 1) ? 0 : DMA_BUF_SAMPLES
/ 2);
170 unsigned long physical_address(void *p
)
172 unsigned long adr
= (unsigned long)p
;
173 return (MMUBLOCK((adr
>> 21) & 0xf) << 21) | (adr
& ((1 << 21) - 1));
182 pcm_callback_for_more
= NULL
;
184 memset(dma_buf_left
, 0, sizeof(dma_buf_left
));
185 memset(dma_buf_right
, 0, sizeof(dma_buf_right
));
187 for (i
= 0; i
< 8; i
++)
197 DMAINTSTAT
= 0xc000ffff;
198 DMAINTEN
= 0xc000ffff;
200 DMASRC(0) = physical_address(dma_buf_left
);
201 DMADEST(0) = 0x80200280;
206 DMASRC(1) = physical_address(dma_buf_right
);
207 DMADEST(1) = 0x80200284;
212 irq_set_int_handler(0x1b, audio_irq
);
213 irq_enable_int(0x1b);
222 void pcm_postinit(void)
227 void pcm_set_frequency(unsigned int frequency
)
230 pcm_freq
= HW_SAMPR_DEFAULT
;
232 size_t pcm_get_bytes_waiting(void)
236 #endif /* CONFIG_CPU == */
238 /* dummy functions for those not actually supporting all this yet */
239 void pcm_apply_settings(void)
244 void pcm_mute(bool mute
)
246 #if defined(HAVE_WM8975) || defined(HAVE_WM8758) \
247 || defined(HAVE_WM8731) || defined(HAVE_WM8721)
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
)
273 #if CONFIG_CPU == PNX0101
274 size_t samples
= p_size
/ 4;
276 #endif /* CONFIG_CPU */.
278 if (samples
> PEAK_SAMPLES
)
279 samples
= PEAK_SAMPLES
- (PEAK_STRIDE
- 1);
281 samples
-= MIN(PEAK_STRIDE
- 1, samples
);
283 end
= &addr
[samples
* 2];
287 int left_peak
= 0, right_peak
= 0;
291 if ((value
= addr
[0]) > left_peak
)
293 else if (-value
> left_peak
)
296 if ((value
= addr
[PEAK_STRIDE
| 1]) > right_peak
)
298 else if (-value
> right_peak
)
301 addr
= &addr
[PEAK_STRIDE
* 2];
307 else if (left
|| right
) {
308 int peak_value
= 0, value
;
311 addr
+= (PEAK_STRIDE
| 1);
314 if ((value
= addr
[0]) > peak_value
)
316 else if (-value
> peak_value
)
319 addr
+= PEAK_STRIDE
* 2;
328 #endif /* !defined(CPU_PP) */
330 #endif /* !defined(CPU_COLDFIRE) && (CONFIG_CPU != S3C2440) */
332 /****************************************************************************
333 * Functions that do not require targeted implementation but only a targeted
337 /* Common code to pcm_play_data and pcm_play_pause
338 Returns true if DMA playback was started, else false. */
339 bool pcm_play_data_start(pcm_more_callback_type get_more
,
340 unsigned char *start
, size_t size
)
342 if (!(start
&& size
))
346 get_more(&start
, &size
);
351 pcm_play_dma_start(start
, size
);
358 void pcm_play_data(pcm_more_callback_type get_more
,
359 unsigned char *start
, size_t size
)
361 pcm_callback_for_more
= get_more
;
363 if (pcm_play_data_start(get_more
, start
, size
) && pcm_paused
)
366 pcm_play_pause(false);
370 void pcm_play_pause(bool play
)
372 bool needs_change
= pcm_paused
== play
;
374 /* This needs to be done ahead of the rest to prevent infinite
375 recursion from pcm_play_data */
378 if (pcm_playing
&& needs_change
)
382 if (pcm_get_bytes_waiting())
385 pcm_play_pause_unpause();
389 logf("unpause, no data waiting");
390 if (!pcm_play_data_start(pcm_callback_for_more
, NULL
, 0))
393 logf("unpause attempted, no data");
400 pcm_play_pause_pause();
402 } /* pcm_playing && needs_change */
405 void pcm_play_stop(void)
411 bool pcm_is_playing(void)
416 bool pcm_is_paused(void)