Rockbox supports not only 1bpp BMPs
[kugel-rb.git] / firmware / drivers / audio / wm8975.c
blobf14890db74d5d85b16751eb75671494a25c1ae83
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"
37 const struct sound_settings_info audiohw_settings[] = {
38 [SOUND_VOLUME] = {"dB", 0, 1, -74, 6, -25},
39 [SOUND_BASS] = {"dB", 0, 1, -6, 9, 0},
40 [SOUND_TREBLE] = {"dB", 0, 1, -6, 9, 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, 63, 23},
46 [SOUND_RIGHT_GAIN] = {"dB", 1, 1, 0, 63, 23},
47 [SOUND_MIC_GAIN] = {"dB", 1, 1, 0, 63, 0},
48 #endif
51 static unsigned short wm8975_regs[] =
53 [LINVOL] = LINVOL_LZCEN | 23, /* 0dB */
54 [RINVOL] = RINVOL_RIVU | RINVOL_RZCEN | 23, /* 0dB */
55 [DAPCTRL] = DAPCTRL_DACMU,
56 [PWRMGMT1] = PWRMGMT1_VMIDSEL_5K | PWRMGMT1_VREF,
57 [PWRMGMT2] = PWRMGMT2_DACL | PWRMGMT2_DACR | PWRMGMT2_LOUT1
58 | PWRMGMT2_ROUT1 | PWRMGMT2_LOUT2 | PWRMGMT2_ROUT2,
61 static void wm8975_write(int reg, unsigned val)
63 wm8975_regs[reg] = val;
64 wmcodec_write(reg, val);
67 static void wm8975_write_and(int reg, unsigned bits)
69 wm8975_write(reg, wm8975_regs[reg] & bits);
72 static void wm8975_write_or(int reg, unsigned bits)
74 wm8975_write(reg, wm8975_regs[reg] | bits);
77 /* convert tenth of dB volume (-730..60) to master volume register value */
78 int tenthdb2master(int db)
80 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
81 /* 1111111 == +6dB (0x7f) */
82 /* 1111001 == 0dB (0x79) */
83 /* 0110000 == -73dB (0x30 */
84 /* 0101111..0000000 == mute (0x2f) */
86 if (db < VOLUME_MIN) {
87 return 0x0;
88 } else {
89 return((db/10)+73+0x30);
93 int sound_val2phys(int setting, int value)
95 int result;
97 switch(setting)
99 #ifdef HAVE_RECORDING
100 case SOUND_LEFT_GAIN:
101 case SOUND_RIGHT_GAIN:
102 result = ((value - 23) * 15) / 2;
103 break;
104 case SOUND_MIC_GAIN:
105 result = ((value - 23) * 15) / 2 + 200;
106 break;
107 #endif
108 default:
109 result = value;
110 break;
113 return result;
116 void audiohw_mute(bool mute)
118 if (mute) {
119 /* Set DACMU = 1 to soft-mute the audio DACs. */
120 wm8975_write_or(DAPCTRL, DAPCTRL_DACMU);
121 } else {
122 /* Set DACMU = 0 to soft-un-mute the audio DACs. */
123 wm8975_write_and(DAPCTRL, ~DAPCTRL_DACMU);
127 #define IPOD_PCM_LEVEL 0x65 /* -6dB */
129 void audiohw_preinit(void)
131 /* POWER UP SEQUENCE */
132 wmcodec_write(RESET, RESET_RESET);
134 /* 2. Enable Vmid and VREF, quick startup. */
135 wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1]);
136 sleep(HZ/50);
137 wm8975_regs[PWRMGMT1] &= ~PWRMGMT1_VMIDSEL_MASK;
138 wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1] | PWRMGMT1_VMIDSEL_50K);
140 /* 4. Enable DACs, line and headphone output buffers as required. */
141 wm8975_write(PWRMGMT2, wm8975_regs[PWRMGMT2]);
143 wmcodec_write(AINTFCE, AINTFCE_MS | AINTFCE_LRP_I2S_RLO
144 | AINTFCE_IWL_16BIT | AINTFCE_FORMAT_I2S);
146 wm8975_write(DAPCTRL, wm8975_regs[DAPCTRL] );
148 wmcodec_write(SAMPCTRL, WM8975_44100HZ);
150 /* set the volume to -6dB */
151 wmcodec_write(LOUT1VOL, LOUT1VOL_LO1ZC | IPOD_PCM_LEVEL);
152 wmcodec_write(ROUT1VOL, ROUT1VOL_RO1VU | ROUT1VOL_RO1ZC | IPOD_PCM_LEVEL);
154 wmcodec_write(LOUTMIX1, LOUTMIX1_LD2LO| LOUTMIX1_LI2LOVOL(5));
155 wmcodec_write(LOUTMIX2, LOUTMIX2_RI2LOVOL(5));
157 wmcodec_write(ROUTMIX1, ROUTMIX1_LI2ROVOL(5));
158 wmcodec_write(ROUTMIX2, ROUTMIX2_RD2RO| ROUTMIX2_RI2ROVOL(5));
160 wmcodec_write(MOUTMIX1, 0);
161 wmcodec_write(MOUTMIX2, 0);
164 void audiohw_postinit(void)
166 audiohw_mute(false);
170 void audiohw_set_master_vol(int vol_l, int vol_r)
172 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
173 /* 1111111 == +6dB */
174 /* 1111001 == 0dB */
175 /* 0110000 == -73dB */
176 /* 0101111 == mute (0x2f) */
178 /* OUT1 */
179 wmcodec_write(LOUT1VOL, LOUT1VOL_LO1ZC | vol_l);
180 wmcodec_write(ROUT1VOL, ROUT1VOL_RO1VU | ROUT1VOL_RO1ZC | vol_r);
183 void audiohw_set_lineout_vol(int vol_l, int vol_r)
185 /* OUT2 */
186 wmcodec_write(LOUT2VOL, LOUT2VOL_LO2ZC | vol_l);
187 wmcodec_write(ROUT2VOL, ROUT2VOL_RO2VU | ROUT2VOL_RO2ZC | vol_r);
190 void audiohw_set_bass(int value)
192 const int regvalues[] = {
193 11, 10, 10, 9, 8, 8, 0xf, 6, 6, 5, 4, 4, 3, 2, 2, 1
196 if ((value >= -6) && (value <= 9)) {
197 /* We use linear bass control with 200 Hz cutoff */
198 wmcodec_write(BASSCTRL, regvalues[value + 6] | BASSCTRL_BC);
202 void audiohw_set_treble(int value)
204 const int regvalues[] = {
205 11, 10, 10, 9, 8, 8, 0xf, 6, 6, 5, 4, 4, 3, 2, 2, 1
208 if ((value >= -6) && (value <= 9)) {
209 /* We use linear treble control with 4 kHz cutoff */
210 wmcodec_write(TREBCTRL, regvalues[value + 6] | TREBCTRL_TC);
214 /* Nice shutdown of WM8975 codec */
215 void audiohw_close(void)
217 audiohw_mute(true);
219 /* 2. Disable all output buffers. */
220 wmcodec_write(PWRMGMT2, 0x0);
222 /* 3. Switch off the power supplies. */
223 wmcodec_write(PWRMGMT1, 0x0);
226 /* Note: Disable output before calling this function */
227 void audiohw_set_frequency(int fsel)
229 (void)fsel;
232 #ifdef HAVE_RECORDING
233 void audiohw_enable_recording(bool source_mic)
235 wm8975_regs[PWRMGMT1] |= PWRMGMT1_AINL | PWRMGMT1_AINR
236 | PWRMGMT1_ADCL | PWRMGMT1_ADCR;
237 wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1]);
239 /* NOTE: When switching to digital monitoring we will not want
240 * the DACs disabled. Also the outputs shouldn't be disabled
241 * when recording from line in (dock connector) - needs testing. */
242 wm8975_regs[PWRMGMT2] &= ~(PWRMGMT2_LOUT1 | PWRMGMT2_ROUT1
243 | PWRMGMT2_LOUT2 | PWRMGMT2_ROUT2);
244 wm8975_write(PWRMGMT2, wm8975_regs[PWRMGMT2]);
246 wm8975_write_or(LINVOL, LINVOL_LINMUTE);
247 wm8975_write_or(RINVOL, RINVOL_RINMUTE);
249 wmcodec_write(ADDCTRL3, ADDCTRL3_VROI);
251 if (source_mic) {
252 wmcodec_write(ADDCTRL1, ADDCTRL1_VSEL_LOWBIAS | ADDCTRL1_DATSEL_RADC
253 | ADDCTRL1_TOEN);
254 wmcodec_write(ADCLPATH, 0);
255 wmcodec_write(ADCRPATH, ADCRPATH_RINSEL_RIN2 | ADCRPATH_RMICBOOST_20dB);
256 } else {
257 wmcodec_write(ADDCTRL1, ADDCTRL1_VSEL_LOWBIAS | ADDCTRL1_DATSEL_NORMAL
258 | ADDCTRL1_TOEN);
259 wmcodec_write(ADCLPATH, ADCLPATH_LINSEL_LIN1 | ADCLPATH_LMICBOOST_OFF);
260 wmcodec_write(ADCRPATH, ADCRPATH_RINSEL_RIN1 | ADCRPATH_RMICBOOST_OFF);
262 wm8975_write_and(LINVOL, ~LINVOL_LINMUTE);
263 wm8975_write_and(RINVOL, ~RINVOL_RINMUTE);
266 void audiohw_disable_recording(void)
268 /* mute inputs */
269 wm8975_write_or(LINVOL, LINVOL_LINMUTE);
270 wm8975_write_or(RINVOL, RINVOL_RINMUTE);
272 wmcodec_write(ADDCTRL3, 0);
274 wm8975_regs[PWRMGMT2] |= PWRMGMT2_DACL | PWRMGMT2_DACR
275 | PWRMGMT2_LOUT1 | PWRMGMT2_ROUT1
276 | PWRMGMT2_LOUT2 | PWRMGMT2_ROUT2;
277 wm8975_write(PWRMGMT2, wm8975_regs[PWRMGMT2]);
279 wm8975_regs[PWRMGMT1] &= ~(PWRMGMT1_AINL | PWRMGMT1_AINR
280 | PWRMGMT1_ADCL | PWRMGMT1_ADCR);
281 wm8975_write(PWRMGMT1, wm8975_regs[PWRMGMT1]);
284 void audiohw_set_recvol(int left, int right, int type)
286 switch (type)
288 case AUDIO_GAIN_MIC: /* Mic uses right ADC */
289 wm8975_regs[RINVOL] &= ~RINVOL_MASK;
290 wm8975_write_or(RINVOL, left & RINVOL_MASK);
291 break;
292 case AUDIO_GAIN_LINEIN:
293 wm8975_regs[LINVOL] &= ~LINVOL_MASK;
294 wm8975_write_or(LINVOL, left & LINVOL_MASK);
295 wm8975_regs[RINVOL] &= ~RINVOL_MASK;
296 wm8975_write_or(RINVOL, right & RINVOL_MASK);
297 break;
298 default:
299 return;
303 void audiohw_set_monitor(bool enable)
305 (void)enable;
307 #endif /* HAVE_RECORDING */