Fix some more #includes
[kugel-rb.git] / firmware / drivers / audio / wm8975.c
blob23ce91fb5b1092680cc7cd7ac5e13e9739dcf1fc
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Driver for WM8975 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 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
25 * KIND, either express or implied.
27 ****************************************************************************/
28 #include "logf.h"
29 #include "system.h"
30 #include "string.h"
31 #include "audio.h"
32 #include "sound.h"
34 #include "wmcodec.h"
35 #include "audiohw.h"
36 #include "i2s.h"
38 const struct sound_settings_info audiohw_settings[] = {
39 [SOUND_VOLUME] = {"dB", 0, 1, -74, 6, -25},
40 [SOUND_BASS] = {"dB", 0, 1, -6, 9, 0},
41 [SOUND_TREBLE] = {"dB", 0, 1, -6, 9, 0},
42 [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
43 [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
44 [SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100},
45 #ifdef HAVE_RECORDING
46 [SOUND_LEFT_GAIN] = {"dB", 1, 1, 0, 63, 23},
47 [SOUND_RIGHT_GAIN] = {"dB", 1, 1, 0, 63, 23},
48 [SOUND_MIC_GAIN] = {"dB", 1, 1, 0, 63, 0},
49 #endif
52 static unsigned short wm8975_regs[] =
54 [LINVOL] = LINVOL_LZCEN | 23, /* 0dB */
55 [RINVOL] = RINVOL_RIVU | RINVOL_RZCEN | 23, /* 0dB */
56 [DAPCTRL] = DAPCTRL_DACMU,
57 [PWRMGMT1] = PWRMGMT1_VMIDSEL_5K | PWRMGMT1_VREF,
58 [PWRMGMT2] = PWRMGMT2_DACL | PWRMGMT2_DACR | PWRMGMT2_LOUT1
59 | PWRMGMT2_ROUT1 | PWRMGMT2_LOUT2 | PWRMGMT2_ROUT2,
62 static void wm8975_write(int reg, unsigned val)
64 wm8975_regs[reg] = val;
65 wmcodec_write(reg, val);
68 static void wm8975_write_and(int reg, unsigned bits)
70 wm8975_write(reg, wm8975_regs[reg] & bits);
73 static void wm8975_write_or(int reg, unsigned bits)
75 wm8975_write(reg, wm8975_regs[reg] | bits);
78 /* convert tenth of dB volume (-730..60) to master volume register value */
79 int tenthdb2master(int db)
81 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
82 /* 1111111 == +6dB (0x7f) */
83 /* 1111001 == 0dB (0x79) */
84 /* 0110000 == -73dB (0x30 */
85 /* 0101111..0000000 == mute (0x2f) */
87 if (db < VOLUME_MIN) {
88 return 0x0;
89 } else {
90 return((db/10)+73+0x30);
94 int sound_val2phys(int setting, int value)
96 int result;
98 switch(setting)
100 #ifdef HAVE_RECORDING
101 case SOUND_LEFT_GAIN:
102 case SOUND_RIGHT_GAIN:
103 result = ((value - 23) * 15) / 2;
104 break;
105 case SOUND_MIC_GAIN:
106 result = ((value - 23) * 15) / 2 + 200;
107 break;
108 #endif
109 default:
110 result = value;
111 break;
114 return result;
117 void audiohw_mute(bool mute)
119 if (mute) {
120 /* Set DACMU = 1 to soft-mute the audio DACs. */
121 wm8975_write_or(DAPCTRL, DAPCTRL_DACMU);
122 } else {
123 /* Set DACMU = 0 to soft-un-mute the audio DACs. */
124 wm8975_write_and(DAPCTRL, ~DAPCTRL_DACMU);
128 #define IPOD_PCM_LEVEL 0x65 /* -6dB */
130 void audiohw_preinit(void)
132 i2s_reset();
134 /* POWER UP SEQUENCE */
135 wmcodec_write(RESET, RESET_RESET);
137 /* 2. Enable Vmid and VREF, quick startup. */
138 wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1]);
139 sleep(HZ/50);
140 wm8975_regs[PWRMGMT1] &= ~PWRMGMT1_VMIDSEL_MASK;
141 wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1] | PWRMGMT1_VMIDSEL_50K);
143 /* 4. Enable DACs, line and headphone output buffers as required. */
144 wm8975_write(PWRMGMT2, wm8975_regs[PWRMGMT2]);
146 wmcodec_write(AINTFCE, AINTFCE_MS | AINTFCE_LRP_I2S_RLO
147 | AINTFCE_IWL_16BIT | AINTFCE_FORMAT_I2S);
149 wm8975_write(DAPCTRL, wm8975_regs[DAPCTRL] );
151 audiohw_set_sample_rate(WM8975_44100HZ);
153 /* set the volume to -6dB */
154 wmcodec_write(LOUT1VOL, LOUT1VOL_LO1ZC | IPOD_PCM_LEVEL);
155 wmcodec_write(ROUT1VOL, ROUT1VOL_RO1VU | ROUT1VOL_RO1ZC | IPOD_PCM_LEVEL);
157 wmcodec_write(LOUTMIX1, LOUTMIX1_LD2LO| LOUTMIX1_LI2LOVOL(5));
158 wmcodec_write(LOUTMIX2, LOUTMIX2_RI2LOVOL(5));
160 wmcodec_write(ROUTMIX1, ROUTMIX1_LI2ROVOL(5));
161 wmcodec_write(ROUTMIX2, ROUTMIX2_RD2RO| ROUTMIX2_RI2ROVOL(5));
163 wmcodec_write(MOUTMIX1, 0);
164 wmcodec_write(MOUTMIX2, 0);
167 void audiohw_postinit(void)
169 audiohw_mute(false);
173 void audiohw_set_master_vol(int vol_l, int vol_r)
175 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
176 /* 1111111 == +6dB */
177 /* 1111001 == 0dB */
178 /* 0110000 == -73dB */
179 /* 0101111 == mute (0x2f) */
181 /* OUT1 */
182 wmcodec_write(LOUT1VOL, LOUT1VOL_LO1ZC | vol_l);
183 wmcodec_write(ROUT1VOL, ROUT1VOL_RO1VU | ROUT1VOL_RO1ZC | vol_r);
186 void audiohw_set_lineout_vol(int vol_l, int vol_r)
188 /* OUT2 */
189 wmcodec_write(LOUT2VOL, LOUT2VOL_LO2ZC | vol_l);
190 wmcodec_write(ROUT2VOL, ROUT2VOL_RO2VU | ROUT2VOL_RO2ZC | vol_r);
193 void audiohw_set_bass(int value)
195 const int regvalues[] = {
196 11, 10, 10, 9, 8, 8, 0xf, 6, 6, 5, 4, 4, 3, 2, 2, 1
199 if ((value >= -6) && (value <= 9)) {
200 /* We use linear bass control with 200 Hz cutoff */
201 wmcodec_write(BASSCTRL, regvalues[value + 6] | BASSCTRL_BC);
205 void audiohw_set_treble(int value)
207 const int regvalues[] = {
208 11, 10, 10, 9, 8, 8, 0xf, 6, 6, 5, 4, 4, 3, 2, 2, 1
211 if ((value >= -6) && (value <= 9)) {
212 /* We use linear treble control with 4 kHz cutoff */
213 wmcodec_write(TREBCTRL, regvalues[value + 6] | TREBCTRL_TC);
217 /* Nice shutdown of WM8975 codec */
218 void audiohw_close(void)
220 audiohw_mute(true);
222 /* 2. Disable all output buffers. */
223 wmcodec_write(PWRMGMT2, 0x0);
225 /* 3. Switch off the power supplies. */
226 wmcodec_write(PWRMGMT1, 0x0);
229 void audiohw_set_nsorder(int order)
231 (void)order;
234 /* Note: Disable output before calling this function */
235 void audiohw_set_sample_rate(int sampling_control)
237 wmcodec_write(SAMPCTRL, sampling_control);
240 #ifdef HAVE_RECORDING
241 void audiohw_enable_recording(bool source_mic)
243 wm8975_regs[PWRMGMT1] |= PWRMGMT1_AINL | PWRMGMT1_AINR
244 | PWRMGMT1_ADCL | PWRMGMT1_ADCR;
245 wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1]);
247 /* NOTE: When switching to digital monitoring we will not want
248 * the DACs disabled. Also the outputs shouldn't be disabled
249 * when recording from line in (dock connector) - needs testing. */
250 wm8975_regs[PWRMGMT2] &= ~(PWRMGMT2_LOUT1 | PWRMGMT2_ROUT1
251 | PWRMGMT2_LOUT2 | PWRMGMT2_ROUT2);
252 wm8975_write(PWRMGMT2, wm8975_regs[PWRMGMT2]);
254 wm8975_write_or(LINVOL, LINVOL_LINMUTE);
255 wm8975_write_or(RINVOL, RINVOL_RINMUTE);
257 wmcodec_write(ADDCTRL3, ADDCTRL3_VROI);
259 if (source_mic) {
260 wmcodec_write(ADDCTRL1, ADDCTRL1_VSEL_LOWBIAS | ADDCTRL1_DATSEL_RADC
261 | ADDCTRL1_TOEN);
262 wmcodec_write(ADCLPATH, 0);
263 wmcodec_write(ADCRPATH, ADCRPATH_RINSEL_RIN2 | ADCRPATH_RMICBOOST_20dB);
264 } else {
265 wmcodec_write(ADDCTRL1, ADDCTRL1_VSEL_LOWBIAS | ADDCTRL1_DATSEL_NORMAL
266 | ADDCTRL1_TOEN);
267 wmcodec_write(ADCLPATH, ADCLPATH_LINSEL_LIN1 | ADCLPATH_LMICBOOST_OFF);
268 wmcodec_write(ADCRPATH, ADCRPATH_RINSEL_RIN1 | ADCRPATH_RMICBOOST_OFF);
270 wm8975_write_and(LINVOL, ~LINVOL_LINMUTE);
271 wm8975_write_and(RINVOL, ~RINVOL_RINMUTE);
274 void audiohw_disable_recording(void)
276 /* mute inputs */
277 wm8975_write_or(LINVOL, LINVOL_LINMUTE);
278 wm8975_write_or(RINVOL, RINVOL_RINMUTE);
280 wmcodec_write(ADDCTRL3, 0);
282 wm8975_regs[PWRMGMT2] |= PWRMGMT2_DACL | PWRMGMT2_DACR
283 | PWRMGMT2_LOUT1 | PWRMGMT2_ROUT1
284 | PWRMGMT2_LOUT2 | PWRMGMT2_ROUT2;
285 wm8975_write(PWRMGMT2, wm8975_regs[PWRMGMT2]);
287 wm8975_regs[PWRMGMT1] &= ~(PWRMGMT1_AINL | PWRMGMT1_AINR
288 | PWRMGMT1_ADCL | PWRMGMT1_ADCR);
289 wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1]);
292 void audiohw_set_recvol(int left, int right, int type)
294 switch (type)
296 case AUDIO_GAIN_MIC: /* Mic uses right ADC */
297 wm8975_regs[RINVOL] &= ~RINVOL_MASK;
298 wm8975_write_or(RINVOL, left & RINVOL_MASK);
299 break;
300 case AUDIO_GAIN_LINEIN:
301 wm8975_regs[LINVOL] &= ~LINVOL_MASK;
302 wm8975_write_or(LINVOL, left & LINVOL_MASK);
303 wm8975_regs[RINVOL] &= ~RINVOL_MASK;
304 wm8975_write_or(RINVOL, right & RINVOL_MASK);
305 break;
306 default:
307 return;
311 void audiohw_set_monitor(bool enable)
313 (void)enable;
315 #endif /* HAVE_RECORDING */