as3514: mute headphones at the lowest volume
[kugel-rb.git] / firmware / drivers / audio / as3514.c
blob25a7bef7967cb224b22caac7775c62c6b49c9936
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Driver for AS3514 and compatible audio codec
12 * Copyright (c) 2007 Daniel Ankers
13 * Copyright (c) 2007 Christian Gmeiner
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "cpu.h"
25 #include "debug.h"
26 #include "system.h"
27 #include "audio.h"
28 #include "sound.h"
30 #include "audiohw.h"
31 #include "i2s.h"
32 #include "ascodec.h"
35 * This drivers supports:
36 * as3514 , as used in the PP targets
37 * as3517 , as used in the as3525 targets
38 * as3543 , as used in the as3525v2 targets
41 #if CONFIG_CPU == AS3525
42 /* AMS Sansas based on the AS3525 use the LINE2 input for the analog radio
43 signal instead of LINE1 */
44 #define AS3514_LINE_IN_R AS3514_LINE_IN2_R
45 #define AS3514_LINE_IN_L AS3514_LINE_IN2_L
46 #define ADC_R_ADCMUX_LINE_IN ADC_R_ADCMUX_LINE_IN2
47 #define AUDIOSET1_LIN_on AUDIOSET1_LIN2_on
49 #elif CONFIG_CPU == AS3525v2
50 /* There is only 1 pair of registers on AS3543, the line input is selectable in
51 LINE_IN_R register */
52 #define AS3514_LINE_IN_R AS3514_LINE_IN1_R
53 #define AS3514_LINE_IN_L AS3514_LINE_IN1_L
54 #define ADC_R_ADCMUX_LINE_IN ADC_R_ADCMUX_LINE_IN2
55 #define AUDIOSET1_LIN_on AUDIOSET1_LIN1_on
57 #else /* PP use line1 */
59 #define AS3514_LINE_IN_R AS3514_LINE_IN1_R
60 #define AS3514_LINE_IN_L AS3514_LINE_IN1_L
61 #define ADC_R_ADCMUX_LINE_IN ADC_R_ADCMUX_LINE_IN1
62 #define AUDIOSET1_LIN_on AUDIOSET1_LIN1_on
64 #endif
66 const struct sound_settings_info audiohw_settings[] = {
67 [SOUND_VOLUME] = {"dB", 0, 1, -73, 6, -25},
68 /* HAVE_SW_TONE_CONTROLS */
69 [SOUND_BASS] = {"dB", 0, 1, -24, 24, 0},
70 [SOUND_TREBLE] = {"dB", 0, 1, -24, 24, 0},
71 [SOUND_BALANCE] = {"%", 0, 1,-100, 100, 0},
72 [SOUND_CHANNELS] = {"", 0, 1, 0, 5, 0},
73 [SOUND_STEREO_WIDTH] = {"%", 0, 5, 0, 250, 100},
74 #ifdef HAVE_RECORDING
75 [SOUND_MIC_GAIN] = {"dB", 1, 1, 0, 39, 23},
76 [SOUND_LEFT_GAIN] = {"dB", 1, 1, 0, 31, 23},
77 [SOUND_RIGHT_GAIN] = {"dB", 1, 1, 0, 31, 23},
78 #endif
81 /* Shadow registers */
82 static uint8_t as3514_regs[AS3514_NUM_AUDIO_REGS]; /* 8-bit registers */
85 * little helper method to set register values.
86 * With the help of as3514_regs, we minimize i2c
87 * traffic.
89 static void as3514_write(unsigned int reg, unsigned int value)
91 if (ascodec_write(reg, value) != 2)
93 DEBUGF("as3514 error reg=0x%02x", reg);
96 if (reg < ARRAYLEN(as3514_regs))
98 as3514_regs[reg] = value;
100 else
102 DEBUGF("as3514 error reg=0x%02x", reg);
106 /* Helpers to set/clear bits */
107 static void as3514_set(unsigned int reg, unsigned int bits)
109 as3514_write(reg, as3514_regs[reg] | bits);
112 static void as3514_clear(unsigned int reg, unsigned int bits)
114 as3514_write(reg, as3514_regs[reg] & ~bits);
117 static void as3514_write_masked(unsigned int reg, unsigned int bits,
118 unsigned int mask)
120 as3514_write(reg, (as3514_regs[reg] & ~mask) | (bits & mask));
123 /* convert tenth of dB volume to master volume register value */
124 int tenthdb2master(int db)
126 /* +6 to -73.5dB in 1.5dB steps == 53 levels */
127 if (db < VOLUME_MIN) {
128 return 0x0;
129 } else if (db >= VOLUME_MAX) {
130 return 0x35;
131 } else {
132 return((db-VOLUME_MIN)/15); /* VOLUME_MIN is negative */
136 int sound_val2phys(int setting, int value)
138 int result;
140 switch(setting)
142 #if defined(HAVE_RECORDING)
143 case SOUND_LEFT_GAIN:
144 case SOUND_RIGHT_GAIN:
145 case SOUND_MIC_GAIN:
146 result = (value - 23) * 15;
147 break;
148 #endif
150 default:
151 result = value;
152 break;
155 return result;
159 * Initialise the PP I2C and I2S.
161 void audiohw_preinit(void)
163 unsigned int i;
165 /* read all reg values */
166 for (i = 0; i < ARRAYLEN(as3514_regs); i++)
168 as3514_regs[i] = ascodec_read(i);
171 /* Set ADC off, mixer on, DAC on, line out off, line in off, mic off */
173 /* Turn on SUM, DAC */
174 as3514_write(AS3514_AUDIOSET1, AUDIOSET1_DAC_on | AUDIOSET1_SUM_on);
176 #ifdef HAVE_AS3543
177 as3514_write(AS3514_AUDIOSET2, AUDIOSET2_HPH_QUALITY_LOW_POWER);
178 #else
179 /* Set BIAS on, DITH off, AGC off, IBR_DAC max reduction, LSP_LP on,
180 IBR_LSP max reduction (50%), taken from c200v2 OF
182 as3514_write(AS3514_AUDIOSET2, AUDIOSET2_IBR_LSP_50 | AUDIOSET2_LSP_LP |
183 AUDIOSET2_IBR_DAC_50 | AUDIOSET2_AGC_off | AUDIOSET2_DITH_off );
184 #endif
186 /* AMS Sansas based on the AS3525 need HPCM enabled, otherwise they output the
187 L-R signal on both L and R headphone outputs instead of normal stereo.
188 Turning it off saves a little power on targets that don't need it. */
189 #if (CONFIG_CPU == AS3525)
190 /* Set HPCM on, ZCU off, reduce bias current, settings taken from c200v2 OF
192 as3514_write(AS3514_AUDIOSET3, AUDIOSET3_IBR_HPH | AUDIOSET3_ZCU_off);
193 #else
194 /* TODO: check if AS3525 settings save power on e200v1 or as3525v2 */
195 /* Set HPCM off, ZCU on */
196 as3514_write(AS3514_AUDIOSET3, AUDIOSET3_HPCM_off);
197 #endif
199 #ifdef HAVE_AS3543
200 as3514_write(AS3543_DAC_IF, AS3543_DAC_INT_PLL);
201 as3514_set(AS3514_LINE_IN1_R, LINE_IN_R_LINE_SELECT); /* Line 2 */
202 #else
203 /* Mute and disable speaker */
204 as3514_write(AS3514_LSP_OUT_R, LSP_OUT_R_SP_OVC_TO_256MS | 0x00);
205 as3514_write(AS3514_LSP_OUT_L, LSP_OUT_L_SP_MUTE | 0x00);
206 #endif
208 #ifdef HAVE_AS3543
209 as3514_write(AS3514_HPH_OUT_R, (0<<7) /* out */ | HPH_OUT_R_HP_OUT_SUM |
210 0x00);
211 #else
212 /* Set headphone over-current to 0, Min volume */
213 as3514_write(AS3514_HPH_OUT_R,
214 HPH_OUT_R_HP_OVC_TO_0MS | 0x00);
215 #endif
216 /* Headphone ON, MUTE, Min volume */
217 as3514_write(AS3514_HPH_OUT_L,
218 HPH_OUT_L_HP_ON | HPH_OUT_L_HP_MUTE | 0x00);
220 #ifdef PHILIPS_SA9200
221 /* LRCK 8-23kHz (there are audible clicks while reading the ADC otherwise) */
222 as3514_write(AS3514_PLLMODE, PLLMODE_LRCK_8_23);
223 #else
224 /* LRCK 24-48kHz */
225 as3514_write(AS3514_PLLMODE, PLLMODE_LRCK_24_48);
226 #endif
228 /* DAC_Mute_off */
229 as3514_set(AS3514_DAC_L, DAC_L_DAC_MUTE_off);
231 /* M1_Sup_off */
232 as3514_set(AS3514_MIC1_L, MIC1_L_M1_SUP_off);
233 #ifndef HAVE_AS3543
234 /* M2_Sup_off */
235 as3514_set(AS3514_MIC2_L, MIC2_L_M2_SUP_off);
236 #endif
239 static void audiohw_mute(bool mute)
241 if (mute) {
242 as3514_set(AS3514_HPH_OUT_L, HPH_OUT_L_HP_MUTE);
243 } else {
244 as3514_clear(AS3514_HPH_OUT_L, HPH_OUT_L_HP_MUTE);
248 void audiohw_postinit(void)
250 /* wait until outputs have stabilized */
251 sleep(HZ/4);
253 #ifdef CPU_PP
254 ascodec_suppressor_on(false);
255 #endif
257 audiohw_mute(false);
260 void audiohw_set_master_vol(int vol_l, int vol_r)
262 unsigned int hph_r, hph_l;
263 unsigned int mix_l, mix_r;
265 if(vol_l == 0 && vol_r == 0)
267 audiohw_mute(true);
268 return;
271 /* We combine the mixer channel volume range with the headphone volume
272 range - keep first stage as loud as possible */
273 if (vol_r <= 0x16) {
274 mix_r = vol_r;
275 hph_r = 0;
276 } else {
277 mix_r = 0x16;
278 hph_r = vol_r - 0x16;
281 if (vol_l <= 0x16) {
282 mix_l = vol_l;
283 hph_l = 0;
284 } else {
285 mix_l = 0x16;
286 hph_l = vol_l - 0x16;
289 as3514_write_masked(AS3514_DAC_R, mix_r, AS3514_VOL_MASK);
290 as3514_write_masked(AS3514_DAC_L, mix_l, AS3514_VOL_MASK);
291 #if defined(HAVE_RECORDING) || defined(HAVE_FMRADIO_IN)
292 as3514_write_masked(AS3514_LINE_IN_R, mix_r, AS3514_VOL_MASK);
293 as3514_write_masked(AS3514_LINE_IN_L, mix_l, AS3514_VOL_MASK);
294 #endif
295 as3514_write_masked(AS3514_HPH_OUT_R, hph_r, AS3514_VOL_MASK);
296 as3514_write_masked(AS3514_HPH_OUT_L, hph_l, AS3514_VOL_MASK);
298 audiohw_mute(false);
301 void audiohw_set_lineout_vol(int vol_l, int vol_r)
303 as3514_write_masked(AS3514_LINE_OUT_R, vol_r, AS3514_VOL_MASK);
304 as3514_write_masked(AS3514_LINE_OUT_L, vol_l, AS3514_VOL_MASK);
307 /* Nice shutdown of AS3514 audio codec */
308 void audiohw_close(void)
310 /* mute headphones */
311 audiohw_mute(true);
313 #ifdef CPU_PP
314 ascodec_suppressor_on(true);
315 #endif
317 /* turn on common */
318 as3514_clear(AS3514_AUDIOSET3, AUDIOSET3_HPCM_off);
320 /* turn off everything */
321 as3514_clear(AS3514_HPH_OUT_L, HPH_OUT_L_HP_ON);
322 as3514_write(AS3514_AUDIOSET1, 0x0);
324 /* Allow caps to discharge */
325 sleep(HZ/4);
328 void audiohw_set_frequency(int fsel)
330 (void)fsel;
333 #if defined(HAVE_RECORDING)
334 void audiohw_enable_recording(bool source_mic)
336 if (source_mic) {
337 /* ADCmux = Stereo Microphone */
338 as3514_write_masked(AS3514_ADC_R, ADC_R_ADCMUX_ST_MIC,
339 ADC_R_ADCMUX);
341 /* MIC1_on, others off */
342 as3514_write_masked(AS3514_AUDIOSET1, AUDIOSET1_MIC1_on,
343 AUDIOSET1_INPUT_MASK);
345 #if CONFIG_CPU == AS3525v2
346 /* Enable supply */
347 as3514_clear(AS3514_MIC1_L, MIC1_L_M1_SUP_off);
348 #endif
350 /* M1_AGC_off */
351 as3514_clear(AS3514_MIC1_R, MIC1_R_M1_AGC_off);
352 } else {
353 /* ADCmux = Line_IN1 or Line_IN2 */
354 as3514_write_masked(AS3514_ADC_R, ADC_R_ADCMUX_LINE_IN,
355 ADC_R_ADCMUX);
357 /* LIN1_or LIN2 on, rest off */
358 as3514_write_masked(AS3514_AUDIOSET1, AUDIOSET1_LIN_on,
359 AUDIOSET1_INPUT_MASK);
361 #if CONFIG_CPU == AS3525v2
362 /* Disable supply */
363 as3514_set(AS3514_MIC1_L, MIC1_L_M1_SUP_off);
364 #endif
367 /* ADC_Mute_off */
368 as3514_set(AS3514_ADC_L, ADC_L_ADC_MUTE_off);
369 /* ADC_on */
370 as3514_set(AS3514_AUDIOSET1, AUDIOSET1_ADC_on);
373 void audiohw_disable_recording(void)
375 /* ADC_Mute_on */
376 as3514_clear(AS3514_ADC_L, ADC_L_ADC_MUTE_off);
378 /* ADC_off, all input sources off */
379 as3514_clear(AS3514_AUDIOSET1, AUDIOSET1_ADC_on | AUDIOSET1_INPUT_MASK);
383 * Set recording volume
385 * Line in : 0 .. 23 .. 31 =>
386 Volume -34.5 .. +00.0 .. +12.0 dB
387 * Mic (left): 0 .. 23 .. 39 =>
388 * Volume -34.5 .. +00.0 .. +24.0 dB
391 void audiohw_set_recvol(int left, int right, int type)
393 switch (type)
395 case AUDIO_GAIN_MIC:
397 /* Combine MIC gains seamlessly with ADC levels */
398 unsigned int mic1_r;
400 if (left >= 36) {
401 /* M1_Gain = +40db, ADR_Vol = +7.5dB .. +12.0 dB =>
402 +19.5 dB .. +24.0 dB */
403 left -= 8;
404 mic1_r = MIC1_R_M1_GAIN_40DB;
405 } else if (left >= 32) {
406 /* M1_Gain = +34db, ADR_Vol = +7.5dB .. +12.0 dB =>
407 +13.5 dB .. +18.0 dB */
408 left -= 4;
409 mic1_r = MIC1_R_M1_GAIN_34DB;
410 } else {
411 /* M1_Gain = +28db, ADR_Vol = -34.5dB .. +12.0 dB =>
412 -34.5 dB .. +12.0 dB */
413 mic1_r = MIC1_R_M1_GAIN_28DB;
416 right = left;
418 as3514_write_masked(AS3514_MIC1_R, mic1_r, MIC1_R_M1_GAIN);
419 break;
421 case AUDIO_GAIN_LINEIN:
422 break;
423 default:
424 return;
427 as3514_write_masked(AS3514_ADC_R, right, AS3514_VOL_MASK);
428 as3514_write_masked(AS3514_ADC_L, left, AS3514_VOL_MASK);
430 #endif /* HAVE_RECORDING */
432 #if defined(HAVE_RECORDING) || defined(HAVE_FMRADIO_IN)
434 * Enable line in analog monitoring
437 void audiohw_set_monitor(bool enable)
439 if (enable) {
440 /* select either LIN1 or LIN2 */
441 as3514_write_masked(AS3514_AUDIOSET1, AUDIOSET1_LIN_on,
442 AUDIOSET1_LIN1_on | AUDIOSET1_LIN2_on);
443 as3514_set(AS3514_LINE_IN_R, LINE_IN1_R_LI1R_MUTE_off);
444 as3514_set(AS3514_LINE_IN_L, LINE_IN1_L_LI1L_MUTE_off);
446 #ifdef HAVE_AS3543
447 as3514_write_masked(AS3514_HPH_OUT_R,
448 HPH_OUT_R_HP_OUT_LINE, HPH_OUT_R_HP_OUT_MASK);
449 #endif
451 else {
452 /* turn off both LIN1 and LIN2 */
453 as3514_clear(AS3514_LINE_IN1_R, LINE_IN1_R_LI1R_MUTE_off);
454 as3514_clear(AS3514_LINE_IN1_L, LINE_IN1_L_LI1L_MUTE_off);
455 #ifdef HAVE_AS3543
456 as3514_write_masked(AS3514_HPH_OUT_R,
457 HPH_OUT_R_HP_OUT_SUM, HPH_OUT_R_HP_OUT_MASK);
458 #else
459 as3514_clear(AS3514_LINE_IN2_R, LINE_IN2_R_LI2R_MUTE_off);
460 as3514_clear(AS3514_LINE_IN2_L, LINE_IN2_L_LI2L_MUTE_off);
461 #endif
462 as3514_clear(AS3514_AUDIOSET1, AUDIOSET1_LIN1_on | AUDIOSET1_LIN2_on);
465 #endif /* HAVE_RECORDING || HAVE_FMRADIO_IN */