convert to unix line endings.
[Rockbox.git] / firmware / drivers / audio / wm8751.c
blob831f16ab1799365025cf5c4d47d772b93b1498e9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Driver for WM8751 audio codec
12 * Based on code from the ipodlinux project - http://ipodlinux.org/
13 * Adapted for Rockbox in December 2005
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 "kernel.h"
27 #include "wmcodec.h"
28 #include "i2s.h"
29 #include "audio.h"
30 #include "audiohw.h"
31 #include "system.h"
33 const struct sound_settings_info audiohw_settings[] = {
34 [SOUND_VOLUME] = {"dB", 0, 1, -74, 6, -25},
35 #ifdef USE_ADAPTIVE_BASS
36 [SOUND_BASS] = {"", 0, 1, 0, 15, 0},
37 #else
38 [SOUND_BASS] = {"dB", 1, 15, -60, 90, 0},
39 #endif
40 [SOUND_TREBLE] = {"dB", 1, 15, -60, 90, 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},
46 /* Flags used in combination with settings */
48 /* use zero crossing to reduce clicks during volume changes */
49 #define LOUT1_BITS (LOUT1_LO1ZC)
50 /* latch left volume first then update left+right together */
51 #define ROUT1_BITS (ROUT1_RO1ZC | ROUT1_RO1VU)
52 #define LOUT2_BITS (LOUT2_LO2ZC)
53 #define ROUT2_BITS (ROUT2_RO2ZC | ROUT2_RO2VU)
54 /* We use linear bass control with 200 Hz cutoff */
55 #ifdef USE_ADAPTIVE_BASE
56 #define BASSCTRL_BITS (BASSCTRL_BC | BASSCTRL_BB)
57 #else
58 #define BASSCTRL_BITS (BASSCTRL_BC)
59 #endif
60 /* We use linear treble control with 4 kHz cutoff */
61 #define TREBCTRL_BITS (TREBCTRL_TC)
63 /* convert tenth of dB volume (-730..60) to master volume register value */
64 int tenthdb2master(int db)
66 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
67 /* 1111111 == +6dB (0x7f) */
68 /* 1111001 == 0dB (0x79) */
69 /* 0110000 == -73dB (0x30) */
70 /* 0101111..0000000 == mute (<= 0x2f) */
71 if (db < VOLUME_MIN)
72 return 0x0;
73 else
74 return (db / 10) + 73 + 0x30;
77 static int tone_tenthdb2hw(int value)
79 /* -6.0db..+0db..+9.0db step 1.5db - translate -60..+0..+90 step 15
80 to 10..6..0 step -1.
82 value = 10 - (value + 60) / 15;
84 if (value == 6)
85 value = 0xf; /* 0db -> off */
87 return value;
91 #ifdef USE_ADAPTIVE_BASS
92 static int adaptivebass2hw(int value)
94 /* 0 to 15 step 1 - step -1 0 = off is a 15 in the register */
95 value = 15 - value;
97 return value;
99 #endif
101 /* Reset and power up the WM8751 */
102 void audiohw_preinit(void)
104 #ifdef MROBE_100
105 /* controls headphone ouput */
106 GPIOL_ENABLE |= 0x10;
107 GPIOL_OUTPUT_EN |= 0x10;
108 GPIOL_OUTPUT_VAL |= 0x10; /* disable */
109 #endif
111 #ifdef CPU_PP502x
112 i2s_reset();
113 #endif
116 * 1. Switch on power supplies.
117 * By default the WM8751 is in Standby Mode, the DAC is
118 * digitally muted and the Audio Interface, Line outputs
119 * and Headphone outputs are all OFF (DACMU = 1 Power
120 * Management registers 1 and 2 are all zeros).
122 wmcodec_write(RESET, RESET_RESET); /*Reset*/
124 /* 2. Enable Vmid and VREF. */
125 wmcodec_write(PWRMGMT1, PWRMGMT1_VREF | PWRMGMT1_VMIDSEL_5K);
127 /* BCLKINV=0(Dont invert BCLK) MS=1(Enable Master) LRSWAP=0 LRP=0 */
128 /* IWL=00(16 bit) FORMAT=10(I2S format) */
129 wmcodec_write(AINTFCE, AINTFCE_MS | AINTFCE_WL_16 |
130 AINTFCE_FORMAT_I2S);
133 /* Enable DACs and audio output after a short delay */
134 void audiohw_postinit(void)
136 /* From app notes: allow Vref to stabilize to reduce clicks */
137 sleep(HZ);
139 /* 3. Enable DACs as required. */
140 wmcodec_write(PWRMGMT2, PWRMGMT2_DACL | PWRMGMT2_DACR);
142 /* 4. Enable line and / or headphone output buffers as required. */
143 #ifdef MROBE_100
144 wmcodec_write(PWRMGMT2, PWRMGMT2_DACL | PWRMGMT2_DACR |
145 PWRMGMT2_LOUT1 | PWRMGMT2_ROUT1);
146 #else
147 wmcodec_write(PWRMGMT2, PWRMGMT2_DACL | PWRMGMT2_DACR |
148 PWRMGMT2_LOUT1 | PWRMGMT2_ROUT1 | PWRMGMT2_LOUT2 |
149 PWRMGMT2_ROUT2);
150 #endif
152 wmcodec_write(ADDITIONAL1, ADDITIONAL1_TSDEN | ADDITIONAL1_TOEN |
153 ADDITIONAL1_DMONOMIX_LLRR | ADDITIONAL1_VSEL_DEFAULT);
155 wmcodec_write(LEFTMIX1, LEFTMIX1_LD2LO | LEFTMIX1_LI2LO_DEFAULT);
156 wmcodec_write(RIGHTMIX2, RIGHTMIX2_RD2RO | RIGHTMIX2_RI2RO_DEFAULT);
158 audiohw_mute(false);
160 #ifdef MROBE_100
161 /* enable headphone output */
162 GPIOL_OUTPUT_VAL &= ~0x10;
163 GPIOL_OUTPUT_EN |= 0x10;
164 #endif
167 void audiohw_set_master_vol(int vol_l, int vol_r)
169 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
170 /* 1111111 == +6dB */
171 /* 1111001 == 0dB */
172 /* 0110000 == -73dB */
173 /* 0101111 == mute (0x2f) */
175 wmcodec_write(LOUT1, LOUT1_BITS | LOUT1_LOUT1VOL(vol_l));
176 wmcodec_write(ROUT1, ROUT1_BITS | ROUT1_ROUT1VOL(vol_r));
179 #ifndef MROBE_100
180 void audiohw_set_lineout_vol(int vol_l, int vol_r)
182 wmcodec_write(LOUT2, LOUT2_BITS | LOUT2_LOUT2VOL(vol_l));
183 wmcodec_write(ROUT2, ROUT2_BITS | ROUT2_ROUT2VOL(vol_r));
185 #endif
187 void audiohw_set_bass(int value)
189 wmcodec_write(BASSCTRL, BASSCTRL_BITS |
191 #ifdef USE_ADAPTIVE_BASS
192 BASSCTRL_BASS(adaptivebass2hw(value)));
193 #else
194 BASSCTRL_BASS(tone_tenthdb2hw(value)));
195 #endif
198 void audiohw_set_treble(int value)
200 wmcodec_write(TREBCTRL, TREBCTRL_BITS |
201 TREBCTRL_TREB(tone_tenthdb2hw(value)));
204 void audiohw_mute(bool mute)
206 /* Mute: Set DACMU = 1 to soft-mute the audio DACs. */
207 /* Unmute: Set DACMU = 0 to soft-un-mute the audio DACs. */
208 wmcodec_write(DACCTRL, mute ? DACCTRL_DACMU : 0);
211 /* Nice shutdown of WM8751 codec */
212 void audiohw_close(void)
214 /* 1. Set DACMU = 1 to soft-mute the audio DACs. */
215 audiohw_mute(true);
217 /* 2. Disable all output buffers. */
218 wmcodec_write(PWRMGMT2, 0x0);
220 /* 3. Switch off the power supplies. */
221 wmcodec_write(PWRMGMT1, 0x0);
224 /* Note: Disable output before calling this function */
225 void audiohw_set_frequency(int fsel)
227 wmcodec_write(CLOCKING, fsel);