Get rid of remaining audiohw_enable_output style codec setup and use pre/post split...
[kugel-rb.git] / firmware / drivers / audio / wm8751.c
blobd53cfa3d79af479eef03bcf6e7b438a127f1eb89
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 * 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 "kernel.h"
29 #include "wmcodec.h"
30 #include "audio.h"
31 #include "audiohw.h"
32 #include "system.h"
34 const struct sound_settings_info audiohw_settings[] = {
35 [SOUND_VOLUME] = {"dB", 0, 1, -74, 6, -25},
36 #ifdef USE_ADAPTIVE_BASS
37 [SOUND_BASS] = {"", 0, 1, 0, 15, 0},
38 #else
39 [SOUND_BASS] = {"dB", 1, 15, -60, 90, 0},
40 #endif
41 [SOUND_TREBLE] = {"dB", 1, 15, -60, 90, 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},
47 /* Flags used in combination with settings */
49 /* use zero crossing to reduce clicks during volume changes */
50 #define LOUT1_BITS (LOUT1_LO1ZC)
51 /* latch left volume first then update left+right together */
52 #define ROUT1_BITS (ROUT1_RO1ZC | ROUT1_RO1VU)
53 #define LOUT2_BITS (LOUT2_LO2ZC)
54 #define ROUT2_BITS (ROUT2_RO2ZC | ROUT2_RO2VU)
55 /* We use linear bass control with 200 Hz cutoff */
56 #ifdef USE_ADAPTIVE_BASE
57 #define BASSCTRL_BITS (BASSCTRL_BC | BASSCTRL_BB)
58 #else
59 #define BASSCTRL_BITS (BASSCTRL_BC)
60 #endif
61 /* We use linear treble control with 4 kHz cutoff */
62 #define TREBCTRL_BITS (TREBCTRL_TC)
64 /* convert tenth of dB volume (-730..60) to master volume register value */
65 int tenthdb2master(int db)
67 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
68 /* 1111111 == +6dB (0x7f) */
69 /* 1111001 == 0dB (0x79) */
70 /* 0110000 == -73dB (0x30) */
71 /* 0101111..0000000 == mute (<= 0x2f) */
72 if (db < VOLUME_MIN)
73 return 0x0;
74 else
75 return (db / 10) + 73 + 0x30;
78 static int tone_tenthdb2hw(int value)
80 /* -6.0db..+0db..+9.0db step 1.5db - translate -60..+0..+90 step 15
81 to 10..6..0 step -1.
83 value = 10 - (value + 60) / 15;
85 if (value == 6)
86 value = 0xf; /* 0db -> off */
88 return value;
92 #ifdef USE_ADAPTIVE_BASS
93 static int adaptivebass2hw(int value)
95 /* 0 to 15 step 1 - step -1 0 = off is a 15 in the register */
96 value = 15 - value;
98 return value;
100 #endif
102 /* Reset and power up the WM8751 */
103 void audiohw_preinit(void)
105 #ifdef MROBE_100
106 /* controls headphone ouput */
107 GPIOL_ENABLE |= 0x10;
108 GPIOL_OUTPUT_EN |= 0x10;
109 GPIOL_OUTPUT_VAL |= 0x10; /* disable */
110 #endif
112 #ifdef CPU_PP502x
113 i2s_reset();
114 #endif
117 * 1. Switch on power supplies.
118 * By default the WM8751 is in Standby Mode, the DAC is
119 * digitally muted and the Audio Interface, Line outputs
120 * and Headphone outputs are all OFF (DACMU = 1 Power
121 * Management registers 1 and 2 are all zeros).
123 wmcodec_write(RESET, RESET_RESET); /*Reset*/
125 /* 2. Enable Vmid and VREF. */
126 wmcodec_write(PWRMGMT1, PWRMGMT1_VREF | PWRMGMT1_VMIDSEL_5K);
128 /* BCLKINV=0(Dont invert BCLK) MS=1(Enable Master) LRSWAP=0 LRP=0 */
129 /* IWL=00(16 bit) FORMAT=10(I2S format) */
130 wmcodec_write(AINTFCE, AINTFCE_MS | AINTFCE_WL_16 |
131 AINTFCE_FORMAT_I2S);
134 /* Enable DACs and audio output after a short delay */
135 void audiohw_postinit(void)
137 /* From app notes: allow Vref to stabilize to reduce clicks */
138 sleep(HZ);
140 /* 3. Enable DACs as required. */
141 wmcodec_write(PWRMGMT2, PWRMGMT2_DACL | PWRMGMT2_DACR);
143 /* 4. Enable line and / or headphone output buffers as required. */
144 #ifdef MROBE_100
145 wmcodec_write(PWRMGMT2, PWRMGMT2_DACL | PWRMGMT2_DACR |
146 PWRMGMT2_LOUT1 | PWRMGMT2_ROUT1);
147 #else
148 wmcodec_write(PWRMGMT2, PWRMGMT2_DACL | PWRMGMT2_DACR |
149 PWRMGMT2_LOUT1 | PWRMGMT2_ROUT1 | PWRMGMT2_LOUT2 |
150 PWRMGMT2_ROUT2);
151 #endif
153 wmcodec_write(ADDITIONAL1, ADDITIONAL1_TSDEN | ADDITIONAL1_TOEN |
154 ADDITIONAL1_DMONOMIX_LLRR | ADDITIONAL1_VSEL_DEFAULT);
156 wmcodec_write(LEFTMIX1, LEFTMIX1_LD2LO | LEFTMIX1_LI2LO_DEFAULT);
157 wmcodec_write(RIGHTMIX2, RIGHTMIX2_RD2RO | RIGHTMIX2_RI2RO_DEFAULT);
159 audiohw_mute(false);
161 #ifdef MROBE_100
162 /* enable headphone output */
163 GPIOL_OUTPUT_VAL &= ~0x10;
164 GPIOL_OUTPUT_EN |= 0x10;
165 #endif
168 void audiohw_set_master_vol(int vol_l, int vol_r)
170 /* +6 to -73dB 1dB steps (plus mute == 80levels) 7bits */
171 /* 1111111 == +6dB */
172 /* 1111001 == 0dB */
173 /* 0110000 == -73dB */
174 /* 0101111 == mute (0x2f) */
176 wmcodec_write(LOUT1, LOUT1_BITS | LOUT1_LOUT1VOL(vol_l));
177 wmcodec_write(ROUT1, ROUT1_BITS | ROUT1_ROUT1VOL(vol_r));
180 #ifndef MROBE_100
181 void audiohw_set_lineout_vol(int vol_l, int vol_r)
183 wmcodec_write(LOUT2, LOUT2_BITS | LOUT2_LOUT2VOL(vol_l));
184 wmcodec_write(ROUT2, ROUT2_BITS | ROUT2_ROUT2VOL(vol_r));
186 #endif
188 void audiohw_set_bass(int value)
190 wmcodec_write(BASSCTRL, BASSCTRL_BITS |
192 #ifdef USE_ADAPTIVE_BASS
193 BASSCTRL_BASS(adaptivebass2hw(value)));
194 #else
195 BASSCTRL_BASS(tone_tenthdb2hw(value)));
196 #endif
199 void audiohw_set_treble(int value)
201 wmcodec_write(TREBCTRL, TREBCTRL_BITS |
202 TREBCTRL_TREB(tone_tenthdb2hw(value)));
205 void audiohw_mute(bool mute)
207 /* Mute: Set DACMU = 1 to soft-mute the audio DACs. */
208 /* Unmute: Set DACMU = 0 to soft-un-mute the audio DACs. */
209 wmcodec_write(DACCTRL, mute ? DACCTRL_DACMU : 0);
212 /* Nice shutdown of WM8751 codec */
213 void audiohw_close(void)
215 /* 1. Set DACMU = 1 to soft-mute the audio DACs. */
216 audiohw_mute(true);
218 /* 2. Disable all output buffers. */
219 wmcodec_write(PWRMGMT2, 0x0);
221 /* 3. Switch off the power supplies. */
222 wmcodec_write(PWRMGMT1, 0x0);
225 /* Note: Disable output before calling this function */
226 void audiohw_set_frequency(int fsel)
228 wmcodec_write(CLOCKING, fsel);