Revert previous commit (still learning to work with git...)
[Rockbox.git] / firmware / drivers / audio / wm8731.c
blobe0e7cb38c030af511789108030364ccb4a1d6a24
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 const struct sound_settings_info audiohw_settings[] = {
37 [SOUND_VOLUME] = {"dB", 0, 1, -74, 6, -25},
38 /* HAVE_SW_TONE_CONTROLS */
39 [SOUND_BASS] = {"dB", 0, 1, -24, 24, 0},
40 [SOUND_TREBLE] = {"dB", 0, 1, -24, 24, 0},
41 [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
42 [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
43 [SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100},
44 #ifdef HAVE_RECORDING
45 [SOUND_LEFT_GAIN] = {"dB", 1, 1, 0, 31, 23},
46 [SOUND_RIGHT_GAIN] = {"dB", 1, 1, 0, 31, 23},
47 [SOUND_MIC_GAIN] = {"dB", 1, 1, 0, 1, 0},
48 #endif
51 /* Init values/shadows
52 * Ignore bit 8 since that only specifies "both" for updating
53 * gains */
54 static unsigned char wm8731_regs[7] =
56 [LINVOL] = LINVOL_LINMUTE,
57 [RINVOL] = RINVOL_RINMUTE,
58 [LOUTVOL] = ROUTVOL_RZCEN,
59 [ROUTVOL] = ROUTVOL_RZCEN,
60 [AAPCTRL] = AAPCTRL_MUTEMIC | AAPCTRL_DACSEL,
61 [DAPCTRL] = DAPCTRL_DACMU | DAPCTRL_DEEMP_44KHz | DAPCTRL_ADCHPD,
62 [PDCTRL] = PDCTRL_LINEINPD | PDCTRL_MICPD | PDCTRL_ADCPD |
63 PDCTRL_OUTPD | PDCTRL_OSCPD | PDCTRL_CLKOUTPD,
66 static void wm8731_write(int reg, unsigned val)
68 wm8731_regs[reg] = (unsigned char)val;
69 wmcodec_write(reg, val);
72 static void wm8731_write_and(int reg, unsigned bits)
74 wm8731_write(reg, wm8731_regs[reg] & bits);
77 static void wm8731_write_or(int reg, unsigned bits)
79 wm8731_write(reg, wm8731_regs[reg] | bits);
82 /* convert tenth of dB volume (-730..60) to master volume register value */
83 int tenthdb2master(int db)
85 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
86 /* 1111111 == +6dB (0x7f) */
87 /* 1111001 == 0dB (0x79) */
88 /* 0110000 == -73dB (0x30 */
89 /* 0101111 == mute (0x2f) */
91 if (db < VOLUME_MIN) {
92 return 0x2f;
93 } else {
94 return((db/10)+0x30+73);
98 /* convert tenth of dB volume (-780..0) to mixer volume register value */
99 int tenthdb2mixer(int db)
101 if (db < -660) /* 1.5 dB steps */
102 return (2640 - db) / 15;
103 else if (db < -600) /* 0.75 dB steps */
104 return (990 - db) * 2 / 15;
105 else if (db < -460) /* 0.5 dB steps */
106 return (460 - db) / 5;
107 else /* 0.25 dB steps */
108 return -db * 2 / 5;
111 int sound_val2phys(int setting, int value)
113 int result;
115 switch(setting)
117 case SOUND_LEFT_GAIN:
118 case SOUND_RIGHT_GAIN:
119 result = (value - 23) * 15;
120 break;
121 case SOUND_MIC_GAIN:
122 result = value * 200;
123 break;
124 default:
125 result = value;
126 break;
129 return result;
132 void audiohw_mute(bool mute)
134 if (mute) {
135 /* Set DACMU = 1 to soft-mute the audio DACs. */
136 wm8731_write_or(DAPCTRL, DAPCTRL_DACMU);
137 } else {
138 /* Set DACMU = 0 to soft-un-mute the audio DACs. */
139 wm8731_write_and(DAPCTRL, ~DAPCTRL_DACMU);
143 static void codec_set_active(int active)
145 /* set active to 0x0 or 0x1 */
146 wmcodec_write(ACTIVECTRL, active ? ACTIVECTRL_ACTIVE : 0);
149 void audiohw_preinit(void)
151 i2s_reset();
153 /* POWER UP SEQUENCE */
154 /* 1) Switch on power supplies. By default the WM8731 is in Standby Mode,
155 * the DAC is digitally muted and the Audio Interface and Outputs are
156 * all OFF. */
157 wmcodec_write(RESET, RESET_RESET);
159 /* 2) Set all required bits in the Power Down register (0Ch) to '0';
160 * EXCEPT the OUTPD bit, this should be set to '1' (Default). */
161 wm8731_write(PDCTRL, wm8731_regs[PDCTRL]);
163 /* 3) Set required values in all other registers except 12h (Active). */
164 wmcodec_write(AINTFCE, AINTFCE_FORMAT_I2S | AINTFCE_IWL_16BIT |
165 AINTFCE_MS);
166 wm8731_write(AAPCTRL, wm8731_regs[AAPCTRL]);
167 wm8731_write(DAPCTRL, wm8731_regs[DAPCTRL]);
168 wmcodec_write(SAMPCTRL, WM8731_USB24_44100HZ);
170 /* 5) The last write of the sequence should be setting OUTPD to '0'
171 * (active) in register 0Ch, enabling the DAC signal path, free
172 * of any significant power-up noise. */
173 wm8731_write_and(PDCTRL, ~PDCTRL_OUTPD);
176 void audiohw_postinit(void)
178 sleep(HZ);
180 /* 4) Set the 'Active' bit in register 12h. */
181 codec_set_active(true);
183 audiohw_mute(false);
185 #if defined(IRIVER_H10) || defined(IRIVER_H10_5GB)
186 /* We need to enable bit 4 of GPIOL for output for sound on H10 */
187 GPIOL_OUTPUT_VAL |= 0x10;
188 #endif
191 int audiohw_set_master_vol(int vol_l, int vol_r)
193 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
194 /* 1111111 == +6dB */
195 /* 1111001 == 0dB */
196 /* 0110000 == -73dB */
197 /* 0101111 == mute (0x2f) */
198 wm8731_write(LOUTVOL, LOUTVOL_LZCEN | (vol_l & LOUTVOL_LHPVOL_MASK));
199 wm8731_write(ROUTVOL, ROUTVOL_RZCEN | (vol_r & ROUTVOL_RHPVOL_MASK));
200 return 0;
203 /* Nice shutdown of WM8731 codec */
204 void audiohw_close(void)
206 /* POWER DOWN SEQUENCE */
207 /* 1) Set the OUTPD bit to '1' (power down). */
208 wm8731_write_or(PDCTRL, PDCTRL_OUTPD);
209 /* 2) Remove the WM8731 supplies. */
212 void audiohw_set_nsorder(int order)
214 static const unsigned char deemp[4] =
216 DAPCTRL_DEEMP_DISABLE,
217 DAPCTRL_DEEMP_32KHz,
218 DAPCTRL_DEEMP_44KHz,
219 DAPCTRL_DEEMP_48KHz
222 if ((unsigned)order >= ARRAYLEN(deemp))
223 order = 0;
225 wm8731_write(DAPCTRL,
226 (wm8731_regs[DAPCTRL] & ~DAPCTRL_DEEMP_MASK) | deemp[order]);
229 void audiohw_set_sample_rate(int sampling_control)
231 codec_set_active(false);
232 wmcodec_write(SAMPCTRL, sampling_control);
233 codec_set_active(true);
236 void audiohw_enable_recording(bool source_mic)
238 codec_set_active(false);
240 wm8731_regs[PDCTRL] &= ~PDCTRL_ADCPD;
241 /* NOTE: When switching to digital monitoring we will not want
242 * the DAC disabled. */
243 wm8731_regs[PDCTRL] |= PDCTRL_DACPD;
244 wm8731_regs[AAPCTRL] &= ~AAPCTRL_DACSEL;
246 if (source_mic) {
247 wm8731_write_or(LINVOL, LINVOL_LINMUTE);
248 wm8731_write_or(RINVOL, RINVOL_RINMUTE);
249 wm8731_regs[PDCTRL] &= ~PDCTRL_MICPD;
250 wm8731_regs[PDCTRL] |= PDCTRL_LINEINPD;
251 wm8731_regs[AAPCTRL] |= AAPCTRL_INSEL | AAPCTRL_SIDETONE;
252 wm8731_regs[AAPCTRL] &= ~(AAPCTRL_MUTEMIC | AAPCTRL_BYPASS);
253 } else {
254 wm8731_regs[PDCTRL] |= PDCTRL_MICPD;
255 wm8731_regs[PDCTRL] &= ~PDCTRL_LINEINPD;
256 wm8731_regs[AAPCTRL] |= AAPCTRL_MUTEMIC | AAPCTRL_BYPASS;
257 wm8731_regs[AAPCTRL] &= ~(AAPCTRL_INSEL | AAPCTRL_SIDETONE);
260 wm8731_write(PDCTRL, wm8731_regs[PDCTRL]);
261 wm8731_write(AAPCTRL, wm8731_regs[AAPCTRL]);
263 if (!source_mic) {
264 wm8731_write_and(LINVOL, ~LINVOL_LINMUTE);
265 wm8731_write_and(RINVOL, ~RINVOL_RINMUTE);
268 codec_set_active(true);
271 void audiohw_disable_recording(void)
273 codec_set_active(false);
275 /* Mute inputs */
276 wm8731_write_or(LINVOL, LINVOL_LINMUTE);
277 wm8731_write_or(RINVOL, RINVOL_RINMUTE);
278 wm8731_write_or(AAPCTRL, AAPCTRL_MUTEMIC);
280 /* Turn off input analog audio paths */
281 wm8731_regs[AAPCTRL] &= ~(AAPCTRL_BYPASS | AAPCTRL_SIDETONE);
282 wm8731_write(AAPCTRL, wm8731_regs[AAPCTRL]);
284 /* Set power config */
285 wm8731_regs[PDCTRL] &= ~PDCTRL_DACPD;
286 wm8731_regs[PDCTRL] |= PDCTRL_MICPD | PDCTRL_LINEINPD |
287 PDCTRL_ADCPD;
288 wm8731_write(PDCTRL, wm8731_regs[PDCTRL]);
290 /* Select DAC */
291 wm8731_write_or(AAPCTRL, AAPCTRL_DACSEL);
293 codec_set_active(true);
296 void audiohw_set_recvol(int left, int right, int type)
298 switch (type)
300 case AUDIO_GAIN_MIC:
301 if (left > 0) {
302 wm8731_write_or(AAPCTRL, AAPCTRL_MIC_BOOST);
304 else {
305 wm8731_write_and(AAPCTRL, ~AAPCTRL_MIC_BOOST);
307 break;
308 case AUDIO_GAIN_LINEIN:
309 wm8731_regs[LINVOL] &= ~LINVOL_MASK;
310 wm8731_write(LINVOL, wm8731_regs[LINVOL] | (left & LINVOL_MASK));
311 wm8731_regs[RINVOL] &= ~RINVOL_MASK;
312 wm8731_write(RINVOL, wm8731_regs[RINVOL] | (right & RINVOL_MASK));
313 break;
314 default:
315 return;
319 void audiohw_set_monitor(bool enable)
321 if(enable)
323 wm8731_write_and(PDCTRL, ~PDCTRL_LINEINPD);
324 wm8731_write_or(AAPCTRL, AAPCTRL_BYPASS);
326 else {
327 wm8731_write_and(AAPCTRL, ~AAPCTRL_BYPASS);
328 wm8731_write_or(PDCTRL, PDCTRL_LINEINPD);