Commit FS#10082, enlarge volume control range for WM8758. This will enable volume...
[kugel-rb.git] / firmware / drivers / audio / wm8758.c
blob715c921e3316f380298e21b5a98b7d96233f686a
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 * 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 "system.h"
29 #include "string.h"
30 #include "audio.h"
32 #include "wmcodec.h"
33 #include "audiohw.h"
35 const struct sound_settings_info audiohw_settings[] = {
36 [SOUND_VOLUME] = {"dB", 0, 1, -90, 6, -25},
37 [SOUND_BASS] = {"dB", 0, 1, -12, 12, 0},
38 [SOUND_TREBLE] = {"dB", 0, 1, -12, 12, 0},
39 [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
40 [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
41 [SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100},
42 #ifdef HAVE_RECORDING
43 [SOUND_LEFT_GAIN] = {"dB", 1, 1, 0, 63, 16},
44 [SOUND_RIGHT_GAIN] = {"dB", 1, 1, 0, 63, 16},
45 [SOUND_MIC_GAIN] = {"dB", 1, 1, 0, 63, 16},
46 #endif
47 [SOUND_BASS_CUTOFF] = {"", 0, 1, 1, 4, 1},
48 [SOUND_TREBLE_CUTOFF] = {"", 0, 1, 1, 4, 1},
51 /* shadow registers */
52 static unsigned short eq1_reg = EQ1_EQ3DMODE | EQ_GAIN_VALUE(0);
53 static unsigned short eq5_reg = EQ_GAIN_VALUE(0);
55 /* convert tenth of dB volume (-89..6) to master volume register value */
56 int tenthdb2master(int db)
58 /* att DAC AMP result
59 +6dB 0 +6 96
60 0dB 0 0 90
61 -57dB 0 -57 33
62 -58dB -1 -57 32
63 -89dB -32 -57 1
64 -90dB -oo -oo 0 */
65 if (db < VOLUME_MIN) {
66 return 0;
67 } else {
68 return (db-VOLUME_MIN)/10 + 1;
72 /* helper function that calculates the register setting for amplifier and
73 DAC volume out of the input from tenthdb2master() */
74 static void get_volume_params(int db, int *dac, int *amp)
76 /* should never happen, set max volume for amp and dac */
77 if (db > 96) {
78 *dac = 255;
79 *amp = 63;
81 /* set dac to max and set volume for amp (better snr) */
82 else if (db > 32) {
83 *dac = 255;
84 *amp = (db-90)+57;
86 /* set amp to min and reduce dac output */
87 else if (db > 0) {
88 *dac = (db-33)*2 + 255;
89 *amp = 0;
91 /* mute all */
92 else {
93 *dac = 0x00;
94 *amp = 0x40;
98 int sound_val2phys(int setting, int value)
100 int result;
102 switch(setting)
104 #ifdef HAVE_RECORDING
105 case SOUND_LEFT_GAIN:
106 case SOUND_RIGHT_GAIN:
107 case SOUND_MIC_GAIN:
108 result = ((value - 16) * 15) / 2;
109 break;
110 #endif
111 default:
112 result = value;
113 break;
116 return result;
119 void audiohw_mute(bool mute)
121 if (mute) {
122 wmcodec_write(DACCTRL, DACCTRL_SOFTMUTE);
123 } else {
124 wmcodec_write(DACCTRL, 0);
128 void audiohw_preinit(void)
130 wmcodec_write(RESET, RESET_RESET);
132 wmcodec_write(PWRMGMT1, PWRMGMT1_PLLEN | PWRMGMT1_BIASEN
133 | PWRMGMT1_VMIDSEL_5K);
134 wmcodec_write(PWRMGMT2, PWRMGMT2_ROUT1EN | PWRMGMT2_LOUT1EN);
135 wmcodec_write(PWRMGMT3, PWRMGMT3_LOUT2EN | PWRMGMT3_ROUT2EN
136 | PWRMGMT3_RMIXEN | PWRMGMT3_LMIXEN
137 | PWRMGMT3_DACENR | PWRMGMT3_DACENL);
139 wmcodec_write(AINTFCE, AINTFCE_IWL_16BIT | AINTFCE_FORMAT_I2S);
140 wmcodec_write(OUTCTRL, OUTCTRL_VROI);
141 wmcodec_write(CLKCTRL, CLKCTRL_MS); /* WM8758 is clock master */
143 audiohw_set_frequency(HW_FREQ_44);
145 wmcodec_write(LOUTMIX, LOUTMIX_DACL2LMIX);
146 wmcodec_write(ROUTMIX, ROUTMIX_DACR2RMIX);
149 void audiohw_postinit(void)
151 wmcodec_write(PWRMGMT1, PWRMGMT1_PLLEN | PWRMGMT1_BIASEN
152 | PWRMGMT1_VMIDSEL_75K);
153 /* lower the VMID power consumption */
154 audiohw_mute(false);
157 void audiohw_set_master_vol(int vol_l, int vol_r)
159 int dac_l, amp_l, dac_r, amp_r;
160 get_volume_params(vol_l, &dac_l, &amp_l);
161 get_volume_params(vol_r, &dac_r, &amp_r);
163 /* set DAC
164 Important: DAC is global and will also affect lineout */
165 wmcodec_write(LDACVOL, dac_l);
166 wmcodec_write(RDACVOL, dac_r | RDACVOL_DACVU);
168 /* set headphone amp OUT1 */
169 wmcodec_write(LOUT1VOL, amp_l | LOUT1VOL_LOUT1ZC);
170 wmcodec_write(ROUT1VOL, amp_r | ROUT1VOL_ROUT1ZC | ROUT1VOL_OUT1VU);
173 void audiohw_set_lineout_vol(int vol_l, int vol_r)
175 int dac_l, amp_l, dac_r, amp_r;
176 get_volume_params(vol_l, &dac_l, &amp_l);
177 get_volume_params(vol_r, &dac_r, &amp_r);
179 /* set lineout amp OUT2 */
180 wmcodec_write(LOUT2VOL, amp_l | LOUT2VOL_LOUT2ZC);
181 wmcodec_write(ROUT2VOL, amp_r | ROUT2VOL_ROUT2ZC | ROUT2VOL_OUT2VU);
184 void audiohw_set_bass(int value)
186 eq1_reg = (eq1_reg & ~EQ_GAIN_MASK) | EQ_GAIN_VALUE(value);
187 wmcodec_write(EQ1, eq1_reg);
190 void audiohw_set_bass_cutoff(int value)
192 eq1_reg = (eq1_reg & ~EQ_CUTOFF_MASK) | EQ_CUTOFF_VALUE(value);
193 wmcodec_write(EQ1, eq1_reg);
196 void audiohw_set_treble(int value)
198 eq5_reg = (eq5_reg & ~EQ_GAIN_MASK) | EQ_GAIN_VALUE(value);
199 wmcodec_write(EQ5, eq5_reg);
202 void audiohw_set_treble_cutoff(int value)
204 eq5_reg = (eq5_reg & ~EQ_CUTOFF_MASK) | EQ_CUTOFF_VALUE(value);
205 wmcodec_write(EQ5, eq5_reg);
208 /* Nice shutdown of WM8758 codec */
209 void audiohw_close(void)
211 audiohw_mute(true);
213 wmcodec_write(PWRMGMT3, 0);
214 wmcodec_write(PWRMGMT1, 0);
215 wmcodec_write(PWRMGMT2, PWRMGMT2_SLEEP);
218 /* Note: Disable output before calling this function */
219 void audiohw_set_frequency(int fsel)
221 /* CLKCTRL_MCLKDIV_MASK and ADDCTRL_SR_MASK don't overlap,
222 so they can both fit in one byte. Bit 0 selects PLL
223 configuration via pll_setups.
225 static const unsigned char freq_setups[HW_NUM_FREQ] =
227 [HW_FREQ_48] = CLKCTRL_MCLKDIV_2 | ADDCTRL_SR_48kHz | 1,
228 [HW_FREQ_44] = CLKCTRL_MCLKDIV_2 | ADDCTRL_SR_48kHz,
229 [HW_FREQ_32] = CLKCTRL_MCLKDIV_3 | ADDCTRL_SR_32kHz | 1,
230 [HW_FREQ_24] = CLKCTRL_MCLKDIV_4 | ADDCTRL_SR_24kHz | 1,
231 [HW_FREQ_22] = CLKCTRL_MCLKDIV_4 | ADDCTRL_SR_24kHz,
232 [HW_FREQ_16] = CLKCTRL_MCLKDIV_6 | ADDCTRL_SR_16kHz | 1,
233 [HW_FREQ_12] = CLKCTRL_MCLKDIV_8 | ADDCTRL_SR_12kHz | 1,
234 [HW_FREQ_11] = CLKCTRL_MCLKDIV_8 | ADDCTRL_SR_12kHz,
235 [HW_FREQ_8] = CLKCTRL_MCLKDIV_12 | ADDCTRL_SR_8kHz | 1
238 /* Each PLL configuration is an array consisting of
239 { PLLN, PLLK1, PLLK2, PLLK3 }. The WM8983 datasheet requires
240 5 < PLLN < 13, and states optimum is PLLN = 8, f2 = 90 MHz
242 static const unsigned short pll_setups[2][4] =
244 /* f1 = 12 MHz, R = 7.5264, f2 = 90.3168 MHz, fPLLOUT = 22.5792 MHz */
245 { PLLN_PLLPRESCALE | 0x7, 0x21, 0x161, 0x26 },
246 /* f1 = 12 MHz, R = 8.192, f2 = 98.304 MHz, fPLLOUT = 24.576 MHz */
247 { PLLN_PLLPRESCALE | 0x8, 0xC, 0x93, 0xE9 }
250 int i;
252 /* PLLN, PLLK1, PLLK2, PLLK3 are contiguous (at 0x24 to 0x27) */
253 for (i = 0; i < 4; i++)
254 wmcodec_write(PLLN + i, pll_setups[freq_setups[fsel] & 1][i]);
256 /* CLKCTRL_MCLKDIV divides fPLLOUT to get SYSCLK (256 * sample rate) */
257 wmcodec_write(CLKCTRL, CLKCTRL_CLKSEL
258 | (freq_setups[fsel] & CLKCTRL_MCLKDIV_MASK)
259 | CLKCTRL_BCLKDIV_2 | CLKCTRL_MS);
261 /* set ADC and DAC filter characteristics according to sample rate */
262 wmcodec_write(ADDCTRL, (freq_setups[fsel] & ADDCTRL_SR_MASK)
263 | ADDCTRL_SLOWCLKEN);
264 /* SLOWCLK enabled for zero cross timeout to work */
267 void audiohw_enable_recording(bool source_mic)
269 (void)source_mic; /* We only have a line-in (I think) */
271 wmcodec_write(PWRMGMT2, PWRMGMT2_ROUT1EN | PWRMGMT2_LOUT1EN
272 | PWRMGMT2_INPGAENR | PWRMGMT2_INPGAENL
273 | PWRMGMT2_ADCENR | PWRMGMT2_ADCENL);
275 wmcodec_write(INCTRL, INCTRL_R2_2INPGA | INCTRL_L2_2INPGA);
277 wmcodec_write(LADCBOOST, LADCBOOST_L2_2BOOST(5));
278 wmcodec_write(RADCBOOST, RADCBOOST_R2_2BOOST(5));
280 /* Enable monitoring */
281 wmcodec_write(LOUTMIX, LOUTMIX_BYP2LMIXVOL(5)
282 | LOUTMIX_BYPL2LMIX | LOUTMIX_DACL2LMIX);
283 wmcodec_write(ROUTMIX, ROUTMIX_BYP2RMIXVOL(5)
284 | ROUTMIX_BYPR2RMIX | ROUTMIX_DACR2RMIX);
287 void audiohw_disable_recording(void)
289 wmcodec_write(LOUTMIX, LOUTMIX_DACL2LMIX);
290 wmcodec_write(ROUTMIX, ROUTMIX_DACR2RMIX);
292 wmcodec_write(PWRMGMT2, PWRMGMT2_ROUT1EN | PWRMGMT2_LOUT1EN);
295 void audiohw_set_recvol(int left, int right, int type)
297 switch (type)
299 case AUDIO_GAIN_MIC:
300 right = left;
301 /* fall through */
302 case AUDIO_GAIN_LINEIN:
303 wmcodec_write(LINPGAVOL, LINPGAVOL_INPGAZCL
304 | (left & LINPGAVOL_INPGAVOL_MASK));
305 wmcodec_write(RINPGAVOL, RINPGAVOL_INPGAVU | RINPGAVOL_INPGAZCR
306 | (right & RINPGAVOL_INPGAVOL_MASK));
307 break;
308 default:
309 return;
313 void audiohw_set_monitor(bool enable)
315 (void)enable;