convert to unix line endings.
[Rockbox.git] / firmware / drivers / audio / wm8985.c
blob95afbd97c9e7f4567a6d925f160e7bd51d63ee9f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * All files in this archive are subject to the GNU General Public License.
11 * See the file COPYING in the source tree root for full license agreement.
13 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
14 * KIND, either express or implied.
16 ****************************************************************************/
17 #include "logf.h"
18 #include "system.h"
19 #include "string.h"
20 #include "audio.h"
22 #include "wmcodec.h"
23 #include "audiohw.h"
24 #include "i2s.h"
26 /* Register addresses as per datasheet Rev.4.4 */
27 #define RESET 0x00
28 #define PWRMGMT1 0x01
29 #define PWRMGMT2 0x02
30 #define PWRMGMT3 0x03
31 #define AINTFCE 0x04
32 #define COMPAND 0x05
33 #define CLKGEN 0x06
34 #define SRATECTRL 0x07
35 #define GPIOCTL 0x08
36 #define JACKDETECT0 0x09
37 #define DACCTRL 0x0a
38 #define LDACVOL 0x0b
39 #define RDACVOL 0x0c
40 #define JACKDETECT1 0x0d
41 #define ADCCTL 0x0e
42 #define LADCVOL 0x0f
43 #define RADCVOL 0x10
45 #define EQ1 0x12
46 #define EQ2 0x13
47 #define EQ3 0x14
48 #define EQ4 0x15
49 #define EQ5 0x16
50 #define EQ_GAIN_MASK 0x001f
51 #define EQ_CUTOFF_MASK 0x0060
52 #define EQ_GAIN_VALUE(x) (((-x) + 12) & 0x1f)
53 #define EQ_CUTOFF_VALUE(x) ((((x) - 1) & 0x03) << 5)
55 #define CLASSDCTL 0x17
56 #define DACLIMIT1 0x18
57 #define DACLIMIT2 0x19
58 #define NOTCH1 0x1b
59 #define NOTCH2 0x1c
60 #define NOTCH3 0x1d
61 #define NOTCH4 0x1e
62 #define ALCCTL1 0x20
63 #define ALCCTL2 0x21
64 #define ALCCTL3 0x22
65 #define NOISEGATE 0x23
66 #define PLLN 0x24
67 #define PLLK1 0x25
68 #define PLLK2 0x26
69 #define PLLK3 0x27
70 #define THREEDCTL 0x29
71 #define OUT4ADC 0x2a
72 #define BEEPCTRL 0x2b
73 #define INCTRL 0x2c
74 #define LINPGAGAIN 0x2d
75 #define RINPGAGAIN 0x2e
76 #define LADCBOOST 0x2f
77 #define RADCBOOST 0x30
78 #define OUTCTRL 0x31
79 #define LOUTMIX 0x32
80 #define ROUTMIX 0x33
81 #define LOUT1VOL 0x34
82 #define ROUT1VOL 0x35
83 #define LOUT2VOL 0x36
84 #define ROUT2VOL 0x37
85 #define OUT3MIX 0x38
86 #define OUT4MIX 0x39
87 #define BIASCTL 0x3d
89 /* Register settings for the supported samplerates: */
90 #define WM8985_8000HZ 0x4d
91 #define WM8985_12000HZ 0x61
92 #define WM8985_16000HZ 0x55
93 #define WM8985_22050HZ 0x77
94 #define WM8985_24000HZ 0x79
95 #define WM8985_32000HZ 0x59
96 #define WM8985_44100HZ 0x63
97 #define WM8985_48000HZ 0x41
98 #define WM8985_88200HZ 0x7f
99 #define WM8985_96000HZ 0x5d
101 const struct sound_settings_info audiohw_settings[] = {
102 [SOUND_VOLUME] = {"dB", 0, 1, -58, 6, -25},
103 [SOUND_BASS] = {"dB", 0, 1, -12, 12, 0},
104 [SOUND_TREBLE] = {"dB", 0, 1, -12, 12, 0},
105 [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
106 [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
107 [SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100},
108 [SOUND_LEFT_GAIN] = {"dB", 1, 1,-128, 96, 0},
109 [SOUND_RIGHT_GAIN] = {"dB", 1, 1,-128, 96, 0},
110 [SOUND_MIC_GAIN] = {"dB", 1, 1,-128, 108, 16},
111 [SOUND_BASS_CUTOFF] = {"", 0, 1, 1, 4, 1},
112 [SOUND_TREBLE_CUTOFF] = {"", 0, 1, 1, 4, 1},
115 /* shadow registers */
116 unsigned int eq1_reg;
117 unsigned int eq5_reg;
119 /* convert tenth of dB volume (-57..6) to master volume register value */
120 int tenthdb2master(int db)
122 /* +6 to -57dB in 1dB steps == 64 levels = 6 bits */
123 /* 0111111 == +6dB (0x3f) = 63) */
124 /* 0111001 == 0dB (0x39) = 57) */
125 /* 0000001 == -56dB (0x01) = */
126 /* 0000000 == -57dB (0x00) */
128 /* 1000000 == Mute (0x40) */
130 if (db < VOLUME_MIN) {
131 return 0x40;
132 } else {
133 return((db/10)+57);
137 /* convert tenth of dB volume (-780..0) to mixer volume register value */
138 int tenthdb2mixer(int db)
140 if (db < -660) /* 1.5 dB steps */
141 return (2640 - db) / 15;
142 else if (db < -600) /* 0.75 dB steps */
143 return (990 - db) * 2 / 15;
144 else if (db < -460) /* 0.5 dB steps */
145 return (460 - db) / 5;
146 else /* 0.25 dB steps */
147 return -db * 2 / 5;
150 /* Silently enable / disable audio output */
151 void audiohw_enable_output(bool enable)
153 if (enable)
155 /* TODO: reset the I2S controller into known state */
156 //i2s_reset();
158 /* TODO: Review the power-up sequence to prevent pops (see datasheet) */
160 wmcodec_write(RESET, 0x1ff); /*Reset*/
162 wmcodec_write(PWRMGMT1, 0x2b);
163 wmcodec_write(PWRMGMT2, 0x180);
164 wmcodec_write(PWRMGMT3, 0x6f);
166 wmcodec_write(AINTFCE, 0x10);
167 wmcodec_write(CLKCTRL, 0x49);
169 wmcodec_write(OUTCTRL, 1);
171 /* The iPod can handle multiple frequencies, but fix at 44.1KHz
172 for now */
173 audiohw_set_sample_rate(WM8985_44100HZ);
175 wmcodec_write(LOUTMIX,0x1); /* Enable mixer */
176 wmcodec_write(ROUTMIX,0x1); /* Enable mixer */
177 audiohw_mute(0);
178 } else {
179 audiohw_mute(1);
183 void audiohw_set_master_vol(int vol_l, int vol_r)
185 /* OUT1 */
186 wmcodec_write(LOUT1VOL, 0x080 | vol_l);
187 wmcodec_write(ROUT1VOL, 0x180 | vol_r);
190 void audiohw_set_lineout_vol(int vol_l, int vol_r)
192 /* OUT2 */
193 wmcodec_write(LOUT2VOL, vol_l);
194 wmcodec_write(ROUT2VOL, 0x100 | vol_r);
197 void audiohw_set_mixer_vol(int channel1, int channel2)
199 (void)channel1;
200 (void)channel2;
203 void audiohw_set_bass(int value)
205 eq1_reg = (eq1_reg & ~EQ_GAIN_MASK) | EQ_GAIN_VALUE(value);
206 wmcodec_write(EQ1, 0x100 | eq1_reg);
209 void audiohw_set_bass_cutoff(int value)
211 eq1_reg = (eq1_reg & ~EQ_CUTOFF_MASK) | EQ_CUTOFF_VALUE(value);
212 wmcodec_write(EQ1, 0x100 | eq1_reg);
215 void audiohw_set_treble(int value)
217 eq5_reg = (eq5_reg & ~EQ_GAIN_MASK) | EQ_GAIN_VALUE(value);
218 wmcodec_write(EQ5, eq5_reg);
221 void audiohw_set_treble_cutoff(int value)
223 eq5_reg = (eq5_reg & ~EQ_CUTOFF_MASK) | EQ_CUTOFF_VALUE(value);
224 wmcodec_write(EQ5, eq5_reg);
227 void audiohw_mute(bool mute)
229 if (mute)
231 /* Set DACMU = 1 to soft-mute the audio DACs. */
232 wmcodec_write(DACCTRL, 0x40);
233 } else {
234 /* Set DACMU = 0 to soft-un-mute the audio DACs. */
235 wmcodec_write(DACCTRL, 0x0);
239 /* Nice shutdown of WM8758 codec */
240 void audiohw_close(void)
242 audiohw_mute(1);
244 wmcodec_write(PWRMGMT3, 0x0);
246 wmcodec_write(PWRMGMT1, 0x0);
248 wmcodec_write(PWRMGMT2, 0x40);
251 /* Change the order of the noise shaper, 5th order is recommended above 32kHz */
252 void audiohw_set_nsorder(int order)
254 (void)order;
257 /* Note: Disable output before calling this function */
258 void audiohw_set_sample_rate(int sampling_control)
260 /**** We force 44.1KHz for now. ****/
261 (void)sampling_control;
263 /* set clock div */
264 wmcodec_write(CLKCTRL, 1 | (0 << 2) | (2 << 5));
266 /* setup PLL for MHZ=11.2896 */
267 wmcodec_write(PLLN, (1 << 4) | 0x7);
268 wmcodec_write(PLLK1, 0x21);
269 wmcodec_write(PLLK2, 0x161);
270 wmcodec_write(PLLK3, 0x26);
272 /* set clock div */
273 wmcodec_write(CLKCTRL, 1 | (1 << 2) | (2 << 5) | (1 << 8));
275 /* set srate */
276 wmcodec_write(SRATECTRL, (0 << 1));
279 #ifdef HAVE_RECORDING
280 void audiohw_enable_recording(bool source_mic)
282 (void)source_mic; /* We only have a line-in (I think) */
284 /* TODO: reset the I2S controller into known state */
285 //i2s_reset();
287 wmcodec_write(RESET, 0x1ff); /*Reset*/
289 wmcodec_write(PWRMGMT1, 0x2b);
290 wmcodec_write(PWRMGMT2, 0x18f); /* Enable ADC - 0x0c enables left/right PGA input, and 0x03 turns on power to the ADCs */
291 wmcodec_write(PWRMGMT3, 0x6f);
293 wmcodec_write(AINTFCE, 0x10);
294 wmcodec_write(CLKCTRL, 0x49);
296 wmcodec_write(OUTCTRL, 1);
298 /* The iPod can handle multiple frequencies, but fix at 44.1KHz
299 for now */
300 audiohw_set_sample_rate(WM8985_44100HZ);
302 wmcodec_write(INCTRL,0x44); /* Connect L2 and R2 inputs */
304 /* Set L2/R2_2BOOSTVOL to 0db (bits 4-6) */
305 /* 000 = disabled
306 001 = -12dB
307 010 = -9dB
308 011 = -6dB
309 100 = -3dB
310 101 = 0dB
311 110 = 3dB
312 111 = 6dB
314 wmcodec_write(LADCBOOST,0x50);
315 wmcodec_write(RADCBOOST,0x50);
317 /* Set L/R input PGA Volume to 0db */
318 // wm8758_write(LINPGAVOL,0x3f);
319 // wm8758_write(RINPGAVOL,0x13f);
321 /* Enable monitoring */
322 wmcodec_write(LOUTMIX,0x17); /* Enable output mixer - BYPL2LMIX @ 0db*/
323 wmcodec_write(ROUTMIX,0x17); /* Enable output mixer - BYPR2RMIX @ 0db*/
325 audiohw_mute(0);
328 void audiohw_disable_recording(void) {
329 audiohw_mute(1);
331 wmcodec_write(PWRMGMT3, 0x0);
333 wmcodec_write(PWRMGMT1, 0x0);
335 wmcodec_write(PWRMGMT2, 0x40);
338 void audiohw_set_recvol(int left, int right, int type) {
340 (void)left;
341 (void)right;
342 (void)type;
345 void audiohw_set_monitor(bool enable) {
347 (void)enable;
349 #endif /* HAVE_RECORDING */