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
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)
83 void pcm_play_pause_pause(void)
87 void pcm_play_pause_unpause(void)
91 static inline void fill_dma_buf(int offset
)
95 l
= dma_buf_left
+ offset
;
96 lend
= l
+ DMA_BUF_SAMPLES
/ 2;
97 r
= dma_buf_right
+ offset
;
99 if (pcm_playing
&& !pcm_paused
)
104 unsigned short *tmp_p
;
105 count
= MIN(p_size
/ 4, (size_t)(lend
- l
));
117 asm("ldmia %0!, {r0, r1, r2, r3}\n\t"
119 "orr r4, r4, r1, lsl #16\n\t"
121 "orr r5, r5, r3, lsl #16\n\t"
122 "stmia %1!, {r4, r5}\n\t"
124 "orr r4, r4, r0, lsr #16\n\t"
126 "orr r5, r5, r2, lsr #16\n\t"
127 "stmia %2!, {r4, r5}"
128 : "+r" (tmp_p
), "+r" (l
), "+r" (r
)
130 : "r0", "r1", "r2", "r3", "r4", "r5", "memory");
142 else if (pcm_callback_for_more
)
143 pcm_callback_for_more((unsigned char**)&p
,
152 memset(l
, 0, sizeof(short) * (lend
- l
));
153 memset(r
, 0, sizeof(short) * (lend
- l
));
157 static void audio_irq(void)
159 unsigned long st
= DMAINTSTAT
& ~DMAINTEN
;
161 for (i
= 0; i
< 2; i
++)
164 fill_dma_buf((i
== 1) ? 0 : DMA_BUF_SAMPLES
/ 2);
169 unsigned long physical_address(void *p
)
171 unsigned long adr
= (unsigned long)p
;
172 return (MMUBLOCK((adr
>> 21) & 0xf) << 21) | (adr
& ((1 << 21) - 1));
181 pcm_callback_for_more
= NULL
;
183 memset(dma_buf_left
, 0, sizeof(dma_buf_left
));
184 memset(dma_buf_right
, 0, sizeof(dma_buf_right
));
186 for (i
= 0; i
< 8; i
++)
196 DMAINTSTAT
= 0xc000ffff;
197 DMAINTEN
= 0xc000ffff;
199 DMASRC(0) = physical_address(dma_buf_left
);
200 DMADEST(0) = 0x80200280;
205 DMASRC(1) = physical_address(dma_buf_right
);
206 DMADEST(1) = 0x80200284;
211 irq_set_int_handler(0x1b, audio_irq
);
212 irq_enable_int(0x1b);
221 void pcm_postinit(void)
226 void pcm_set_frequency(unsigned int frequency
)
229 pcm_freq
= HW_SAMPR_DEFAULT
;
231 size_t pcm_get_bytes_waiting(void)
235 #endif /* CONFIG_CPU == */
237 /* dummy functions for those not actually supporting all this yet */
238 void pcm_apply_settings(void)
243 void pcm_mute(bool mute
)
245 #if defined(HAVE_WM8975) || defined(HAVE_WM8758) \
246 || defined(HAVE_WM8731) || defined(HAVE_WM8721)
254 * This function goes directly into the DMA buffer to calculate the left and
255 * right peak values. To avoid missing peaks it tries to look forward two full
256 * peek periods (2/HZ sec, 100% overlap), although it's always possible that
257 * the entire period will not be visible. To reduce CPU load it only looks at
258 * every third sample, and this can be reduced even further if needed (even
259 * every tenth sample would still be pretty accurate).
262 /* Check for a peak every PEAK_STRIDE samples */
263 #define PEAK_STRIDE 3
264 /* Up to 1/50th of a second of audio for peak calculation */
265 /* This should use NATIVE_FREQUENCY, or eventually an adjustable freq. value */
266 #define PEAK_SAMPLES (44100/50)
267 void pcm_calculate_peaks(int *left
, int *right
)
269 #if (CONFIG_CPU == S3C2440)
276 #if CONFIG_CPU == PNX0101
277 size_t samples
= p_size
/ 4;
281 if (samples
> PEAK_SAMPLES
)
282 samples
= PEAK_SAMPLES
- (PEAK_STRIDE
- 1);
284 samples
-= MIN(PEAK_STRIDE
- 1, samples
);
286 end
= &addr
[samples
* 2];
290 int left_peak
= 0, right_peak
= 0;
294 if ((value
= addr
[0]) > left_peak
)
296 else if (-value
> left_peak
)
299 if ((value
= addr
[PEAK_STRIDE
| 1]) > right_peak
)
301 else if (-value
> right_peak
)
304 addr
= &addr
[PEAK_STRIDE
* 2];
310 else if (left
|| right
) {
311 int peak_value
= 0, value
;
314 addr
+= (PEAK_STRIDE
| 1);
317 if ((value
= addr
[0]) > peak_value
)
319 else if (-value
> peak_value
)
322 addr
+= PEAK_STRIDE
* 2;
333 #endif /* CPU_COLDFIRE */
335 /****************************************************************************
336 * Functions that do not require targeted implementation but only a targeted
340 /* Common code to pcm_play_data and pcm_play_pause
341 Returns true if DMA playback was started, else false. */
342 bool pcm_play_data_start(pcm_more_callback_type get_more
,
343 unsigned char *start
, size_t size
)
345 if (!(start
&& size
))
349 get_more(&start
, &size
);
354 pcm_play_dma_start(start
, size
);
361 void pcm_play_data(pcm_more_callback_type get_more
,
362 unsigned char *start
, size_t size
)
364 pcm_callback_for_more
= get_more
;
366 if (pcm_play_data_start(get_more
, start
, size
) && pcm_paused
)
369 pcm_play_pause(false);
373 void pcm_play_pause(bool play
)
375 bool needs_change
= pcm_paused
== play
;
377 /* This needs to be done ahead of the rest to prevent infinite
378 recursion from pcm_play_data */
381 if (pcm_playing
&& needs_change
)
385 if (pcm_get_bytes_waiting())
388 pcm_play_pause_unpause();
392 logf("unpause, no data waiting");
393 if (!pcm_play_data_start(pcm_callback_for_more
, NULL
, 0))
396 logf("unpause attempted, no data");
403 pcm_play_pause_pause();
405 } /* pcm_playing && needs_change */
408 void pcm_play_stop(void)
414 bool pcm_is_playing(void)
419 bool pcm_is_paused(void)