the DAC drivers need to include audiohw.h now
[Rockbox.git] / firmware / drivers / audio / wm8731l.c
blob4c4f7cb1e75eba3835c6697da4282f3108b12dc3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Driver for WM8731L audio codec
12 * Based on code from the ipodlinux project - http://ipodlinux.org/
13 * Adapted for Rockbox in January 2006
15 * Original file: linux/arch/armnommu/mach-ipod/audio.c
17 * Copyright (c) 2003-2005 Bernard Leach (leachbj@bouncycastle.org)
19 * All files in this archive are subject to the GNU General Public License.
20 * See the file COPYING in the source tree root for full license agreement.
22 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23 * KIND, either express or implied.
25 ****************************************************************************/
26 #include "config.h"
27 #include "logf.h"
28 #include "system.h"
29 #include "string.h"
30 #include "audio.h"
32 #include "wmcodec.h"
33 #include "audiohw.h"
34 #include "i2s.h"
36 #define IPOD_PCM_LEVEL 0x65 /* -6dB */
38 /* use zero crossing to reduce clicks during volume changes */
39 #define VOLUME_ZC_WAIT (1<<7)
41 const struct sound_settings_info audiohw_settings[] = {
42 [SOUND_VOLUME] = {"dB", 0, 1, -74, 6, -25},
43 /* HAVE_SW_TONE_CONTROLS */
44 [SOUND_BASS] = {"dB", 0, 1, -24, 24, 0},
45 [SOUND_TREBLE] = {"dB", 0, 1, -24, 24, 0},
46 [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
47 [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
48 [SOUND_STEREO_WIDTH] = {"%", 0, 1, 0, 255, 100},
49 #ifdef HAVE_RECORDING
50 [SOUND_LEFT_GAIN] = {"dB", 1, 1,-128, 96, 0},
51 [SOUND_RIGHT_GAIN] = {"dB", 1, 1,-128, 96, 0},
52 [SOUND_MIC_GAIN] = {"dB", 1, 1,-128, 108, 16},
53 #endif
56 /* convert tenth of dB volume (-730..60) to master volume register value */
57 int tenthdb2master(int db)
59 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
60 /* 1111111 == +6dB (0x7f) */
61 /* 1111001 == 0dB (0x79) */
62 /* 0110000 == -73dB (0x30 */
63 /* 0101111 == mute (0x2f) */
65 if (db < VOLUME_MIN) {
66 return 0x2f;
67 } else {
68 return((db/10)+0x30+73);
72 /* convert tenth of dB volume (-780..0) to mixer volume register value */
73 int tenthdb2mixer(int db)
75 if (db < -660) /* 1.5 dB steps */
76 return (2640 - db) / 15;
77 else if (db < -600) /* 0.75 dB steps */
78 return (990 - db) * 2 / 15;
79 else if (db < -460) /* 0.5 dB steps */
80 return (460 - db) / 5;
81 else /* 0.25 dB steps */
82 return -db * 2 / 5;
85 int audiohw_mute(int mute)
87 if (mute)
89 /* Set DACMU = 1 to soft-mute the audio DACs. */
90 wmcodec_write(DACCTRL, 0x8);
91 } else {
92 /* Set DACMU = 0 to soft-un-mute the audio DACs. */
93 wmcodec_write(DACCTRL, 0x0);
96 return 0;
99 /** From ipodLinux **/
100 static void codec_set_active(int active)
102 /* set active to 0x0 or 0x1 */
103 if (active) {
104 wmcodec_write(ACTIVECTRL, 0x01);
105 } else {
106 wmcodec_write(ACTIVECTRL, 0x00);
111 /* Silently enable / disable audio output */
112 void audiohw_enable_output(bool enable)
114 if (enable)
116 /* reset the I2S controller into known state */
117 i2s_reset();
119 wmcodec_write(RESET, 0x0); /*Reset*/
121 codec_set_active(0x0);
123 #ifdef HAVE_WM8721
124 /* DACSEL=1 */
125 wmcodec_write(0x4, 0x10);
126 #elif defined HAVE_WM8731
127 /* DACSEL=1, BYPASS=1 */
128 wmcodec_write(0x4, 0x18);
129 #endif
131 /* set power register to POWEROFF=0 on OUTPD=0, DACPD=0 */
132 wmcodec_write(PWRMGMT, 0x67);
134 /* BCLKINV=0(Dont invert BCLK) MS=1(Enable Master) LRSWAP=0 LRP=0 */
135 /* IWL=00(16 bit) FORMAT=10(I2S format) */
136 wmcodec_write(AINTFCE, 0x42);
138 audiohw_set_sample_rate(WM8731L_44100HZ);
140 /* set the volume to -6dB */
141 wmcodec_write(LOUTVOL, IPOD_PCM_LEVEL);
142 wmcodec_write(ROUTVOL, 0x100 | IPOD_PCM_LEVEL);
144 /* ACTIVE=1 */
145 codec_set_active(1);
147 /* 5. Set DACMU = 0 to soft-un-mute the audio DACs. */
148 wmcodec_write(DACCTRL, 0x0);
150 #if defined(IRIVER_H10) || defined(IRIVER_H10_5GB)
151 /* We need to enable bit 4 of GPIOL for output for sound on H10 */
152 GPIOL_OUTPUT_VAL |= 0x10;
153 #endif
154 audiohw_mute(0);
155 } else {
156 #if defined(IRIVER_H10) || defined(IRIVER_H10_5GB)
157 /* We need to disable bit 4 of GPIOL to disable sound on H10 */
158 GPIOL_OUTPUT_VAL &= ~0x10;
159 #endif
160 audiohw_mute(1);
164 int audiohw_set_master_vol(int vol_l, int vol_r)
166 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
167 /* 1111111 == +6dB */
168 /* 1111001 == 0dB */
169 /* 0110000 == -73dB */
170 /* 0101111 == mute (0x2f) */
172 wmcodec_write(LOUTVOL, VOLUME_ZC_WAIT | vol_l);
173 wmcodec_write(ROUTVOL, VOLUME_ZC_WAIT | vol_r);
175 return 0;
178 int audiohw_set_mixer_vol(int channel1, int channel2)
180 (void)channel1;
181 (void)channel2;
183 return 0;
186 void audiohw_set_bass(int value)
188 (void)value;
191 void audiohw_set_treble(int value)
193 (void)value;
196 /* Nice shutdown of WM8731 codec */
197 void audiohw_close(void)
199 /* set DACMU=1 DEEMPH=0 */
200 wmcodec_write(DACCTRL, 0x8);
202 /* ACTIVE=0 */
203 codec_set_active(0x0);
205 /* line in mute left & right*/
206 wmcodec_write(LINVOL, 0x100 | 0x80);
208 /* set DACSEL=0, MUTEMIC=1 */
209 wmcodec_write(0x4, 0x2);
211 /* set POWEROFF=0 OUTPD=0 DACPD=1 */
212 wmcodec_write(PWRMGMT, 0x6f);
214 /* set POWEROFF=1 OUTPD=1 DACPD=1 */
215 wmcodec_write(PWRMGMT, 0xff);
218 /* Change the order of the noise shaper, 5th order is recommended above 32kHz */
219 void audiohw_set_nsorder(int order)
221 (void)order;
224 void audiohw_set_sample_rate(int sampling_control)
226 codec_set_active(0x0);
227 wmcodec_write(SAMPCTRL, sampling_control);
228 codec_set_active(0x1);
231 void audiohw_enable_recording(bool source_mic)
233 static int line_level = 0x17;
234 static int mic_boost = true;
235 codec_set_active(0x0);
237 /* set BCLKINV=0(Dont invert BCLK) MS=1(Enable Master) LRSWAP=0
238 * LRP=0 IWL=00(16 bit) FORMAT=10(I2S format) */
239 wmcodec_write(AINTFCE, 0x42);
241 wmcodec_write(LOUTVOL, 0x0); /* headphone mute left */
242 wmcodec_write(ROUTVOL, 0x0); /* headphone mute right */
245 if(source_mic){
246 wmcodec_write(LINVOL, 0x80); /* line in mute left */
247 wmcodec_write(RINVOL, 0x80); /* line in mute right */
250 if (mic_boost) {
251 wmcodec_write(AAPCTRL, 0x5); /* INSEL=mic, MIC_BOOST=enable */
252 } else {
253 wmcodec_write(AAPCTRL, 0x4); /* INSEL=mic */
255 } else {
256 if (line_level == 0) {
257 wmcodec_write(LINVOL, 0x80);
258 wmcodec_write(RINVOL, 0x80);
259 } else {
260 wmcodec_write(LINVOL, line_level);
261 wmcodec_write(RINVOL, line_level);
263 wmcodec_write(AAPCTRL, 0xa); /* BY PASS, mute mic, INSEL=line in */
266 /* disable ADC high pass filter, mute dac */
267 wmcodec_write(DACCTRL, 0x9);
269 /* power on (PWR_OFF=0) */
270 if(source_mic){
271 /* CLKOUTPD OSCPD OUTPD DACPD LINEINPD */
272 wmcodec_write(PWRMGMT, 0x79);
273 } else {
274 wmcodec_write(PWRMGMT, 0x7a); /* MICPD */
277 codec_set_active(0x1);
280 void audiohw_disable_recording(void)
282 /* set DACMU=1 DEEMPH=0 */
283 wmcodec_write(DACCTRL, 0x8);
285 /* ACTIVE=0 */
286 codec_set_active(0x0);
288 /* line in mute left & right*/
289 wmcodec_write(LINVOL, 0x80);
290 wmcodec_write(RINVOL, 0x80);
292 /* set DACSEL=0, MUTEMIC=1 */
293 wmcodec_write(AAPCTRL, 0x2);
295 /* set POWEROFF=0 OUTPD=0 DACPD=1 */
296 wmcodec_write(PWRMGMT, 0x6f);
298 /* set POWEROFF=1 OUTPD=1 DACPD=1 */
299 wmcodec_write(PWRMGMT, 0xff);
302 void audiohw_set_recvol(int left, int right, int type)
304 (void)left;
305 (void)right;
306 (void)type;
309 void audiohw_set_monitor(int enable)
311 (void)enable;