Revert previous commit (still learning to work with git...)
[Rockbox.git] / firmware / drivers / audio / wm8758.c
bloba9f10fbe67fb9ad917669351bc60b9b827e1a89c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Driver for WM8758 audio codec - based on datasheet for WM8983
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 "system.h"
27 #include "string.h"
28 #include "audio.h"
30 #include "wmcodec.h"
31 #include "audiohw.h"
32 #include "i2s.h"
34 const struct sound_settings_info audiohw_settings[] = {
35 [SOUND_VOLUME] = {"dB", 0, 1, -58, 6, -25},
36 [SOUND_BASS] = {"dB", 0, 1, -12, 12, 0},
37 [SOUND_TREBLE] = {"dB", 0, 1, -12, 12, 0},
38 [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
39 [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
40 [SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100},
41 [SOUND_LEFT_GAIN] = {"dB", 1, 1,-128, 96, 0},
42 [SOUND_RIGHT_GAIN] = {"dB", 1, 1,-128, 96, 0},
43 [SOUND_MIC_GAIN] = {"dB", 1, 1,-128, 108, 16},
44 [SOUND_BASS_CUTOFF] = {"", 0, 1, 1, 4, 1},
45 [SOUND_TREBLE_CUTOFF] = {"", 0, 1, 1, 4, 1},
48 /* shadow registers */
49 unsigned int eq1_reg;
50 unsigned int eq5_reg;
52 /* convert tenth of dB volume (-57..6) to master volume register value */
53 int tenthdb2master(int db)
55 /* +6 to -57dB in 1dB steps == 64 levels = 6 bits */
56 /* 0111111 == +6dB (0x3f) = 63) */
57 /* 0111001 == 0dB (0x39) = 57) */
58 /* 0000001 == -56dB (0x01) = */
59 /* 0000000 == -57dB (0x00) */
61 /* 1000000 == Mute (0x40) */
63 if (db < VOLUME_MIN) {
64 return 0x40;
65 } else {
66 return((db/10)+57);
70 /* convert tenth of dB volume (-780..0) to mixer volume register value */
71 int tenthdb2mixer(int db)
73 if (db < -660) /* 1.5 dB steps */
74 return (2640 - db) / 15;
75 else if (db < -600) /* 0.75 dB steps */
76 return (990 - db) * 2 / 15;
77 else if (db < -460) /* 0.5 dB steps */
78 return (460 - db) / 5;
79 else /* 0.25 dB steps */
80 return -db * 2 / 5;
83 #define IPOD_PCM_LEVEL 0x65 /* -6dB */
85 //#define BASSCTRL 0x
86 //#define TREBCTRL 0x0b
88 /* Silently enable / disable audio output */
89 void audiohw_enable_output(bool enable)
91 if (enable)
93 /* reset the I2S controller into known state */
94 i2s_reset();
96 /* TODO: Review the power-up sequence to prevent pops */
98 wmcodec_write(RESET, 0x1ff); /*Reset*/
100 wmcodec_write(PWRMGMT1, 0x2b);
101 wmcodec_write(PWRMGMT2, 0x180);
102 wmcodec_write(PWRMGMT3, 0x6f);
104 wmcodec_write(AINTFCE, 0x10);
105 wmcodec_write(CLKCTRL, 0x49);
107 wmcodec_write(OUTCTRL, 1);
109 /* The iPod can handle multiple frequencies, but fix at 44.1KHz
110 for now */
111 audiohw_set_sample_rate(WM8758_44100HZ);
113 wmcodec_write(LOUTMIX,0x1); /* Enable mixer */
114 wmcodec_write(ROUTMIX,0x1); /* Enable mixer */
115 audiohw_mute(0);
116 } else {
117 audiohw_mute(1);
121 int audiohw_set_master_vol(int vol_l, int vol_r)
123 /* OUT1 */
124 wmcodec_write(LOUT1VOL, 0x080 | vol_l);
125 wmcodec_write(ROUT1VOL, 0x180 | vol_r);
127 return 0;
130 int audiohw_set_lineout_vol(int vol_l, int vol_r)
132 /* OUT2 */
133 wmcodec_write(LOUT2VOL, vol_l);
134 wmcodec_write(ROUT2VOL, 0x100 | vol_r);
136 return 0;
139 int audiohw_set_mixer_vol(int channel1, int channel2)
141 (void)channel1;
142 (void)channel2;
144 return 0;
147 void audiohw_set_bass(int value)
149 eq1_reg = (eq1_reg & ~EQ_GAIN_MASK) | EQ_GAIN_VALUE(value);
150 wmcodec_write(EQ1, 0x100 | eq1_reg);
153 void audiohw_set_bass_cutoff(int value)
155 eq1_reg = (eq1_reg & ~EQ_CUTOFF_MASK) | EQ_CUTOFF_VALUE(value);
156 wmcodec_write(EQ1, 0x100 | eq1_reg);
159 void audiohw_set_treble(int value)
161 eq5_reg = (eq5_reg & ~EQ_GAIN_MASK) | EQ_GAIN_VALUE(value);
162 wmcodec_write(EQ5, eq5_reg);
165 void audiohw_set_treble_cutoff(int value)
167 eq5_reg = (eq5_reg & ~EQ_CUTOFF_MASK) | EQ_CUTOFF_VALUE(value);
168 wmcodec_write(EQ5, eq5_reg);
171 void audiohw_mute(bool mute)
173 if (mute)
175 /* Set DACMU = 1 to soft-mute the audio DACs. */
176 wmcodec_write(DACCTRL, 0x40);
177 } else {
178 /* Set DACMU = 0 to soft-un-mute the audio DACs. */
179 wmcodec_write(DACCTRL, 0x0);
183 /* Nice shutdown of WM8758 codec */
184 void audiohw_close(void)
186 audiohw_mute(1);
188 wmcodec_write(PWRMGMT3, 0x0);
190 wmcodec_write(PWRMGMT1, 0x0);
192 wmcodec_write(PWRMGMT2, 0x40);
195 /* Change the order of the noise shaper, 5th order is recommended above 32kHz */
196 void audiohw_set_nsorder(int order)
198 (void)order;
201 /* Note: Disable output before calling this function */
202 void audiohw_set_sample_rate(int sampling_control)
204 /**** We force 44.1KHz for now. ****/
205 (void)sampling_control;
207 /* set clock div */
208 wmcodec_write(CLKCTRL, 1 | (0 << 2) | (2 << 5));
210 /* setup PLL for MHZ=11.2896 */
211 wmcodec_write(PLLN, (1 << 4) | 0x7);
212 wmcodec_write(PLLK1, 0x21);
213 wmcodec_write(PLLK2, 0x161);
214 wmcodec_write(PLLK3, 0x26);
216 /* set clock div */
217 wmcodec_write(CLKCTRL, 1 | (1 << 2) | (2 << 5) | (1 << 8));
219 /* set srate */
220 wmcodec_write(SRATECTRL, (0 << 1));
223 void audiohw_enable_recording(bool source_mic)
225 (void)source_mic; /* We only have a line-in (I think) */
227 /* reset the I2S controller into known state */
228 i2s_reset();
230 wmcodec_write(RESET, 0x1ff); /*Reset*/
232 wmcodec_write(PWRMGMT1, 0x2b);
233 wmcodec_write(PWRMGMT2, 0x18f); /* Enable ADC - 0x0c enables left/right PGA input, and 0x03 turns on power to the ADCs */
234 wmcodec_write(PWRMGMT3, 0x6f);
236 wmcodec_write(AINTFCE, 0x10);
237 wmcodec_write(CLKCTRL, 0x49);
239 wmcodec_write(OUTCTRL, 1);
241 /* The iPod can handle multiple frequencies, but fix at 44.1KHz
242 for now */
243 audiohw_set_sample_rate(WM8758_44100HZ);
245 wmcodec_write(INCTRL,0x44); /* Connect L2 and R2 inputs */
247 /* Set L2/R2_2BOOSTVOL to 0db (bits 4-6) */
248 /* 000 = disabled
249 001 = -12dB
250 010 = -9dB
251 011 = -6dB
252 100 = -3dB
253 101 = 0dB
254 110 = 3dB
255 111 = 6dB
257 wmcodec_write(LADCBOOST,0x50);
258 wmcodec_write(RADCBOOST,0x50);
260 /* Set L/R input PGA Volume to 0db */
261 // wm8758_write(LINPGAVOL,0x3f);
262 // wm8758_write(RINPGAVOL,0x13f);
264 /* Enable monitoring */
265 wmcodec_write(LOUTMIX,0x17); /* Enable output mixer - BYPL2LMIX @ 0db*/
266 wmcodec_write(ROUTMIX,0x17); /* Enable output mixer - BYPR2RMIX @ 0db*/
268 audiohw_mute(0);
271 void audiohw_disable_recording(void) {
272 audiohw_mute(1);
274 wmcodec_write(PWRMGMT3, 0x0);
276 wmcodec_write(PWRMGMT1, 0x0);
278 wmcodec_write(PWRMGMT2, 0x40);
281 void audiohw_set_recvol(int left, int right, int type) {
283 (void)left;
284 (void)right;
285 (void)type;
288 void audiohw_set_monitor(bool enable) {
290 (void)enable;